Social Icons

banner image

OPERATIONS ON LINKED LIST

#include<iostream>
using namespace std;
#include<conio.h>

struct node
{
int data;
node *link;
};
node *start=NULL;
node *tail=NULL;

void create();
void display();
void insert(int);
void Delete(int);
int main()
{
int ch;
int position,item;
do
{
cout<<"\n Enter 1 to create"<<"\n"<<"Enter 2 to display "<<"\n Enter 3 to search"<<"\nEnter 4 to insert"<<"\n Enter 5 to delete :"<<"\n";
cin>>ch;
if(ch==1)
{
create();
}
else if(ch==2)
{
display();
}
else if(ch==4)
{
cout<<"Enter the position";
cin>>position;
insert(position);
}
else if(ch==5)
{
cout<<"enter the item";
cin>>item;
Delete(item);
}
}
while(ch==1||2||3||4||5);
getch();
return 0;
}



void display()
{
node *k;
if (start==NULL)
{
cout<<"list is empty";
}
else
{
for(k=start;k!=NULL;k=k->link)
{
cout<<"\n"<<k;
cout<<"\t"<<k->data;
cout<<"\t"<k->link;
}
}
}
void create()
{
node *p,*q;
p=new node;
cout<<"enter data of node";
cin>>p->data;
p->link==NULL;
if(start==NULL)
{
start=p;
tail=p;
}
else
{
tail->link=p;
tail=p;
}
}
void insert(int position)
{
node *p,*q;
p=new node;
cout<<"enter data part";
cin>>p->data;
p->link=NULL;
if(position==1)
{
p->link=start;
start=p;
}
else
{
q=start;
int k=1;
while((q->link!=NULL ) && (k!=position-1))
{
k++;
q=q->link;
}
}
}
void Delete(int item)
{
if(start==NULL)
{
cout<<"case of underflow,it can not be deleted";
}
else
{
node *p,*pp;
if( start ->data==item)
{
p=start;
start=start ->link;
}
else
{
p=start;
while((p != NULL)&& (p ->data != item) )
{
pp =p;
p=p -> link;
}
if(p==NULL)
{
cout<<"can be delete";
}
else
{
pp -> link = p -> link;
}
}
delete p;
}


}
OPERATIONS ON LINKED LIST OPERATIONS ON LINKED LIST Reviewed by Shobhit Goel on September 11, 2015 Rating: 5

No comments:

Airtel Hackaton 2017

Powered by Blogger.