Link List Move Method

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

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

Link List Move Method

 
0
  #1
Dec 14th, 2005
Below I have attached 4 methods from my linked list class.


1) find 2)remove 3)retrieve 4) Insert.

what I'm trying to do is write code that will let me MOVE the position of a node to another position in the linked list.

Anyone got a clue??

regards

Tim


  1. SeqItemType Sequence::retrieve(int index)
  2. {
  3.  
  4. if ((index < 1) || (index > size())){
  5. // can't delete something that isn't there
  6. }
  7. ListNode *cur = find(index);
  8. return cur->item;
  9. }
  10.  
  11.  
  12.  
  13. void Sequence::insert(int index, SeqItemType newItem)
  14. {
  15.  
  16. if ((index < 1) || (index > (size()+1))){
  17. // if index is out of bounds don't do anything
  18. }
  19. else
  20. {
  21. // otherwise create new node and place newItem in it
  22. ListNode *newPtr = new ListNode();
  23.  
  24. // increment the size attribute
  25. length++;
  26. // store the data item in the new node
  27. newPtr->item = newItem;
  28.  
  29. // if the index is 1, insert new node at beginning of list
  30. if (index == 1)
  31. {
  32. newPtr->next = head;
  33. head = newPtr;
  34. }
  35. else
  36. {
  37. ListNode *prev = find(index-1);
  38. newPtr->next = prev->next;
  39. prev->next = newPtr;
  40.  
  41. }
  42. }
  43. }
  44.  
  45.  
  46. void Sequence::remove(int index)
  47. {
  48.  
  49. ListNode *cur;
  50.  
  51. if ((index < 1) || (index > size())){
  52.  
  53. }
  54. else
  55. {
  56. // remove the first node in the list
  57. if (index == 1)
  58. {
  59. cur = head;
  60. head = head->next;
  61.  
  62. }
  63. else{
  64. ListNode *prev = find(index-1);
  65.  
  66. cur = prev->next;
  67. prev->next = cur->next;
  68. }
  69.  
  70. // clean things up!
  71. length--;
  72. cur->next = NULL;
  73. delete cur;
  74. }
  75.  
  76. }
  77.  
  78.  
  79. ListNode *Sequence::find(int index){
  80.  
  81. if ((index <1) || (index >size())){
  82. return 0;
  83. }
  84. else{
  85. ListNode *cur = head;
  86. for(int skip =1; skip<index; ++skip)
  87. cur=cur->next;
  88. return cur;
  89. }
  90. }
Last edited by Dave Sinkula; Dec 15th, 2005 at 12:09 am. Reason: Added [code][/code] tags and removed Red coloring.
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: Link List Move Method

 
0
  #2
Dec 14th, 2005
Ok, I cant handle it. my head is wrecked from trying out but miserably failing with new code. Has anyone got a clue where to go from here. Can some take pity on this man....after all its christams
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 78
Reputation: perniciosus is an unknown quantity at this point 
Solved Threads: 4
perniciosus's Avatar
perniciosus perniciosus is offline Offline
Junior Poster in Training

Re: Link List Move Method

 
0
  #3
Dec 15th, 2005
What is wrong with using the functions you already got
  1. void Sequence::move(int from, int to)
  2. {
  3. SeqItemType item = find( from )->item ;
  4. remove(from) ;
  5. insert( to, item ) ;
  6. }
/pern.*/i

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
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: Link List Move Method

 
0
  #4
Dec 15th, 2005
Perfect, solution.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1493 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC