The program which I am posting here you will not find any where on internet. It is very easy to understand. I have attached some pics. SO that you can learn the algorithm of this program as well.
#include<iostream>
#include<conio.h>
#include<malloc.h>
using namespace std;
void creat(int m);
void display();
void rev();
void insert(int data,int pos);
void del(int data1);
struct node
{
int info;
struct node *link;
}*start;
int main ()
{
start =NULL;
int ch,n,m,data,pos;
while(1)
{
cout<<"1. Create List"<<endl;
cout<<"2. Display List"<<endl;
cout<<"3. Insertion"<<endl;
cout<<"4. Deletion"<<endl;
cout<<"5. Reverse"<<endl;
cout<<"6. Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nenter num of nodes to create:: ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\nenter data::";
cin>>m;
creat(m);
}
break;
case 2:
display();
break;
case 3:
cout<<"\nenter data";
cin>>data;
cout<<"\nenter position";
cin>>pos;
insert(data,pos);
display();
break;
case 4:
int data1;
cout<<"\nenter data, u want to del node ";
cin>>data1;
del(data1);
display();
break;
case 5:
cout<<"\nafter reverse\n";
rev();
display();
break;
case 6:
exit(0);
default:
cout<<"invalid choice";
}
}
return 0;
}
void creat(int m)
{
struct node *temp,*q;
temp=(node*)malloc(sizeof(node));
temp->info=m;
temp->link=NULL;
if(start==NULL)
{
start=temp;
}
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=temp;
}
}
void display()
{
struct node *q;
if (start==NULL)
{cout<<"List Empty";
}
q=start;
while(q!=NULL)
{
cout<<q->info<<endl;
q=q->link;
}
}
void rev()
{
struct node *p1,*p2,*p3;
if(start==NULL)
{
return ;
}
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link= p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}
void insert(int data,int pos)
{
struct node *temp,*q;
q=start;
for(int i=0;i<pos-1;i++)
{
q=q->link;
if(q==NULL)
{
cout<<"There are less emelents "<<pos;
}
}
temp=(node*)malloc(sizeof(struct node));
temp->info=data;
temp->link=q->link;
q->link=temp;
}
void del(int data)
{
struct node *tmp,*q;
if(start->info==data)
{
tmp=start;
start=start->link; /*first element deleted*/
free(tmp);
return;
}
q=start;
while(q->link->link!=NULL)
{
if(q->link->info==data) /*element deleted in between*/
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
cout<<"Deleted "<<data<<endl;
return;
}
q=q->link;
}
if(q->link->info==data)
{
tmp=q->link;
free(tmp);
q->link=NULL;
return;
}
cout<<"Element "<<data<<" not found"<<endl;
}
#include<iostream>
#include<conio.h>
#include<malloc.h>
using namespace std;
void creat(int m);
void display();
void rev();
void insert(int data,int pos);
void del(int data1);
struct node
{
int info;
struct node *link;
}*start;
int main ()
{
start =NULL;
int ch,n,m,data,pos;
while(1)
{
cout<<"1. Create List"<<endl;
cout<<"2. Display List"<<endl;
cout<<"3. Insertion"<<endl;
cout<<"4. Deletion"<<endl;
cout<<"5. Reverse"<<endl;
cout<<"6. Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nenter num of nodes to create:: ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\nenter data::";
cin>>m;
creat(m);
}
break;
case 2:
display();
break;
case 3:
cout<<"\nenter data";
cin>>data;
cout<<"\nenter position";
cin>>pos;
insert(data,pos);
display();
break;
case 4:
int data1;
cout<<"\nenter data, u want to del node ";
cin>>data1;
del(data1);
display();
break;
case 5:
cout<<"\nafter reverse\n";
rev();
display();
break;
case 6:
exit(0);
default:
cout<<"invalid choice";
}
}
return 0;
}
void creat(int m)
{
struct node *temp,*q;
temp=(node*)malloc(sizeof(node));
temp->info=m;
temp->link=NULL;
if(start==NULL)
{
start=temp;
}
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=temp;
}
}
void display()
{
struct node *q;
if (start==NULL)
{cout<<"List Empty";
}
q=start;
while(q!=NULL)
{
cout<<q->info<<endl;
q=q->link;
}
}
void rev()
{
struct node *p1,*p2,*p3;
if(start==NULL)
{
return ;
}
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link= p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}
void insert(int data,int pos)
{
struct node *temp,*q;
q=start;
for(int i=0;i<pos-1;i++)
{
q=q->link;
if(q==NULL)
{
cout<<"There are less emelents "<<pos;
}
}
temp=(node*)malloc(sizeof(struct node));
temp->info=data;
temp->link=q->link;
q->link=temp;
}
void del(int data)
{
struct node *tmp,*q;
if(start->info==data)
{
tmp=start;
start=start->link; /*first element deleted*/
free(tmp);
return;
}
q=start;
while(q->link->link!=NULL)
{
if(q->link->info==data) /*element deleted in between*/
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
cout<<"Deleted "<<data<<endl;
return;
}
q=q->link;
}
if(q->link->info==data)
{
tmp=q->link;
free(tmp);
q->link=NULL;
return;
}
cout<<"Element "<<data<<" not found"<<endl;
}
No comments:
Post a Comment