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

Well, but WHAT are your problems?

Is my code for both of the functions correct so far?

Also, when I run the program and get up to the function, my program will give an error and I have to close out of it.

OK, but it's not so good-looking manner (to some extent ) to present this C-like code which I (and anybody else) can't compile w/o numerous errors (mostly undeclared identifiers).

OK, but it's not so good-looking manner (to some extent ) to present this C-like code which I (and anybody else) can't compile w/o numerous errors (mostly undeclared identifiers).

I'm currently stuck with no where to go, so I'm gonig to try my best to explain the problems I am having.

My current result of my program is that my program runs but when it gets to my GetNth functions, my program crashes. It doesn't even get to the InsertNth function, so I can't really analyze it!

1)
I'm going to start with this function first (GetNth), so I can clarify things. For this funtion, I am trying to find the position of a number inside the Linked List and then return the value of that number.

For example: { 3, 4, 5, 4}

I want to find the number in position 2

the number will be 5.


Right now, I am returning current->info in the GetNth function. I'm unsure if I am suppose to be returning current->info because when I try:

current->info = GetNth(first,2) inside Main

I receive an error and says current is not declared. I'm just currently stuck on the function, and I need someone to guide me out.

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}


I wrote code inside of my InsertNth function, but since my program crashes before I can even get to the function, I want to know if I did everything correctly. If I didn't, what should I add? I'm not sure if I'm suppose to add a for-loop in a Linked List. Also, do I need to use a cout statement to ask the user what position and number he/she wants to create the list with under this function? I know it's a lot of questions, but my book is not giving good examples to understand stand how to solve my problem.


Here are the 2 functions and full program below:

GetNth function:

int GetNth(nodeType*& first,int index)
{
    
   int searchCount = 0;   
   int number;
   bool found = false;
   nodeType *current;
   
   current = first;
   
   while (current != NULL)
   {         
    if (current->info == index)
    {
    found = true;
    searchCount++;
    cout<<endl; 
    }      
    current = current->link;
   }
     return current->info;             
}

InsertNth function:

void InsertNth(nodeType*& first)
{
   int index = 0; 
   int searchCount = 3;   
   bool found = false;
   nodeType *current,*newNode,*next;
   
   current = first;
   
   while (current != NULL)
   {        
   newNode = new nodeType;
   newNode->info = 5;  
    if (current->info == index)
    {
    found = true;
    cout<<endl; 
    }      
    current = current->link;
    }
  
}

Create List function: and Main

#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,int index);
void InsertNth(nodeType*& first);

int main()
{
    
    nodeType *first, *last;

    createList(first,last);
    printList(first);
    
    
    GetNth(first,index);
    InsertNth(first);
                              
    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 index)
{
    
   int searchCount = 0;   
   int number;
   bool found = false;
   nodeType *current;
   
   current = first;
   
   while (current != NULL)
   {         
    if (current->info == index)
    {
    found = true;
    searchCount++;
    cout<<endl; 
    }      
    current = current->link;
   }
     return current->info;             
}

void InsertNth(nodeType*& first)
{
   int index = 0; 
   int searchCount = 3;   
   bool found = false;
   nodeType *current,*newNode,*next;
   
   current = first;
   
   while (current != NULL)
   {        
   newNode = new nodeType;
   newNode->info = 5;  
    if (current->info == index)
    {
    found = true;
    cout<<endl; 
    }      
    current = current->link;
    }
  
}
int main()
...
    GetNth(first,index); // line #28: undeclared name index

Is it a subtle insult or a total disorder?..

1) In your current version of GetNth() what role do the variables number and searchCount play? If you're not going to use them, then delete them.

2) You declare GetNth() to have return type int but on line 28 when you call the function you ignore the return value. If you are going to ignore the return value you might as well make the return type void.

3) Is the node member variable called info the distance from first or the information stored in the node.

4) >>The caller may specify any index in the range [0...length],

How does the caller know what length is?

