linked list small problem with Insert Function

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

Join Date: May 2005
Posts: 42
Reputation: TimC is an unknown quantity at this point 
Solved Threads: 0
TimC TimC is offline Offline
Light Poster

linked list small problem with Insert Function

 
0
  #1
Dec 14th, 2005
the following code will print out all the names of patients I have in a linked list.

I have given the user an option to choose which patient they would like to move in the linked list.

i thought if I called S.insert(passed the new postition, and P (where P is the object of the patient class) it would work.

then all I had to do was remove the previous location of that position.

when I run the code it only removes the patient but dose not add it to the location I want.

is the code right by me below. I have just added snippets as the whole project would be too confusing.

thx

tim



  1. for(int a=1;a<(S.size()+1);a++)
  2. {
  3. cout<<a<<". "; S.display(a);
  4. }
  5. cout<<"\nWhich Patient would you like to treat"<<endl;
  6. cin>>num;
  7. cout<<"\nYou have chosen to treat:\n"<<endl;
  8. S.displayall(num);
  9. cout<<"\nWhat position would you like to place this patient in the Priority Q?"<<endl;
  10. cin>>position;
  11. S.insert(position,P);
  12. S.remove(num);
  13. }

  1. #include <iomanip>
  2.  
  3.  
  4. using namespace std;
  5. typedef patient SeqItemType;
  6.  
  7. class ListNode
  8. {
  9. public:
  10. SeqItemType item;
  11. ListNode *next;
  12.  
  13. };
  14.  
  15.  
  16.  
  17. class Sequence
  18. {
  19.  
  20. public:
  21. Sequence();
  22.  
  23.  
  24. bool isEmpty();
  25. int size();
  26. void insert(int index, SeqItemType newItem);
  27. void remove(int index);
  28. SeqItemType retrieve(int index);
  29. void display(int index);
  30. void displayall(int index);
  31.  
  32. private:
  33. ListNode *find(int index);
  34. int length;
  35. ListNode *head;
  36.  
  37. };
  38.  
  39. Sequence::Sequence()
  40. {
  41. length = 0;
  42. head = NULL;
  43. }
  44.  
  45. bool Sequence::isEmpty()
  46. {
  47. return length==0;
  48. }
  49.  
  50. int Sequence::size()
  51. {
  52. return length;
  53. }
  54.  
  55.  
  56. void Sequence::display(int index)
  57. {
  58. ListNode *cur = find(index);
  59.  
  60.  
  61. cout<<"Name :"<< cur->item.get_Name() << endl;
  62. //cout<<"Date of Birth :"<<cur->item.get_dob() << endl;
  63. //cout<<"Illness :"<<cur->item.get_illness() << endl;
  64. //cout<<"Priority :"<<cur->item.get_priority() << endl;
  65. //cout<<"Ward :"<<cur->item.get_ward_no() << endl;
  66. //cout<<"\n\n";
  67.  
  68.  
  69. }
  70.  
  71.  
  72.  
  73. SeqItemType Sequence::retrieve(int index)
  74. {
  75. if ((index < 1) || (index > size())){
  76. // can't delete something that isn't there
  77. }
  78. ListNode *cur = find(index);
  79. return cur->item;
  80. }
  81.  
  82. void Sequence::insert(int index, SeqItemType newItem)
  83. {
  84.  
  85. if ((index < 1) || (index > (size()+1))){
  86. // if index is out of bounds don't do anything
  87. }
  88. else
  89. {
  90. // otherwise create new node and place newItem in it
  91. ListNode *newPtr = new ListNode();
  92.  
  93. // increment the size attribute
  94. length++;
  95. // store the data item in the new node
  96. newPtr->item = newItem;
  97.  
  98. // if the index is 1, insert new node at beginning of list
  99. if (index == 1)
  100. {
  101. newPtr->next = head;
  102. head = newPtr;
  103. }
  104. else
  105. {
  106. ListNode *prev = find(index-1);
  107. newPtr->next = prev->next;
  108. prev->next = newPtr;
  109.  
  110. }
  111. }
  112. }
  113.  
  114.  
  115. void Sequence::remove(int index)
  116. {
  117.  
  118. ListNode *cur;
  119.  
  120. if ((index < 1) || (index > size())){
  121.  
  122. }
  123. else
  124. {
  125. // remove the first node in the list
  126. if (index == 1)
  127. {
  128. cur = head;
  129. head = head->next;
  130.  
  131. }
  132. else{
  133. ListNode *prev = find(index-1);
  134.  
  135. cur = prev->next;
  136. prev->next = cur->next;
  137. }
  138.  
  139. // clean things up!
  140. length--;
  141. cur->next = NULL;
  142. delete cur;
  143. }
  144.  
  145. }
  146.  
  147.  
  148. ListNode *Sequence::find(int index){
  149.  
  150. if ((index <1) || (index >size())){
  151. return 0;
  152. }
  153. else{
  154. ListNode *cur = head;
  155. for(int skip =1; skip<index; ++skip)
  156. cur=cur->next;
  157. return cur;
  158. }
  159. }
  160.  
  161.  
  162. void Sequence::displayall(int index)
  163. {
  164. ListNode *cur = find(index);
  165.  
  166. cout<<index<<". Name: "<<cur->item.get_Name() << endl;
  167. cout<<" Date of Birth: "<<cur->item.get_dob() << endl;
  168. cout<<" Illness: "<<cur->item.get_illness() << endl;
  169. cout<<" Priority: "<<cur->item.get_priority() << endl;
  170. cout<<" Ward: "<<cur->item.get_ward_no() << endl;
  171. cout<<"\n\n";
  172.  
  173. }
  174.  
  175.  
  176. /*void Sequence::displayall()
  177.   {
  178.   for (ListNode *cur = head; cur!= NULL; cur = cur->next)
  179. {
  180.  
  181.   cout<<"Name :"<< cur->item.get_Name() << endl;
  182.   //cout<<"Date of Birth :"<<cur->item.get_dob() << endl;
  183.   //cout<<"Illness :"<<cur->item.get_illness() << endl;
  184.   //cout<<"Priority :"<<cur->item.get_priority() << endl;
  185.   //cout<<"Ward :"<<cur->item.get_ward_no() << endl;
  186.   //cout<<"\n\n";
  187.  
  188. }
  189.   }*/
Last edited by Dave Sinkula; Dec 15th, 2005 at 12:07 am. Reason: Replaced color tags with [code][/code] tags.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 42
Reputation: TimC is an unknown quantity at this point 
Solved Threads: 0
TimC TimC is offline Offline
Light Poster

Re: linked list small problem with Insert Function

 
0
  #2
Dec 16th, 2005
solution thanks to perniciosus



  1.  
  2. void Sequence::move(int from, int to)
  3. {
  4. SeqItemType item = find( from )->item ;
  5. remove(from) ;
  6. insert( to, item ) ;
  7. }
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