Hello.
I am currently having problems. I am asking a user to input the position and number of where they would like to insert into the Linked List. After the user input the information, the number does not appear in the Linked List, but everything else does. Can someone help me fix this problem?
I am asking the user:
cout<<"Enter a position and number to add to Link List"<<endl;
cin>>index;
cin>>number;
in the createList function.
Here is my full program: (Please ignore the functions that are commented out)
#include <iostream>
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
void createList (nodeType*& first, nodeType*& last);
void deleteFront (nodeType*& first,nodeType*& last);
nodeType *GetNth (nodeType *first, int index);
void insertBack (nodeType*& first, nodeType*& last, int info);
void InsertNth (nodeType*& first, nodeType*& last,
int index, int info);
int modifyNum (nodeType*& first);
void printList (nodeType*& first);
int searchNum (nodeType*& first, int info);
int main()
{
nodeType *first, *last;
int index;
int info;
createList(first,last);
printList(first);
GetNth(first,index);
InsertNth(first,last,index,info);
// printList(first);
searchNum(first,info);
modifyNum(first);
// printList(first);
insertBack(first,last,info);
// printList(first);
deleteFront(first,last);
// printList(first);
system("PAUSE");
return 0;
}
void createList(nodeType*& first, nodeType*& last)
{
int number;
nodeType *newNode;
int counter = 0;
int index = 0;
first = NULL;
last = NULL;
cout<<"Enter an integer (-999 to stop): ";
cin>>number;
cout<<endl;
while (number != -999)
{
InsertNth (first, last, counter++, number);
if (counter == 1)
{
last = first;
}
cout<<"Enter an integer (-999 to stop): ";
cin>>number;
cout<<endl;
}
cout<<"Enter a position and number to add to Link List"<<endl;
cin>>index;
cin>>number;
if (first == NULL)
cout<<"Empty list"<<endl;
else
cout<<"There are "<<counter<<" items in the linked list"<<endl;
}
void deleteFront (nodeType*& first,nodeType*& last)
{
//if (first != NULL)
//{
//nodeType* next = first;
//first = first->link;
//if (last == next)
//{
//last = NULL;
//}
//delete (next);
//}
}
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;
}
}
nodeType *GetNth(nodeType *first,int index)
{
nodeType *current = first;
while ((current != NULL) && (index > 0))
{
current = current->link;
index--;
}
return (current);
}
void insertBack (nodeType*& first, nodeType*& last, int info)
{
//nodeType *newNode = new nodeType; // create new node
//newNode->info = info;
//newNode->link = NULL;
//if (last == NULL)
//{
//first = last = newNode;
//}
//else
//{
//last->link = newNode;
//}
}
void InsertNth(nodeType*& first,nodeType*& last,int index, int info)
{
nodeType *newNode = new nodeType; // create new node
newNode->info = info;
newNode->link = NULL;
int number;
if (first == NULL)
{
first = newNode;
}
else
{
nodeType *next = first;
while ((next->link != NULL) && (index > 0))
{
next = next->link;
index--;
}
newNode->link = next->link;
next->link = newNode;
if (newNode->link == NULL)
{
last = newNode;
}
}
}
void printList (nodeType*& first)
{
nodeType *next = first;
int index = 0;
while (next != NULL)
{
cout<<endl;
cout << "Position: " << index << ", Number = " << next->info << endl;
index++;
next = next->link;
}
cout << endl << endl;
}
int searchNum (nodeType*& first, int info)
{
int index = 0;
nodeType *next = first;
while ((next != NULL) && (next->info != info))
{
next = next->link;
index++;
}
if (next->info != info)
{
index = -1;
}
return (index);
}