linked list question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

linked list question

 
0
  #1
Oct 5th, 2008
Im having real trouble seeing this, how do I delete something out of a linked list if it is already there? My current delete function just deletes everything out of the list.

the text file contains numbers like this:
75
85
95
25
35
75
85
95
25

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. class List
  7. {
  8. public:
  9. void Insert(int);
  10. void Print();
  11. //
  12. void Delete(int);
  13. int Length(int);
  14. };
  15.  
  16. struct node
  17. {
  18. int item;
  19. node *nxt;
  20. };
  21. node *start_ptr = 0;
  22. node *current;
  23.  
  24.  
  25. void List::Delete(int item)
  26. {
  27. if(start_ptr==NULL)
  28. cout<<"NULL";
  29.  
  30. node* temp=start_ptr;
  31.  
  32. if(temp->item==item)
  33. {
  34. start_ptr=temp->nxt;
  35.  
  36. delete temp;
  37. }
  38. }
  39. void List::Insert(int item)
  40. {
  41. node *temp, *temp2; // Temporary pointers
  42.  
  43. // Reserve space for new node and fill it with data
  44. temp = new node;
  45. temp->item=item;
  46.  
  47. temp->nxt = NULL;
  48.  
  49. // Set up link to this node
  50. if (start_ptr == NULL)
  51. {
  52. start_ptr = temp;
  53. current = start_ptr;
  54. }
  55. else
  56. {
  57. temp2 = start_ptr;
  58.  
  59. while (temp2->nxt != NULL)
  60. {
  61. temp2 = temp2->nxt;
  62. // Move to next link in chain
  63. }
  64. temp2->nxt = temp;
  65. }
  66. }
  67. void List::Print()
  68. {
  69. node *temp;
  70. temp = start_ptr;
  71.  
  72. if (temp == NULL)
  73. cout << "The list is empty" << endl;
  74. else
  75. { while (temp != NULL)
  76. { // Display details for what temp points to
  77. cout << temp->item << " ";
  78.  
  79. if (temp == current)
  80. cout << " <--";
  81. cout << endl;
  82. temp = temp->nxt;
  83.  
  84. }
  85. cout << "End of list" << endl;
  86. }
  87. }
  88. int List::Length(int item)
  89. {
  90. node *temp;
  91. temp=start_ptr;
  92. temp->item=item;
  93.  
  94. int count=0;
  95.  
  96. while(temp != 0)
  97. {
  98. temp=temp->nxt;
  99. count++;
  100. }
  101. return count;
  102. }
  103. void main()
  104. {
  105. List display;
  106. fstream inFile;
  107. node number;
  108. inFile.open("numbers.txt");
  109.  
  110. while(inFile>>number.item)
  111. {
  112. display.Insert(number.item);
  113. display.Delete(number.item);
  114. }
  115. display.Print();
  116. //cout<<display.Length(number.item);
  117. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,597
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1489
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: linked list question

 
0
  #2
Oct 5th, 2008
Its done something like this: Note: not compiled or tested.

  1. node* temp=start_ptr;
  2. node* prev = NULL;
  3. while( temp )
  4. {
  5. if( temp->item == item)
  6. {
  7. // delete this node
  8. if( prev == NULL)
  9. {
  10. // we are at the head of the linked list, for remove the head
  11. start_ptr = start_ptr->nxt;
  12. }
  13. else
  14. {
  15. // we're somewhere in the middle of the linked list
  16. prev->nxt = temp->nxt;
  17. }
  18. delete temp;
  19. break; // all done
  20. }
  21. prev = hold;
  22. hold = hold->next;
  23. }
Last edited by Ancient Dragon; Oct 5th, 2008 at 7:49 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: linked list question

 
0
  #3
Oct 5th, 2008
This just seems to do the samething, I get an empty list by the end.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,597
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1489
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: linked list question

 
0
  #4
Oct 5th, 2008
post new code.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: linked list question

 
0
  #5
Oct 5th, 2008
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. class List
  7. {
  8. public:
  9. void Insert(int);
  10. void Print();
  11. //
  12. void Delete(int);
  13. int Length(int);
  14. };
  15.  
  16. struct node
  17. {
  18. int item;
  19. node *nxt;
  20. };
  21. node *start_ptr = 0;
  22. node *current;
  23.  
  24.  
  25. void List::Delete(int item)
  26. {
  27. node* temp=start_ptr;
  28. node* prev = NULL;
  29. node* hold;
  30.  
  31. while( temp )
  32. {
  33. if( temp->item == item)
  34. {
  35. // delete this node
  36. if( prev == NULL)
  37. {
  38. // we are at the head of the linked list, for remove the head
  39. start_ptr = start_ptr->nxt;
  40. }
  41. else
  42. {
  43. // we're somewhere in the middle of the linked list
  44. prev->nxt = temp->nxt;
  45. }
  46. delete temp;
  47. break; // all done
  48. }
  49. prev = hold;
  50. hold = hold->nxt;
  51. }
  52. }
  53. void List::Insert(int item)
  54. {
  55. node *temp, *temp2; // Temporary pointers
  56.  
  57. // Reserve space for new node and fill it with data
  58. temp = new node;
  59. temp->item=item;
  60.  
  61. temp->nxt = NULL;
  62.  
  63. // Set up link to this node
  64. if (start_ptr == NULL)
  65. {
  66. start_ptr = temp;
  67. current = start_ptr;
  68. }
  69. else
  70. {
  71. temp2 = start_ptr;
  72.  
  73. while (temp2->nxt != NULL)
  74. {
  75. temp2 = temp2->nxt;
  76. // Move to next link in chain
  77. }
  78. temp2->nxt = temp;
  79. }
  80. }
  81. void List::Print()
  82. {
  83. node *temp;
  84. temp = start_ptr;
  85.  
  86. if (temp == NULL)
  87. cout << "The list is empty" << endl;
  88. else
  89. { while (temp != NULL)
  90. { // Display details for what temp points to
  91. cout << temp->item << " ";
  92.  
  93. if (temp == current)
  94. cout << " <--";
  95. cout << endl;
  96. temp = temp->nxt;
  97.  
  98. }
  99. cout << "End of list" << endl;
  100. }
  101. }
  102. int List::Length(int item)
  103. {
  104. node *temp;
  105. temp=start_ptr;
  106. temp->item=item;
  107.  
  108. int count=0;
  109.  
  110. while(temp != 0)
  111. {
  112. temp=temp->nxt;
  113. count++;
  114. }
  115. return count;
  116. }
  117. void main()
  118. {
  119. List display;
  120. fstream inFile;
  121. node number;
  122. inFile.open("numbers.txt");
  123.  
  124. while(inFile>>number.item)
  125. {
  126. display.Insert(number.item);
  127. display.Delete(number.item);
  128. }
  129.  
  130. display.Print();
  131. //cout<<display.Length(number.item);
  132. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,597
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1489
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: linked list question

 
0
  #6
Oct 5th, 2008
The problem is in main(), not that delete function. First it calls insert to insert a new number then turns right around and deletes it. Delete that line
  1. while(inFile>>number.item)
  2. {
  3. display.Insert(number.item);
  4. //display.Delete(number.item); <<<<< delete this line
  5. }
Last edited by Ancient Dragon; Oct 5th, 2008 at 9:07 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: linked list question

 
0
  #7
Oct 5th, 2008
But I want the delete function to delete any duplicates in the linked list. If I just delete that function then I still have the whole Linked list, duplicates included.
the text file contains numbers like this:
75
85
95
25
35
75
85
95
25

I want it to display these numbers only out of that list:
75
85
95
25
35
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,597
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1489
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: linked list question

 
0
  #8
Oct 5th, 2008
Check to see if the number is already in the list before adding it. The delete() function is not the place to do that. Write another class method that does nothing but search the list for a number, return true if the number already exists in the list or false if it doesn't. Then if it doesn't call that Insert() function.
Last edited by Ancient Dragon; Oct 5th, 2008 at 9:23 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: linked list question

 
0
  #9
Oct 5th, 2008
Ok thanks for the help, I thought I had to delete from the list already created...I didnt think to just not include them!
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC