please check the program and correct the delete option and memory leak.Thanks in advance.
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
int data; // will store information
node *next; // the reference to the next node
};
node *head = NULL; //empty linked list
int info = 0, node_number = 0, counter = 0;
int display();
int insertfirst()
{ cout<<"ENTER ANY NUMBER:";
cin>>info; //take input data
node *temp; //create temporary node
temp=(node*)malloc(sizeof(node));
temp->data = info; //store data in first field
temp->next=head; //store the address of the pointer head for second field
head = temp; //transfer the address of temp to head
counter++;
display();
}
int insertlast()
{
if(head==NULL)
{
cout<<"List is Empty";
}
else
{
cout<<"ENTER ANY NUMBER:";
cin>>info; // take input data
node *temp1; // create a temporary node
temp1 = head; // transfer the address of 'head' to 'temp1'
while(temp1->next!=NULL) // go to the last node
temp1 = temp1->next;//tranfer the address of 'temp1->next' to 'temp1'
node *temp; // create a temporary node
temp=(node*)malloc(sizeof(node));
temp->data = info; // store data(first field)
temp->next = NULL; // second field will be null(last node)
temp1->next = temp;
counter++;
display();
}
}
int insertpos()
{
if(head==NULL)
{
cout<<"List is empty";
}
cout<<"ENTER THE NODE NUMBER:";
cin>>node_number;
cout<<"ENTER ANY NUMBER:";
cin>>info;
node *temp1;
temp1 = head;
for( int i = 1 ; i < node_number ; i++ )
{
temp1 = temp1->next;
if( temp1 == NULL )
{
cout<<node_number<<" node is not exist"<< endl;
return 0;
}
}
node *temp;
temp = (node*)malloc(sizeof(node));
temp->data = info;
temp->next = temp1->next;
temp1->next = temp;
counter++;
display();
}
int insertreppos()
{
if(head==NULL)
{
cout<<"List is empty";
}
cout<<"ENTER THE NODE NUMBER:";
cin>>node_number;
cout<<"ENTER ANY NUMBER:";
cin>>info;
node *temp1;
temp1 = head;
for( int i = 1 ; i < node_number ; i++ )
{
temp1 = temp1->next;
if( temp1 == NULL )
{
cout<<node_number<<" node is not exist"<< endl;
return 0;
}
}
temp1->data = info;
display();
}
int delfirst()
{
if(head==NULL)
{
return 0;
}
else if(head->next==NULL)
{
head = NULL;
cout<<"The first node of the Linked List is deleted"<<endl;
counter--;
return 0;
}
node *temp;
temp = head;
head = temp->next;
free(temp);
cout<<"The first node of the Linked List is deleted"<<endl;
counter--;
display();
}
int dellast()
{
if(head==NULL)
{
display();
}
else if(head->next==NULL)
{
head = NULL;
cout<<"The last node of the Linked List is deleted"<<endl;
counter--;
display();
return 0;
}
else
{
node *temp1,*old_temp;
temp1 = head;
while(temp1->next!=NULL)
{
old_temp=temp1;
temp1= temp1->next;
}
old_temp->next=NULL;
free(temp1);
cout<<"The last node of the Link List is deleted : "<<endl;
counter--;
display();
}
int delpos()
{
if(head==NULL)
{
return 0;
}
else if(head ->next==NULL)
{
head = NULL;
cout<<"The last node of the Linked List is deleted"<<endl;
counter--;
display();
return 0;
}
cout<<"ENTER THE NODE NUMBER:";
cin>>node_number;
if(node_number > counter)
{
cout<<"No such node is exist";
return 0;
}
node *temp1;
temp1 = head;
for(int j=0;j<node_number;j++)
{
head=temp1->next;
if(j==node_number)
{
free(temp1);
counter--;
}
}
cout<<node_number<<" node of the Linked List is deleted"<<endl;
display();
}
int deletelist()
{
node *temp;
if(head==NULL)
return 0;
while(head!=NULL)
{
temp=head->next;
delete temp;
head=temp;
}
}
int display()
{
node *temp;
temp=head;
if(temp==NULL)
{
cout<<"List is empty";
return 0;
}
cout<<"\nThe elements are \n"<<endl;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
}
int main()
{
char ch;
do{
cout<<"\n\n";
cout<<"1.Insert at first\n";
cout<<"2.Insert at last\n";
cout<<"3.Insert after specified number of node\n";
cout<<"4.Delete at first node\n";
cout<<"5.Delete at last node\n";
cout<<"6.Delete specified number of node\n";
cout<<"7.Display\n";
cout<<"8.Replace specified number of node\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case '1': insertfirst();
break;
case '2': insertlast();
break;
case '3': insertpos();
break;
case '4': delfirst();
break;
case '5': dellast();
break;
case '6': delpos();
break;
case '7': display();
break;
case '8' : insertreppos();
break;
default : cout<<"Invalid option";
Your code is rather difficult to read due to the complete lack of whitespace, even after it is reformatted.
Pay attention to the error(s) your compiler reports. Fix them, then get back to us. Or, if you can't fix them, at least let us know what they are and what you've tried to fix them.
There is no point in worrying about a memory leak in code that won't even compile.
#include<iostream>
#include<stdlib.h>
usingnamespace std;
struct node
{
int data; // will store information
node *next; // the reference to the next node
};
node *head = NULL; //empty linked list
int info = 0, node_number = 0, counter = 0;
int display();
int insertfirst()
{ cout<<"ENTER ANY NUMBER:";
cin>>info; //take input data
node *temp; //create temporary node
temp=(node*)malloc(sizeof(node));
temp->data = info; //store data in first field
temp->next=head; //store the address of the pointer head for second field
head = temp; //transfer the address of temp to head
counter++;
display();
}
int insertlast()
{
if(head==NULL)
{
cout<<"List is Empty";
}
else
{
cout<<"ENTER ANY NUMBER:";
cin>>info; // take input data
node *temp1; // create a temporary node
temp1 = head; // transfer the address of 'head' to 'temp1'
while(temp1->next!=NULL) // go to the last node
temp1 = temp1->next;//tranfer the address of 'temp1->next' to 'temp1'
node *temp; // create a temporary node
temp=(node*)malloc(sizeof(node));
temp->data = info; // store data(first field)
temp->next = NULL; // second field will be null(last node)
temp1->next = temp;
counter++;
display();
}
}
int insertpos()
{
if(head==NULL)
{
cout<<"List is empty";
}
cout<<"ENTER THE NODE NUMBER:";
cin>>node_number;
cout<<"ENTER ANY NUMBER:";
cin>>info;
node *temp1;
temp1 = head;
for( int i = 1 ; i < node_number ; i++ )
{
temp1 = temp1->next;
if( temp1 == NULL )
{
cout<<node_number<<" node is not exist"<< endl;
return 0;
}
}
node *temp;
temp = (node*)malloc(sizeof(node));
temp->data = info;
temp->next = temp1->next;
temp1->next = temp;
counter++;
display();
}
int insertreppos()
{
if(head==NULL)
{
cout<<"List is empty";
}
cout<<"ENTER THE NODE NUMBER:";
cin>>node_number;
cout<<"ENTER ANY NUMBER:";
cin>>info;
node *temp1;
temp1 = head;
for( int i = 1 ; i < node_number ; i++ )
{
temp1 = temp1->next;
if( temp1 == NULL )
{
cout<<node_number<<" node is not exist"<< endl;
return 0;
}
}
temp1->data = info;
display();
}
int delfirst()
{
if(head==NULL)
{
return 0;
}
elseif(head->next==NULL)
{
head = NULL;
cout<<"The first node of the Linked List is deleted"<<endl;
counter--;
return 0;
}
node *temp;
temp = head;
head = temp->next;
free(temp);
cout<<"The first node of the Linked List is deleted"<<endl;
counter--;
display();
}
int dellast()
{
if(head==NULL)
{
display();
}
elseif(head->next==NULL)
{
head = NULL;
cout<<"The last node of the Linked List is deleted"<<endl;
counter--;
display();
return 0;
}
else
{
node *temp1,*old_temp;
temp1 = head;
while(temp1->next!=NULL)
{
old_temp=temp1;
temp1= temp1->next;
}
old_temp->next=NULL;
free(temp1);
cout<<"The last node of the Link List is deleted : "<<endl;
counter--;
display();
}
}
int delpos()
{
if(head==NULL)
{
return 0;
}
elseif(head ->next==NULL)
{
head = NULL;
cout<<"The last node of the Linked List is deleted"<<endl;
counter--;
display();
return 0;
}
cout<<"ENTER THE NODE NUMBER:";
cin>>node_number;
if(node_number > counter)
{
cout<<"No such node is exist";
return 0;
}
node *temp1;
temp1 = head;
for(int j=0; j<node_number; j++)
{
head=temp1->next;
if(j==node_number)
{
free(temp1);
counter--;
}
}
cout<<node_number<<" node of the Linked List is deleted"<<endl;
display();
}
int deletelist()
{
node *temp;
if(head==NULL)
return 0;
while(head!=NULL)
{
temp=head->next;
delete temp;
head=temp;
}
}
int display()
{
node *temp;
temp=head;
if(temp==NULL)
{
cout<<"List is empty";
return 0;
}
cout<<"\nThe elements are \n"<<endl;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
}
int main()
{
char ch;
do {
cout<<"\n\n";
cout<<"1.Insert at first\n";
cout<<"2.Insert at last\n";
cout<<"3.Insert after specified number of node\n";
cout<<"4.Delete at first node\n";
cout<<"5.Delete at last node\n";
cout<<"6.Delete specified number of node\n";
cout<<"7.Display\n";
cout<<"8.Replace specified number of node\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case'1':
insertfirst();
break;
case'2':
insertlast();
break;
case'3':
insertpos();
break;
case'4':
delfirst();
break;
case'5':
dellast();
break;
case'6':
delpos();
break;
case'7':
display();
break;
case'8' :
insertreppos();
break;
default :
cout<<"Invalid option";
}
cout<<"\ndo you want continue (y/n) : ";
cin>>ch;
} while(ch=='y');
deletelist();
display();
return 0;
}
Here are the obvious problems I can see:
1. The list exists as a global. This is bad because your functions will only work for that list, and just that one list. If you want to manipulate more lists, you need to make more functions or add branches in the functions.
2. Your I/O code and list manipulation code are mixed. This causes your operations to not be clearly defined, which leads to problem #3:
3. You have duplicate code. I can see at least three places where you malloc() nodes. Never duplicate code. Any bugs you run into will need to be fixed in each of the copies, and you'll always forget to update one (not necessarily always the same one).
I would not bother debugging memory leaks before these issues are resolved.
My recommendation is that you implement these functions and then write your I/O and other logic around them:
1 2 3 4 5 6 7 8 9 10 11 12
//Note: A node *& is a reference to a pointer. It lets you transparently modify
//the pointer that was passed as parameter. Feel free to change it to node **,
//if you prefer.
//Note: The functions that return bool return true on success.
void add_head(node *&list, int data);
void add_tail(node *&list, int data);
bool add_at_pos(node *&list, int pos, int data);
bool remove_head(node *&list);
bool remove_tail(node *&list);
bool remove_at_pos(node *&list, int pos);
void display_list(node *list);
bool replace_at_pos(node *&list, int pos, int data);