| | |
Linked List: Search and Modify Help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 370
Reputation:
Solved Threads: 0
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...
Here is the full program:
- 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...
C++ Syntax (Toggle Plain Text)
int main() { nodeType *first, *last; int num; int searchCount; createList(first,last); printList(first); searchCount = searchNum(first); cout<<endl; cout<<"'1' occurred "<<searchCount<<endl; insertBack(last); printList(first); deleteFront(first); printList(first); modifyNum(first); printList(first); system("PAUSE"); return 0; }
C++ Syntax (Toggle Plain Text)
int searchNum(nodeType*& first) { int searchCount = 0; int num; if (first->info == 1) searchCount++; else cout<<"No number is present"<<endl; return searchCount; } int modifyNum (nodeType*& first) { int num; int Newnum; cout<<"What number do you like to change?"<<endl; cin>>num; cout<<"What do you want to change it to?"<<endl; cin>>Newnum; if ( Newnum != num) Newnum = num; }
Here is the full program:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; struct nodeType { int info; nodeType *link; }; void createList(nodeType*& first, nodeType*& last); void printList(nodeType*& first); void insertBack(nodeType*& last); void deleteFront(nodeType*& first); int searchNum(nodeType*& first); int modifyNum (nodeType*& first); int main() { nodeType *first, *last; int num; int searchCount; createList(first,last); printList(first); searchCount = searchNum(first); cout<<endl; cout<<"'1' occurred "<<searchCount<<endl; insertBack(last); printList(first); deleteFront(first); printList(first); modifyNum(first); printList(first); system("PAUSE"); return 0; } void createList(nodeType*& first, nodeType*& last) { int number; int emptyList; nodeType *newNode; int counter = 0; first = NULL; last = NULL; cout<<"Enter an integer (-999 to stop): "; cin>>number; cout<<endl; while (number != -999) { newNode = new nodeType; // create new node newNode->info = number; newNode->link = NULL; if (first == NULL) { first = newNode; last = newNode; } else { last->link = newNode; last = newNode; } cout<<"Enter an integer (-999 to stop): "; cin>>number; cout<<endl; counter++; } if (first == NULL) cout<<"Empty list"<<endl; else cout<<"There are "<<counter<<" items in the linked list"<<endl; } void printList(nodeType*& first) { cout<<endl; cout<<"Inside printList...printing linked list...\n"<<endl; nodeType *current; current = new nodeType; current = first; while (current != NULL) { cout << current->info<<endl; current = current->link; } } void insertBack(nodeType*& last) { int num; int searchCount = 0; nodeType *first,*newNode; cout<<endl; cout<<"Enter a number to add to the END of the list: "<<endl; cin>>num; newNode = new nodeType; newNode->info = num; newNode->link = NULL; if (first == NULL) { first = newNode; last = newNode; } else { last->link = newNode; last = newNode; } } void deleteFront(nodeType*& first) { nodeType *last,*current,*trailcurrent; bool found; int num; int searchCount = 0; cout<<endl; cout<<"Inside deleteFront...removing item from front of list..."<<endl; cout<<endl; if (first->info == 1) { current = first; first = first->link; } if (first == NULL) { last = NULL; delete current; } else { found = false; trailcurrent = first; current = first->link; } } int searchNum(nodeType*& first) { int searchCount = 0; int num; if (first->info == 1) searchCount++; else cout<<"No number is present"<<endl; return searchCount; } int modifyNum (nodeType*& first) { int num; int Newnum; cout<<"What number do you like to change?"<<endl; cin>>num; cout<<"What do you want to change it to?"<<endl; cin>>Newnum; if ( Newnum != num) Newnum = num; }
•
•
Join Date: Mar 2008
Posts: 370
Reputation:
Solved Threads: 0
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)
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)
C++ Syntax (Toggle Plain Text)
int searchNum(nodeType*& first) { int searchCount = 0; int num; for (int i = 0; i < 3; i++) if (first->info == 1) searchCount++; cout<<"'1' occurred "<<searchCount<<endl; if (first->info != 1) cout<<"No number is present"<<endl; cout<<endl; }
Last edited by NinjaLink; Nov 13th, 2008 at 2:00 pm.
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
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.
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
•
•
Join Date: Mar 2008
Posts: 370
Reputation:
Solved Threads: 0
Is this the correct way? How do I stop the infinite loop?
C++ Syntax (Toggle Plain Text)
int searchNum(nodeType*& first) { int searchCount = 0; int number; nodeType *current; while (number != -999) { if (first->info == 1) { searchCount++; cout<<"'1' occurred "<<searchCount<<endl; } if (first->info != 1) { cout<<"No number is present"<<endl; } } cout<<endl; }
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
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.
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
•
•
Join Date: Mar 2008
Posts: 370
Reputation:
Solved Threads: 0
Thanks..I got the first part now...This is my code for it:
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?
C++ Syntax (Toggle Plain Text)
int searchNum(nodeType*& first) { int searchCount = 0; int number; bool found = false; nodeType *current; current = first; while (current != NULL) { if (current->info == 1 && !found) { found = true; searchCount++; cout<<endl; cout<<"'1' occurred "<<searchCount<<endl; } current = current->link; } if (!found) { cout<<endl; cout<<"Number is not found"<<endl; } cout<<endl; }
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?
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
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.
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
![]() |
Similar Threads
- Checking for duplicates in a orderedered linked list (C++)
- Pascal database check (Pascal and Delphi)
Other Threads in the C++ Forum
- Previous Thread: deleting allocated area
- Next Thread: Round Up Round down(truncate)
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






