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