Issues with double linked list

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 1
Reputation: lvphoenix is an unknown quantity at this point 
Solved Threads: 0
lvphoenix lvphoenix is offline Offline
Newbie Poster

Issues with double linked list

 
1
  #1
Oct 11th, 2007
Hey All I am having issues with my code and it has something to do with the way my double linked list is working.

I cant figure out if its not setting the m_prev pointer if at all and what is going on with my m_next pointer. Any help would be greatly appreciated I have been banging my head on the desk for the last 7 hours working on it. I just need to get the pointers working correctly and I think that it will be working correctly.

Just a little bit of background this is a homework project I am working on that is suppose to simulate a operating system scheduler. The main contains a variable that gets used over and over again during input so its contents are always overwritten after the while loop restarts. it is called process. I want to insert into the Startup queue and I thought I had it until I did some cout statements to see what was going on.

Thanks In advanced!!

Here is the complete code:

  1. //***********************************
  2. // include files
  3. //***********************************
  4. #include <iostream>
  5. #include <fstream>
  6. #include <iomanip>
  7. #include <cctype>
  8. #include <cstdlib>
  9. #include <cmath>
  10. using namespace std;
  11. typedef unsigned char boolean;
  12.  
  13. //***********************************
  14. // define directives
  15. //***********************************
  16. // Directive to store the minimum time slice
  17. #define MIN_TIME_SLICE 10
  18. // Directive to store the maximum time slice
  19. #define MAX_TIME_SLICE 300
  20. // Directive to store the lowest priority (*note* higher number = lower priority)
  21. #define MIN_PRIORITY 140
  22. // Directive to store the highest priority(*note* lower number = higher priority)
  23. #define MAX_PRIORITY 100
  24. // Directive to store the highest nice value(*note* lower number = nicer Process)
  25. #define MAX_NICE = -20
  26. // Directive to store the lowest nice value (*note* higher number = not so nice Process)
  27. #define MIN_NICE = 19
  28.  
  29. //***********************************
  30. // globals
  31. //***********************************
  32.  
  33. // Clock for my scheduler
  34. long myClock;
  35. // Max amount of proccess in the file
  36. int maxProcesses;
  37. // Total turn around time for all Processes
  38. long totalTAT;
  39. // Total wait time for all Processes
  40. long totalWT;
  41. // Total CPU utiliation Time for all Processes
  42. double totalCUT;
  43. // The global count variables that gets used over and over again
  44. int i, j, k;
  45. // The global temp integer variable
  46. int tempInt;
  47.  
  48. // class that defines what a Processes is
  49. class ProcessContents{
  50. public:
  51. // constructors
  52. ProcessContents( );
  53. ProcessContents(ProcessContents*);
  54. // values in a process
  55. int pid; //pid of Process
  56. int nice; // Processes nice value
  57. int arrivalTime; // Processes arrival time
  58. int numCPUBursts; // number of cpu bursts
  59. int numIOBursts; // number of i/o bursts = #cpu bursts - 1
  60. int totalBursts;
  61. int endTime; // Processes end time
  62. int priority; // priority of Process
  63. int timeslice; // Processes timesliceSTRUCT AS A FUNCTION PARAMETER DECLARATION
  64. int* burst; // dynamic array of cpu bursts and io bursts for the Process
  65.  
  66. void insert(ProcessContents*);
  67. void setData(ProcessContents*);
  68. // setters
  69. ProcessContents* getPrev();
  70. ProcessContents* getNext();
  71. void setNext(ProcessContents*);
  72. ProcessContents getData();
  73. int getDataEl();
  74.  
  75. void reset();
  76. void prev();
  77. void next();
  78. void end();
  79.  
  80. //Boolean
  81. boolean isBeginning();
  82. boolean isEnd();
  83. boolean isEmpty();
  84.  
  85. private:
  86. ProcessContents *m_next, *m_prev, *m_curr;
  87.  
  88. };
  89.  
  90.  
  91.  
  92. ProcessContents :: ProcessContents() {
  93. m_prev = NULL;
  94. m_next = NULL;
  95. m_curr = this;
  96. endTime = 0;
  97. priority = 69;
  98. timeslice = 143;
  99. burst = NULL;
  100. }
  101.  
  102. ProcessContents :: ProcessContents(ProcessContents* previous) {
  103. m_prev = previous;
  104. cout << " PRINT PREVIOUS " << previous << endl;
  105. m_next = NULL;
  106. priority = 96;
  107.  
  108. }
  109.  
  110. void ProcessContents::insert(ProcessContents* pNode) {
  111. while (m_curr->getNext() != NULL)
  112. m_curr = m_curr->getNext();
  113.  
  114. m_curr->setData(pNode);
  115. cout << "POINTERS: "<< m_curr<< " " << m_next << " " << m_prev << endl;
  116. m_curr->setNext(new ProcessContents(m_curr));
  117. cout << "POINTERS AFTER: "<< m_curr<< " " << m_next << " " << m_prev << endl;
  118.  
  119. // creates the link between a newly created list and the current list
  120. }
  121.  
  122. void ProcessContents::setData(ProcessContents* pNode) {
  123. int z;
  124. pid = pNode -> pid;
  125. nice = pNode -> nice;
  126. arrivalTime = pNode -> arrivalTime;
  127. numCPUBursts = pNode -> numCPUBursts;
  128. numIOBursts = pNode -> numIOBursts;
  129. totalBursts = pNode -> totalBursts;
  130. endTime = pNode -> endTime;
  131. priority = pNode -> priority;
  132. timeslice = pNode -> timeslice;
  133. burst = new int[totalBursts];
  134. for (z = totalBursts - 1; z >= 0; z--) {
  135. burst[z] = pNode -> burst[z];
  136. }
  137.  
  138. }
  139.  
  140. ProcessContents ProcessContents::getData() {
  141. cout << "Pid: " << pid << endl;
  142. cout << "Arrival time: " << arrivalTime << endl;
  143. cout << "Priority: " << priority << endl;
  144. cout << "Total Burst: " << totalBursts << endl;
  145.  
  146. }
  147.  
  148. ProcessContents* ProcessContents::getPrev() {
  149. return m_prev;
  150. }
  151.  
  152. ProcessContents* ProcessContents::getNext() {
  153. return m_next;
  154. }
  155.  
  156. void ProcessContents::setNext(ProcessContents* link) {
  157. m_next = link;
  158. }
  159.  
  160.  
  161. void ProcessContents::reset() {
  162. m_curr = this;
  163. }
  164.  
  165. void ProcessContents::next() {
  166. m_curr = m_curr->getNext();
  167. }
  168.  
  169. void ProcessContents::prev() {
  170. m_curr = m_curr->getPrev();
  171. }
  172.  
  173. boolean ProcessContents::isEnd() {
  174. return m_curr->getNext() == NULL ? 1 : 0;
  175. }
  176.  
  177. boolean ProcessContents::isBeginning() {
  178. return m_curr == NULL ? 1 : 0; ;
  179. }
  180.  
  181. boolean ProcessContents::isEmpty() {
  182. return m_curr->getNext() == m_curr->getPrev() ? 1 : 0;
  183. }
  184.  
  185. /*
  186. int Queue :: calculateDynPty(ProcessContents* node){
  187. // have to adjust this part
  188.   node -> priority = (node -> priority) + (-1)*((node -> Bonus) + (node -> priority));
  189.  
  190.   return (node -> priority);
  191. }
  192.  
  193. double Queue :: calculateTimeSlice(ProcessContents *node){
  194.   node -> priority = (int)ceil((-180.0/49.0)*(double)(calculateDynPty(node))+(26900.0/49.0));
  195.   return (node -> priority);
  196. }
  197.  
  198. */
  199.  
  200. //***********************************
  201. // function definitions
  202. //***********************************
  203.  
  204. // Function that gets all the input from unix file redirection and places them into the startup queue
  205.  
  206. /*
  207. // *****************MOST PRINT STATEMENTS*******************************
  208. // function that prints Arrival Processes in the active queue
  209. void printArrivalActive(Process myProcessIn);
  210. // function that prints when a Process enters the cpu
  211. void printEnterCPU(Process myProcessIn, int time);
  212. // function that prints when a Process is preempted
  213. void printPreempt(Process myProcessIn, Process myProcessOut, int time);
  214. //function that prints when a Process is finsihed with all cpu bursts
  215. void printFinishCPU(Process myProcessOut, int time);
  216. // function that prints when a Process finishes a cpu burst and moves to the I/O queue
  217. void printCPUToIO(Process myProcessOut, int time);
  218. // funcation that prints when a Process finishes its timeslice and moves to the expired queue
  219. void printExpiredTimeslice(Process myProcessOut, int time);
  220. // function that prints when a Process finishes I/O burst and moves to the expired queue
  221. void printIOToExpired(Process myProcessOut, int time);
  222. // function that prints when a Process finishes I/O burst and moves to active queue
  223. void printIOToActive(Process myProcessOut, int time);
  224. // function that prints when the active and expired arrays are swapped
  225. void printSwap(int time);
  226.  
  227. // *****************MOST CALC STATEMENTS********************************
  228. // function that calculates Turn around time for a Process
  229. void calcTAT(Process myProcessIn);
  230. // fucntion that calculates Total CPU time
  231. void calcTCT(Process myProcessIn);
  232. // funtion that calculates Wait Time
  233. void calcWT(Process myProcessIn);
  234. // function that calculates percentage of CPU utilization time
  235. void calcCUT(Process myProcessIn);
  236. // function that calculates the average Turn around time
  237. void calcAvgTAT();
  238. // function that calculates the average Wait time
  239. void calcAvgWT();
  240. // function that calculates the average CPU utilization time
  241. void calcAvgCUT();
  242.  
  243.  
  244. */
  245. //***********************************
  246. // main
  247. //***********************************
  248.  
  249. int main() {
  250.  
  251. ProcessContents *process;
  252. ProcessContents Startup, Active, Expired, Finished, Io, Cpu;
  253.  
  254. int numTotalBursts= 0;
  255. // check to see if the input is specified and correct
  256.  
  257. i = 0;
  258. // Get input from the file and load startup queue
  259. cout << " Begin startup queue initialization " << endl;
  260. // get the max number of Processes
  261. cin >> maxProcesses;
  262. process = new ProcessContents;
  263. // Check to make sure its not the end of the file
  264. while(cin.eof() == 0 && maxProcesses != 0) {
  265. // give it a pid first
  266. process -> pid = i;
  267. i++;
  268. cout << "pid:" << process -> pid << endl;
  269. // get Processes nice value
  270. cin >> process -> nice;
  271. cout << "nice:" << process -> nice << endl;
  272. // get Processes arrival time
  273. cin >> process -> arrivalTime;
  274. cout << "atime:" << process -> arrivalTime << endl;
  275. // get Processes number of cpu bursts
  276. cin >> process -> numCPUBursts;
  277. cout << "#cpu bursts:" << process -> numCPUBursts << endl;
  278. // calculate the number of io bursts ( numer of cpu bursts - 1)
  279. process -> numIOBursts = process -> numCPUBursts - 1;
  280. cout << "#io bursts:" << process -> numIOBursts << endl;
  281. // get the total bursts in the process
  282. process -> totalBursts = process -> numIOBursts + process -> numCPUBursts;
  283. cout << "#total bursts: " << process -> totalBursts << endl;
  284. process -> burst = new int[process -> totalBursts];
  285.  
  286. // get the cpu bursts or io bursts
  287. for (j = process -> totalBursts - 1; j >= 0; j--) {
  288. cin >> process -> burst[j];
  289. cout << "Burst: " << j << " is " << process-> burst[j] << " long." << endl;
  290.  
  291. } // end of read for bursts
  292.  
  293. Startup.insert(process);
  294.  
  295. } // end of read for an entire file
  296. cout << " Done initilizing startup queue " << endl;
  297. for(; !Startup.isBeginning(); Startup.prev())
  298.  
  299. Startup.getData();
  300.  
  301. return 0;
  302. }
  303.  
  304. //***********************************
  305. // functions
  306. //***********************************
  307.  
  308. /*
  309. // function that prints Arrival Processes in the active queue
  310. void printArrivalActive(Process myProcessIn) {
  311.  
  312. }
  313.  
  314. // function that prints when a Process enters the cpu
  315. void printEnterCPU(Process myProcessIn, int time) {
  316.  
  317. }
  318.  
  319. // function that prints when a Process is preempted
  320. void printPreempt(Process myProcessIn, Process myProcessOut, int time) {
  321.  
  322. }
  323.  
  324. //function that prints when a Process is finsihed with all cpu bursts
  325. void printFinishCPU(Process myProcessOut, int time) {
  326.  
  327. }
  328.  
  329. // function that prints when a Process finishes a cpu burst and moves to the I/O queue
  330. void printCPUToIO(Process myProcessOut, int time) {
  331.  
  332. }
  333.  
  334. // funcation that prints when a Process finishes its timeslice and moves to the expired queue
  335. void printExpiredTimeslice(Process myProcessOut, int time) {
  336.  
  337. }
  338.  
  339. // function that prints when a Process finishes I/O burst and moves to the expired queue
  340. void printIOToExpired(Process myProcessOut, int time) {
  341.  
  342. }
  343.  
  344. // function that prints when a Process finishes I/O burst and moves to active queue
  345. void printIOToActive(Process myProcessOut, int time) {
  346.  
  347. }
  348.  
  349. // function that prints when the active and expired arrays are swapped
  350. void printSwap(int time) {
  351.  
  352. }
  353.  
  354. // function that calculates Turn around time for a Process
  355. void calcTAT(Process myProcessIn) [
  356.  
  357. }
  358.  
  359. // fucntion that calculates Total CPU time
  360. void calcTCT(Process myProcessIn) {
  361.  
  362. }
  363.  
  364. // funtion that calculates Wait Time
  365. void calcWT(Process myProcessIn) {
  366.  
  367. }
  368.  
  369. // function that calculates percentage of CPU utilization time
  370. void calcCUT(Process myProcessIn) {
  371.  
  372. }
  373.  
  374. // function that calculates the average Turn around time
  375. void calcAvgTAT() {
  376.  
  377. }
  378.  
  379. // function that calculates the average Wait time
  380. void calcAvgWT() {
  381.  
  382. }
  383.  
  384. // function that calculates the average CPU utilization time
  385. void calcAvgCUT() {
  386.  
  387. }
  388. */

