Hello.
I'm having problems in 2 different functions: GetNth() and InsertNth()
1) For my GetNth() function, I want to take my linked list and an integer index and return the data value stored in the node at that index position. For example: {42,13,66}, the index of 1 should print out 13 from my Linked List.
2) For my InsertNth() function, I want to write a function that can insert a new node at any index within a list. The caller may specify any index in the range [0...length], and the new node should be inserted so as to be at that index.
For example:
InsertNth(&first,0,13); // after, list is {13}
InsertNth(&first,1,42); // after, list is {13, 42}
InsertNth(&first,1,5); // after, list is {13, 5, 42}
Please help!
Here is my 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 GetNth(nodeType*& first);
void InsertNth(nodeType*& first);
int main()
{
nodeType *first, *last;
int num;
int searchCount;
int index = 2;
createList(first,last);
printList(first);
GetNth(first);
current->info = InsertNth(first,2);
searchNum(first);
modifyNum(first);
printList(first);
insertBack(last);
printList(first);
deleteFront(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<<"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;
}
}
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;
}
}
int GetNth(nodeType*& first)
{
int searchCount = 0;
int number;
int index = 2;
bool found = false;
nodeType *current;
current = first;
while (current != NULL)
{
if (current->info == number[index])
{
found = true;
searchCount++;
cout<<endl;
}
current = current->link;
}
return current->info;
}
void InsertNth(nodeType*& first)
{
int index = 0;
int searchCount = 0;
bool found = false;
nodeType *current,*newNode,*next;
current = first;
while (current != NULL)
{
newNode = (n-1)->next
(n-1)->next = newNode;
if (current->info == index)
{
found = true;
cout<<endl;
}
current = current->link;
}
}