5) This is the stated goal:
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}

and this is the prototype:

void InsertNth(nodeType*& first);

What happened to the other information InsertNth was going to get in the goal statement?

6) I think you need to comment your code or write out long hand on a sheet of paper what the function is going to do, what information the function needs to do what it is supposed to do, where it's going to get the information it needs to do what it is supposed to do. What the calling function is expecting back from the called function, and in what form is the calling function going to get that information back.

1) In your current version of GetNth() what role do the variables number and searchCount play? If you're not going to use them, then delete them.

2) You declare GetNth() to have return type int but on line 28 when you call the function you ignore the return value. If you are going to ignore the return value you might as well make the return type void.

3) Is the node member variable called info the distance from first or the information stored in the node.

4) >>The caller may specify any index in the range [0...length],

How does the caller know what length is?

5) This is the stated goal:
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}

and this is the prototype:

void InsertNth(nodeType*& first);

What happened to the other information InsertNth was going to get in the goal statement?

6) I think you need to comment your code or write out long hand on a sheet of paper what the function is going to do, what information the function needs to do what it is supposed to do, where it's going to get the information it needs to do what it is supposed to do. What the calling function is expecting back from the called function, and in what form is the calling function going to get that information back.

Lerner! Thanks for replying to my post.

1) I guess none of them play a role for this function. Deleted = done.

2) I think I want to return the position of the value? I'm unsure if that is how it suppose to go.

3) info is the information stored in the node.

4) length of Linked List is the count that used in my CreateList function which is the function after main. It works pretty good.

5) Do I need to pass index and number in the function InsertNth?

6) I have weak skills in C++, and I'm trying to get better, but my sources suck lol.

I'm still stuck with no where to go. Can someone please help me???

My current result of my program is that my program runs but when it gets to my GetNth functions, my program crashes. It doesn't even get to the InsertNth function, so I can't really analyze it!

1)
I'm going to start with this function first (GetNth), so I can clarify things. For this funtion, I am trying to find the position of a number inside the Linked List and then return the value of that number.

For example: { 3, 4, 5, 4}

I want to find the number in position 2

the number will be 5.


Right now, I am returning current->info in the GetNth function. I'm unsure if I am suppose to be returning current->info because when I try:

current->info = GetNth(first,2) inside Main

I receive an error and says current is not declared. I'm just currently stuck on the function, and I need someone to guide me out.

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}


I wrote code inside of my InsertNth function, but since my program crashes before I can even get to the function, I want to know if I did everything correctly. If I didn't, what should I add? I'm not sure if I'm suppose to add a for-loop in a Linked List. Also, do I need to use a cout statement to ask the user what position and number he/she wants to create the list with under this function? I know it's a lot of questions, but my book is not giving good examples to understand stand how to solve my problem.


Please tell me what I am doing wrong in my program and guide me to success...


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,int index);
void InsertNth(nodeType*& first, int index, int length);

int main()
{
    
    nodeType *first, *last,*current;
    int num;
    int searchCount;
    int index = 2;

    createList(first,last);
    printList(first);
    
    
    GetNth(first,index);
    InsertNth(first,index,length);
                              
    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 index)
{
    
   int searchCount = 0;   
   int number;
   bool found = false;
   nodeType *current;
   
   current = first;
   
   while (current != NULL)
   {         
    if (current->info == index)
    {
    found = true;
    searchCount++;
    cout<<endl; 
    }      
    current = current->link;
   }
     return current->info;             
}

void InsertNth(nodeType*& first,int index, int length)
{
        
   bool found = false;
   nodeType *current,*newNode,*next;
   
   index = 0;
   length = 4;
   current = first;
   
   while (current != NULL)
   {        
   newNode = new nodeType;
   newNode->info = 5;  
    if (current->info == index)
    {
    found = true;
    cout<<endl; 
    }      
    current = current->link;
    }
  
}
This question has already been answered. Start a new discussion instead.