my input file looks like:

6
0 16 3 100 100 100 100 100
-4 100 3 300 30 300 10 200
-20 200 2 12 30 300
0 25 1 300
15 50 3 340 290 582 102 10
-15 0 4 20 400 10 200 100 45 6

and my output right now currently shows the address of pointers after certain executions and is a little messy since I have been working on debugging it for awhile.

Begin startup queue initialization
pid:0
nice:0
atime:16
#cpu bursts:3
#io bursts:2
#total bursts: 5
Burst: 4 is 100 long.
Burst: 3 is 100 long.
Burst: 2 is 100 long.
Burst: 1 is 100 long.
Burst: 0 is 100 long.
POINTERS: 0xbfc5447c 0 0
PRINT PREVIOUS 0xbfc5447c
POINTERS AFTER: 0xbfc5447c 0x9360070 0
pid:1
nice:-4
atime:100
#cpu bursts:3
#io bursts:2
#total bursts: 5
Burst: 4 is 300 long.
Burst: 3 is 30 long.
Burst: 2 is 300 long.
Burst: 1 is 10 long.
Burst: 0 is 200 long.
POINTERS: 0x9360070 0x9360070 0
PRINT PREVIOUS 0x9360070
POINTERS AFTER: 0x9360070 0x9360070 0
pid:2
nice:-20
atime:200
#cpu bursts:2
#io bursts:1
#total bursts: 3
Burst: 2 is 12 long.
Burst: 1 is 30 long.
Burst: 0 is 300 long.
POINTERS: 0x93600d8 0x9360070 0
PRINT PREVIOUS 0x93600d8
POINTERS AFTER: 0x93600d8 0x9360070 0
pid:3
nice:0
atime:25
#cpu bursts:1
#io bursts:0
#total bursts: 1
Burst: 0 is 300 long.
POINTERS: 0x9360130 0x9360070 0
PRINT PREVIOUS 0x9360130
POINTERS AFTER: 0x9360130 0x9360070 0
pid:4
nice:15
atime:50
#cpu bursts:3
#io bursts:2
#total bursts: 5
Burst: 4 is 340 long.
Burst: 3 is 290 long.
Burst: 2 is 582 long.
Burst: 1 is 102 long.
Burst: 0 is 10 long.
POINTERS: 0x9360188 0x9360070 0
PRINT PREVIOUS 0x9360188
POINTERS AFTER: 0x9360188 0x9360070 0
pid:5
nice:-15
atime:0
#cpu bursts:4
#io bursts:3
#total bursts: 7
Burst: 6 is 20 long.
Burst: 5 is 400 long.
Burst: 4 is 10 long.
Burst: 3 is 200 long.
Burst: 2 is 100 long.
Burst: 1 is 45 long.
Burst: 0 is 6 long.
POINTERS: 0x93601f0 0x9360070 0
PRINT PREVIOUS 0x93601f0
POINTERS AFTER: 0x93601f0 0x9360070 0
Done initilizing startup queue
Pid: 0
Arrival time: 16
Priority: 69
Total Burst: 5
Pid: 0
Arrival time: 16
Priority: 69
Total Burst: 5
Pid: 0
Arrival time: 16
Priority: 69
Total Burst: 5
Pid: 0
Arrival time: 16
Priority: 69
Total Burst: 5
Pid: 0
Arrival time: 16
Priority: 69
Total Burst: 5
Pid: 0
Arrival time: 16
Priority: 69
Total Burst: 5
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 14
Reputation: vincent551987vn is an unknown quantity at this point 
Solved Threads: 1
vincent551987vn's Avatar
vincent551987vn vincent551987vn is offline Offline
Newbie Poster

Re: Issues with double linked list

 
0
  #2
Oct 12th, 2007
A big construction. You may be good at linked list. plz show me some of document i can improve and develop my skill at this. thank you first.
Say It Like It Is.
Có sao nói vậy.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Issues with double linked list

 
0
  #3
Oct 12th, 2007
>Any help would be greatly appreciated I have been banging
>my head on the desk for the last 7 hours working on it.
Remove the unnecessary code and work directly with the linked list. You're definitely not handling the links correctly, so I'm going to assume that you need a refresher on linked lists in general and send you here.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC