Linked List: Search and Modify Help

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

Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Linked List: Search and Modify Help

 
0
  #1
Nov 13th, 2008
This is my goal:

- Search the list for a number and display how many times the number occur or display 'number not present'

- Modify number and change each occurrence of a #, to the new number or display 'number not present'


I have 2 functions.

My first function is called "searchNum".... The problem I have in that function is whenever i input a number..lets say "1"... my counter will only say that it occurs only once even when I type in "1" twice for the input. I want to output how many times the number I input occurs.

My second function is called "modifyNum"...I want to modify the Number and output how many times the number occur. So far, I'm asking a user what number do you want to modify and what number you would like to change it to. When I print my list, the modified number stays the same. It doesn't change to the new number.

Any help is appreciated...


Here is the functions I am working on with the whole code below...

  1. int main()
  2. {
  3.  
  4. nodeType *first, *last;
  5. int num;
  6. int searchCount;
  7.  
  8. createList(first,last);
  9. printList(first);
  10.  
  11. searchCount = searchNum(first);
  12.  
  13. cout<<endl;
  14. cout<<"'1' occurred "<<searchCount<<endl;
  15.  
  16. insertBack(last);
  17. printList(first);
  18.  
  19. deleteFront(first);
  20. printList(first);
  21.  
  22. modifyNum(first);
  23. printList(first);
  24.  
  25.  
  26. system("PAUSE");
  27. return 0;
  28. }


  1. int searchNum(nodeType*& first)
  2. {
  3. int searchCount = 0;
  4. int num;
  5.  
  6. if (first->info == 1)
  7.  
  8. searchCount++;
  9.  
  10. else
  11.  
  12. cout<<"No number is present"<<endl;
  13.  
  14. return searchCount;
  15.  
  16. }
  17.  
  18. int modifyNum (nodeType*& first)
  19. {
  20.  
  21. int num;
  22. int Newnum;
  23.  
  24. cout<<"What number do you like to change?"<<endl;
  25. cin>>num;
  26. cout<<"What do you want to change it to?"<<endl;
  27. cin>>Newnum;
  28.  
  29. if ( Newnum != num)
  30.  
  31. Newnum = num;
  32.  
  33.  
  34. }



