#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
node *link;
};
class linkedlist
{
node *start;
public:
linkedlist()
{
start=NULL;
}
void insertbeg(int x);
void insertend(int x);
void insertpos(int x);
void delbeg();
void delend();
void delpos();
void display();
};
void linkedlist::insertbeg(int x)
{
node *nnode=new node;
nnode->data=x;
nnode->link=start;
start=nnode;
}
void linkedlist::insertend(int x)
{
node *nnode=new node;
nnode->data=x;
nnode->link=NULL;
node *temp=start;
while(temp->link!=NULL)
temp=temp->link;
temp->link=nnode;
}
void linkedlist::insertpos(int x)
{
int pos;
cout<<"Enter position";
cin>>pos;
int c=1;
node *nnode=new node;
nnode->data=x;
node *temp=start;
node *prev;
while(c<pos)
{
prev=temp;
temp=temp->link;
c=c+1;
}
prev->link=nnode;
nnode->link=temp;
}
void linkedlist::delbeg()
{
node *temp=start;
start=start->link;
cout<<"Deleted element is "<<temp->data<<endl;
delete temp;
getch();
}
void linkedlist::delend()
{
node *temp=start;
node *prev;
while(temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
prev->link=NULL;
cout<<"Deleted"<<temp->data<<endl;
delete temp;
getch();
}
void linkedlist::delpos()
{
int pos;
cout<<"Enter position";
cin>>pos;
int c=1;
node *temp=start;
node *prev;
while(c<pos)
{
prev=temp;
temp=temp->link;
c=c+1;
}
prev->link=temp->link;
cout<<"Deleted"<<temp->data<<endl;
delete temp;
getch();
}
void linkedlist::display()
{
node *temp=start;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->link;
}
cout<<endl;
getch();
}
void main()
{
clrscr();
int op;
linkedlist l;
do
{
clrscr();
cout<<"1.insert begin"<<endl;
cout<<"2.insert end"<<endl;
cout<<"3.insert position"<<endl;
cout<<"4.delete begin"<<endl;
cout<<"5.delete end"<<endl;
cout<<"6.delete position"<<endl;
cout<<"7.display"<<endl;
cout<<"8.exit"<<endl;
cout<<"enter ur option:";
cin>>op;
switch(op)
{
case 1:int n;
cout<<"Enter element to insert:";
cin>>n;
l.insertbeg(n);
break;
case 2:cout<<"enter element to insert:";
cin>>n;
l.insertend(n);
break;
case 3:cout<<"Enter element to insert:";
cin>>n;
l.insertpos(n);
break;
case 4:l.delbeg();
break;
case 5:l.delend();
break;
case 6:l.delpos();
break;
case 7:l.display();
break;
case 8:exit(0);
default:
cout<<"Invalid option"<<endl;
break;
}
}while(op!=8);
}
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
node *link;
};
class linkedlist
{
node *start;
public:
linkedlist()
{
start=NULL;
}
void insertbeg(int x);
void insertend(int x);
void insertpos(int x);
void delbeg();
void delend();
void delpos();
void display();
};
void linkedlist::insertbeg(int x)
{
node *nnode=new node;
nnode->data=x;
nnode->link=start;
start=nnode;
}
void linkedlist::insertend(int x)
{
node *nnode=new node;
nnode->data=x;
nnode->link=NULL;
node *temp=start;
while(temp->link!=NULL)
temp=temp->link;
temp->link=nnode;
}
void linkedlist::insertpos(int x)
{
int pos;
cout<<"Enter position";
cin>>pos;
int c=1;
node *nnode=new node;
nnode->data=x;
node *temp=start;
node *prev;
while(c<pos)
{
prev=temp;
temp=temp->link;
c=c+1;
}
prev->link=nnode;
nnode->link=temp;
}
void linkedlist::delbeg()
{
node *temp=start;
start=start->link;
cout<<"Deleted element is "<<temp->data<<endl;
delete temp;
getch();
}
void linkedlist::delend()
{
node *temp=start;
node *prev;
while(temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
prev->link=NULL;
cout<<"Deleted"<<temp->data<<endl;
delete temp;
getch();
}
void linkedlist::delpos()
{
int pos;
cout<<"Enter position";
cin>>pos;
int c=1;
node *temp=start;
node *prev;
while(c<pos)
{
prev=temp;
temp=temp->link;
c=c+1;
}
prev->link=temp->link;
cout<<"Deleted"<<temp->data<<endl;
delete temp;
getch();
}
void linkedlist::display()
{
node *temp=start;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->link;
}
cout<<endl;
getch();
}
void main()
{
clrscr();
int op;
linkedlist l;
do
{
clrscr();
cout<<"1.insert begin"<<endl;
cout<<"2.insert end"<<endl;
cout<<"3.insert position"<<endl;
cout<<"4.delete begin"<<endl;
cout<<"5.delete end"<<endl;
cout<<"6.delete position"<<endl;
cout<<"7.display"<<endl;
cout<<"8.exit"<<endl;
cout<<"enter ur option:";
cin>>op;
switch(op)
{
case 1:int n;
cout<<"Enter element to insert:";
cin>>n;
l.insertbeg(n);
break;
case 2:cout<<"enter element to insert:";
cin>>n;
l.insertend(n);
break;
case 3:cout<<"Enter element to insert:";
cin>>n;
l.insertpos(n);
break;
case 4:l.delbeg();
break;
case 5:l.delend();
break;
case 6:l.delpos();
break;
case 7:l.display();
break;
case 8:exit(0);
default:
cout<<"Invalid option"<<endl;
break;
}
}while(op!=8);
}
No comments:
Post a Comment