Sunday, July 4, 2010

PROGRAM TO ILLUSTRATE LINKED LIST OPERATIONS(DS)

Buzz It
#include<iostream.h>
#include<conio.h>
struct node
{
node*next;
int data;
};
class linkedlist
{
public:
node*start;
node*last;
node*temp;
node*head;
int c;
linkedlist()
{
start=last=NULL;
}
void insertbeg();
void insertlast();
void deletbeg();
void deletlast();
void display();
};
void linkedlist :: insertbeg()
{
head=new node;
cout<<"enter the data\n";
cin>>head->data;
if(start==NULL)
{
head->next=NULL;
start=last=head;
}
else
{
head->next=start;
start=head;
}}
void linkedlist::insertlast()
{
head=new node;
cout<<"enter the data\n";
cin>>head->data;
head->next=NULL;
if(start==NULL)
{
start=last=head;
}
else
{
last->next=head;
last=head;
}}
void linkedlist::deletbeg()
{
if(start==NULL)
cout<<"list is empty\n";
else if(start==last)
start=last=NULL;
else
{
temp=start->next;
delete start;
start=temp;
}}
void linkedlist::deletlast()
{
node*prev;
node*temp;
if(start==NULL)
cout<<"list is empty\n";
else if(start==last)
start=last=NULL;
else
{
temp=start;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
delete temp;
prev->next=NULL;
last=prev;
}}
void linkedlist:: display()
{
node*temp;
temp=start;
while(temp!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->next;
}
if(start==NULL)
cout<<"empty\n";
}
void main()
{
clrscr();
linkedlist ob;
int c;
do
{
cout<<"\nMENU\n1-Insertbeginning\n2-Insertlast\n3-Deletebeginning\n4-
Deletlast\n5-Display\n6-Exit\n";
cin>>c;
switch(c)
{
case 1:ob.insertbeg();
break;
case 2:ob.insertlast();
break;
case 3:ob.deletbeg();
break;
case 4:ob.deletlast();
break;
case 5:ob.display();
break;
}}
while(c<=5);
getch();
}

OUTPUT:
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
1e
Enter the data
10
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
1e
Enter the data
20
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
5
20
10
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
2e
Enter the data
30
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
5
20
10
30
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
3
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
5
10
30
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
4
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
5
10
4
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
5l
List is empty
MENU
1-Insertbeginning
2-Insertlast
3-Deletebeginning
4-Deletlast
5-Display
6-Exit
6


0 comments:

Post a Comment