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...

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;
}
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:

#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;
  
    
}

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.

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)

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;
 
}

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.

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?

Use a loop modeled after the one in printList() in the search function.

Is this the correct way? How do I stop the infinite loop?

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;
 
}

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.

Thanks..I got the first part now...This is my code for it:

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?

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.

I'm still trying to understand. Does this mean I'm taking a number from my Linked List and replacing it with a different number...then I will get the count for the new number?

To me you are to write two different functions. One to count the number of instances of a given value. The other will replace each instance of a given number with a replacement number. That is if the list were:

125315155
and you were to count the number of 1s then one function would output 3.

If you were asked to replace all instances of 5 with 8s then the list would look like:

128318188

if you were to print the list after running the second function.

If you want to count the number of times you replace one value with another in the same function, that's cool. If you want to count the number of instances in one function and replace them in another, that's cool too. To me it's no big deal. You'll have to decide how your instructor wants you to do it.

Well, he didn't give instructions for it. We just get a paper that give us directions on it. I think I should use 1 function for it. I already have the function called modifyNum function. I'm going to work on it and then reply back to you. Thanks for your help so far.

what is the code to replace a number with another number? I'm having trouble with it. Is this correct so far? I need help!

int modifyNum (nodeType*& first)
{
    
   int searchCount = 0;   
   int number;
   bool found = false;
   int num = 2;
   int Newnum = 4;
   nodeType *current;
   
   current = first;
   
   while (current != NULL)
   {         
    if (current->info == num && !found)
    {
    found = true;
    Newnum = num;
    cout<<endl; 
    }     
    current = current->link; 
     }
    if (!found)
    {
    cout<<endl;
    cout<<"Number is not found"<<endl;
    }
    
   cout<<endl; 
  
    
}

Can you help me? How do I change a number..lets say "1" into another number in my Linked List?

here is my function w/ full program below:

int modifyNum (nodeType*& first)
{
    
   int searchCount = 0;   
   bool found = false;
   int num = 1;
   int Newnum = 4;
   nodeType *current;
   
   current = first;
   
   cout<<"The new list is now.."<<endl;
   
   while (current != NULL)
   {         
    if (current->info == num)
    {
    found = true;
    Newnum = num;
    cout<<endl; 
    }     
    cout << current->info<<endl;
    current = current->link; 
     }
    
}

Full Program:

#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);
    
    searchNum(first);
    
    
    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;

  
  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 number;
   bool found = false;
   nodeType *current;
   
   current = first;
   
   while (current != NULL)
   {         
    if (current->info == 1)
    {
    found = true;
    searchCount++;
    cout<<endl; 
    }      
    current = current->link;
   }
   
   cout<<"'1' occurred "<<searchCount<<endl; 
    
    if (!found)
    {
    cout<<endl;
    cout<<"Number is not found"<<endl;
    }
    
   cout<<endl; 
 
}

int modifyNum (nodeType*& first)
{
    
   int searchCount = 0;   
   bool found = false;
   int num = 1;
   int Newnum = 4;
   nodeType *current;
   
   current = first;
   
   cout<<"The new list is now.."<<endl;
   
   while (current != NULL)
   {         
    if (current->info == num)
    {
    found = true;
    Newnum = num;
    cout<<endl; 
    }     
    cout << current->info<<endl;
    current = current->link; 
     }
    
}

Anyone know the code to replace a number in the Linked list with another number?

For example:

12321431

the 1's become 5

5232535

Any help is appreciate...

if (current->info == num)
Newnum = num;

Substitute numberToReplace and ReplacementNumber for num and Newnum respectively and then write out in your native language what the above two lines mean. I think that will show you the problem.

if (current->info == num)
Newnum = num;

Substitute numberToReplace and ReplacementNumber for num and Newnum respectively and then write out in your native language what the above two lines mean. I think that will show you the problem.

Am I on the right track?

The thing is...the number doesn't change even if i changed the value of num. That is weird.

how come the value of num doesn't change in the linked list when i replace it with a new number?

Show an example of what you mean. This will probably get you more responses then bumping your thread every 5 minutes.

Nevermind I figured it out.... Thanks Lerner! =D


Here is my modifyNum function:

int modifyNum (nodeType*& first)
{
    
   int searchCount = 0;   
   int num = 1;
   int Newnum = 4;
   bool found = false;
   nodeType *current;
   
   current = first;
   
   while (current != NULL)
   {         
    if (current->info == num)
    {
    current->info = 5;
    cout<<endl;
    } 
   current = current->link; 
    
    }

   
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.