Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// A program to demonstrate a linked list #include <iostream> #include<cstdlib>

ID: 3760435 • Letter: #

Question

// A program to demonstrate a linked list
#include <iostream>
#include<cstdlib>
using namespace std;

// Function prototypes
void buildlist(void);
void insertnode(void);
void displaylist(void);
void freelist(void);

// Declare a structure for the list
struct listnode
{
   int value;
   struct listnode *next;
};

// Declare pointers for the list
listnode *head, *prev, *current;

int main(void)
{
buildlist();
insertnode();
displaylist();
freelist();
}
void buildlist(void)
{
int input;
cout<<"enter a set of integer values"<<endl;
cout<<"when finished, enter 0 " <<endl;
do{
   cin>>input;
   if(input)
   {
       current=new listnode;
       if (current==NULL)
       {
       cout<<"Error allocating memory"<<endl;
       exit(0);
       }
       current->value=input;
       if(prev==NULL)
       head=current;
       else prev->next=current;
       current->next=NULL;
       prev=current;
}
} while(input);
} // buildlist
// starting
void insertnode(void)
{
insert93 =new listnode;
insert93->value = 93;
insert93->next = NULL;

cout<<endl<<endl;

current=head;
while(current->value !=99)
{
current=current->next;
}
insert93->next = current->next;
current->next = insert93;
} // displaylis

void displaylist(void)
{
cout<<endl<<endl;
cout<<"Entered data:"<<endl<<endl;
current=head;
while(current)
{
cout<<current->value<<endl;
current=current->next;
}
} // displaylist

void freelist(void)
{
struct listnode *temp;
current=head;
while(current)
{
temp=current->next;
delete current;
current=temp;
}

} //freelist

I want do insertion but I coudn't

Explanation / Answer

Hi,

Below is the solution to your problem:

Your insertion function is not right.

// A program to demonstrate a linked list
#include <iostream>
#include<cstdlib>
using namespace std;
// Function prototypes
void buildlist(void);
void displaylist(void);
void freelist(void);
void insertnode();
void insert_pos();
void insert_last();

// Declare a structure for the list
struct listnode
{
int value;
struct listnode *next;
};
// Declare pointers for the list
listnode *head, *prev, *current;
int main(void)
{
buildlist();
insertnode();
insert_pos();
insert_last();
displaylist();
freelist();
}
void buildlist(void)
{
int input;
cout<<"enter a set of integer values"<<endl;
cout<<"when finished, enter 0 " <<endl;
do{
cin>>input;
if(input)
{
current=new listnode;
if (current==NULL)
{
cout<<"Error allocating memory"<<endl;
exit(0);
}
current->value=input;
if(prev==NULL)
head=current;
else prev->next=current;
current->next=NULL;
prev=current;
}
} while(input);
} // buildlist

void insertnode()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp = listnode(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
/*
* Inserting Node at last
*/
void insert_last()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s;
temp =listnode(value);
s = start;
while (s->next != NULL)
{   
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last"<<endl;
}


*
* Insertion of node at a given position
*/
void insert_pos()
{
int value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = listnode(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}

// displaylis
void displaylist(void)
{
cout<<endl<<endl;
cout<<"Entered data:"<<endl<<endl;
current=head;
while(current)
{
cout<<current->value<<endl;
current=current->next;
}
} // displaylist
void freelist(void)
{
struct listnode *temp;
current=head;
while(current)
{
temp=current->next;
delete current;
current=temp;
}
} //freelist

Hope that helps...HAPPY ANSWERING!!!!!!!