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

try this code.

/* Program to implement operations on singly linked list */

#include<stdio.h>
#include<conio.h>
    /* declare structure for a node of singly linked list */
struct node
{
    int info;
    struct node *next;
}*sll,*start;

void insert();
void display();
void delet();
void search();

main()
{
    int ch;
    start=NULL;
    clrscr();
    do
    {
    printf("\n Menu: ");
    printf("\n 1. Insert \t 2. Delete \t 3. Display \t 4. Search \t 5. Exit");
    printf("\n Enter your choice");
    scanf("%d",&ch);
    switch(ch)
      {
        case 1: printf("\n\t\t Insert a node at any position");
            insert();
        break;
        case 2: printf("\n\t\t Delete a node");
            delet();
        break;
        case 3: printf("\n\t\t Display the list");
            display();
        break;
        case 4: printf("\n\t\t Search for a node");
            search();
        break;
        case 5: printf("\n\t\t Exiting");
            exit(1);
        }         /* end of switch */
    }while(ch!=5); /* end of do-while*/
}
void insert()
{
    struct node *temp,*r,*s;
    int inf;
    temp=malloc(sizeof(struct node *)); /* Allocating memory for new node*/
    temp->next=NULL;
    printf("\n Enter information");
    scanf("%d",&temp->info);
    if(start==NULL)                     /* if this is first node*/
    {
        start=temp;
    }
    else                                /* second node & so on */
    {
        printf("\n After which item you want to insert");
        scanf("%d",&inf);
        r=start;
        while(r->info!=inf)
        {
            r=r->next;
        }
        s=r->next;
        r->next=temp;
        temp->next=s;
    }
}

void display()
{
    struct node *temp;
    temp=start;
    if(temp==NULL) /* check whether list is empty*/
    {
        printf("\n Linked list is empty");
        return;
    }
    else
    {
        do
        {
            printf("\n %d",temp->info);
            temp=temp->next;
        }while(temp!=NULL);
    }
}
void delet()
{
    struct node *temp,*r;
    int inf;
    printf("\n Which item you want to delete");
    scanf("%d",&inf);
    temp=start;
    if(temp==NULL)       /* list is empty*/
    {
        printf("\n Linked list is empty");
        return;
    }
    else
    {
        while(temp->info!=inf)
        {
            temp=temp->next;
        }
        if(temp->next==NULL)  /* if this is last node*/
        {
            printf("\n Deleted node is %d",temp->info);
            r=start;
            while(r->next!=temp)
            {
                r=r->next;
            }
            r->next=NULL;
        }
        else if(temp==start)  /* if this is first node*/
        {
            printf("\n Deleted node is %d",temp->info);
            start=start->next;
        }
        else              /* if this is some middle node*/
        {
            printf("\n Deleted node is %d",temp->info);
            r=start;
            while(r->next!=temp)
            {
                r=r->next;
            }
            r->next=temp->next;
         }
         free(temp);
     }
}

void search()
{
    int inf;
    struct node *temp;
    printf("\n Enter the item which you want to search");
    scanf("%d",&inf);
    temp=start;
    do
    {
        if(temp->info==inf)
        {
            printf("\n Item found");
            return;
        }
        else
        {
            temp=temp->next;
        }
    }while(temp!=NULL);
    printf("\n Item not found");
}
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.