Here is the full program:



  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct nodeType
  5. {
  6. int info;
  7. nodeType *link;
  8. };
  9.  
  10. void createList(nodeType*& first, nodeType*& last);
  11. void printList(nodeType*& first);
  12. void insertBack(nodeType*& last);
  13. void deleteFront(nodeType*& first);
  14. int searchNum(nodeType*& first);
  15. int modifyNum (nodeType*& first);
  16.  
  17. int main()
  18. {
  19.  
  20. nodeType *first, *last;
  21. int num;
  22. int searchCount;
  23.  
  24. createList(first,last);
  25. printList(first);
  26.  
  27. searchCount = searchNum(first);
  28.  
  29. cout<<endl;
  30. cout<<"'1' occurred "<<searchCount<<endl;
  31.  
  32. insertBack(last);
  33. printList(first);
  34.  
  35. deleteFront(first);
  36. printList(first);
  37.  
  38. modifyNum(first);
  39. printList(first);
  40.  
  41.  
  42. system("PAUSE");
  43. return 0;
  44. }
  45.  
  46. void createList(nodeType*& first, nodeType*& last)
  47. {
  48. int number;
  49. int emptyList;
  50. nodeType *newNode;
  51. int counter = 0;
  52.  
  53. first = NULL;
  54. last = NULL;
  55.  
  56.  
  57. cout<<"Enter an integer (-999 to stop): ";
  58. cin>>number;
  59. cout<<endl;
  60.  
  61. while (number != -999)
  62. {
  63. newNode = new nodeType; // create new node
  64. newNode->info = number;
  65. newNode->link = NULL;
  66.  
  67. if (first == NULL)
  68. {
  69. first = newNode;
  70. last = newNode;
  71. }
  72. else
  73. {
  74. last->link = newNode;
  75. last = newNode;
  76. }
  77. cout<<"Enter an integer (-999 to stop): ";
  78. cin>>number;
  79. cout<<endl;
  80. counter++;
  81. }
  82.  
  83. if (first == NULL)
  84. cout<<"Empty list"<<endl;
  85. else
  86. cout<<"There are "<<counter<<" items in the linked list"<<endl;
  87.  
  88. }
  89.  
  90. void printList(nodeType*& first)
  91. {
  92.  
  93. cout<<endl;
  94. cout<<"Inside printList...printing linked list...\n"<<endl;
  95. nodeType *current;
  96. current = new nodeType;
  97. current = first;
  98. while (current != NULL)
  99. {
  100. cout << current->info<<endl;
  101. current = current->link;
  102. }
  103. }
  104.  
  105. void insertBack(nodeType*& last)
  106. {
  107.  
  108. int num;
  109. int searchCount = 0;
  110. nodeType *first,*newNode;
  111.  
  112.  
  113. cout<<endl;
  114. cout<<"Enter a number to add to the END of the list: "<<endl;
  115. cin>>num;
  116.  
  117.  
  118. newNode = new nodeType;
  119. newNode->info = num;
  120. newNode->link = NULL;
  121.  
  122. if (first == NULL)
  123. {
  124. first = newNode;
  125. last = newNode;
  126.  
  127. }
  128. else
  129. {
  130. last->link = newNode;
  131. last = newNode;
  132. }
  133.  
  134.  
  135. }
  136.  
  137. void deleteFront(nodeType*& first)
  138. {
  139.  
  140. nodeType *last,*current,*trailcurrent;
  141. bool found;
  142. int num;
  143. int searchCount = 0;
  144.  
  145.  
  146. cout<<endl;
  147. cout<<"Inside deleteFront...removing item from front of list..."<<endl;
  148. cout<<endl;
  149.  
  150. if (first->info == 1)
  151. {
  152. current = first;
  153. first = first->link;
  154. }
  155. if (first == NULL)
  156. {
  157. last = NULL;
  158.  
  159. delete current;
  160. }
  161. else
  162. {
  163. found = false;
  164. trailcurrent = first;
  165.  
  166. current = first->link;
  167. }
  168.  
  169. }
  170.  
  171. int searchNum(nodeType*& first)
  172. {
  173. int searchCount = 0;
  174. int num;
  175.  
  176. if (first->info == 1)
  177.  
  178. searchCount++;
  179.  
  180. else
  181.  
  182. cout<<"No number is present"<<endl;
  183.  
  184. return searchCount;
  185.  
  186. }
  187.  
  188. int modifyNum (nodeType*& first)
  189. {
  190.  
  191. int num;
  192. int Newnum;
  193.  
  194. cout<<"What number do you like to change?"<<endl;
  195. cin>>num;
  196. cout<<"What do you want to change it to?"<<endl;
  197. cin>>Newnum;
  198.  
  199. if ( Newnum != num)
  200.  
  201. Newnum = num;
  202.  
  203.  
  204. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked List: Search and Modify Help

 
0
  #2
Nov 13th, 2008
You should loop through the list from within the function, checking the value of each node using an if statement as you get to it, not just use an if statement without a loop.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Linked List: Search and Modify Help

 
0
  #3
Nov 13th, 2008
How do I check the value of each node using a for loop?


I originally had the brackets in for the for loop but it didnt make much of a difference as the number of counts were still off if you are wondering why i don't have brackets in there.

This is what i have thus far... (my full code is in my first post)



  1. int searchNum(nodeType*& first)
  2. {
  3. int searchCount = 0;
  4. int num;
  5.  
  6. for (int i = 0; i < 3; i++)
  7.  
  8. if (first->info == 1)
  9.  
  10. searchCount++;
  11. cout<<"'1' occurred "<<searchCount<<endl;
  12.  
  13. if (first->info != 1)
  14.  
  15. cout<<"No number is present"<<endl;
  16.  
  17.  
  18. cout<<endl;
  19.  
  20. }
Last edited by NinjaLink; Nov 13th, 2008 at 2:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked List: Search and Modify Help

 
0
  #4
Nov 13th, 2008
Why not use a while loop like you do in printList()? Changing the body of the loop from doing an ouptput statement to checking the equality of the node versus a given value using an if statement should work fine. If you must use a for loop it could look like:

for(ptr == start of list; ptr not null; advance ptr)

Remember, for loops need to have {} around all statement you want in the body of the loop, otherewise only the first statement after the declaration of the loop will be in the body of the loop.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Linked List: Search and Modify Help

 
0
  #5
Nov 13th, 2008
Am I able to use the while loop inside the print function to search the item and print out the occurrences or should i use the while loop in my current function "searchItem" to do it?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked List: Search and Modify Help

 
0
  #6
Nov 13th, 2008
Use a loop modeled after the one in printList() in the search function.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Linked List: Search and Modify Help

 
0
  #7
Nov 13th, 2008
Is this the correct way? How do I stop the infinite loop?


  1. int searchNum(nodeType*& first)
  2. {
  3. int searchCount = 0;
  4. int number;
  5. nodeType *current;
  6.  
  7. while (number != -999)
  8. {
  9.  
  10. if (first->info == 1)
  11. {
  12. searchCount++;
  13. cout<<"'1' occurred "<<searchCount<<endl;
  14. }
  15. if (first->info != 1)
  16. {
  17. cout<<"No number is present"<<endl;
  18. }
  19. }
  20.  
  21. cout<<endl;
  22.  
  23. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked List: Search and Modify Help

 
0
  #8
Nov 13th, 2008
How did you stop the loop in printList()? Use that technique again. How did you move from one node to the next in printList()? Use that technique again. Did you write printList()?------on second thought, don't answer that one!

I would only output the no number found message if you've looke the whole list through and didn't find the number you're looking for. You will probably want to maintain a variable to keep track of whether you found the number while you loop through the list and only output the no number found message if the variable indicates no number found.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Linked List: Search and Modify Help

 
0
  #9
Nov 13th, 2008
Thanks..I got the first part now...This is my code for it:


  1. int searchNum(nodeType*& first)
  2. {
  3. int searchCount = 0;
  4. int number;
  5. bool found = false;
  6. nodeType *current;
  7.  
  8. current = first;
  9.  
  10. while (current != NULL)
  11. {
  12. if (current->info == 1 && !found)
  13. {
  14. found = true;
  15. searchCount++;
  16. cout<<endl;
  17. cout<<"'1' occurred "<<searchCount<<endl;
  18. }
  19. current = current->link;
  20. }
  21. if (!found)
  22. {
  23. cout<<endl;
  24. cout<<"Number is not found"<<endl;
  25. }
  26.  
  27. cout<<endl;
  28.  
  29. }


Now for the 2nd part, I have to "change each occurrence of a #, to the new number or display 'number is not present' " ... I'm not to sure about you, but I really don't understand this step. What is this step asking me to do?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked List: Search and Modify Help

 
0
  #10
Nov 13th, 2008
Job well done! Might work a little on the consistent indenting to make it easier to see mismatched {}s down the road, but then that might the board trolls doing their thing, too.

Second part boils down to obtaining the value that should be replaced and the replacement value. Then search the list for the value to be replaced using a loop. If you find the value to replace, rather than printing it or incrementing a counter, change the value to the replacement value. You've written themost of this function already (see our last 8-10 posts) so it shouldn't be too hard to tweak to the process to get it to do this variation. The overarching point of this exercise is to give you practice writing loops and thereby give you an appreciation for their usefulness.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC