You are on page 1of 8

Stack using Linked List

Class SULL
{ Stack
data Next
struct stack
{
int data; top
struct stack *next; NULL

}*top;
typedef struct stack node;
void push();
int pop();
void display();
SULL();
};
SULL::SULL() { top=NULL; }
Void SULL:: push()
{
top
node *temp; NULL
Int element;
Temp=new node;
Cout<<"enter the inserted element in to the stack";
Cin>>element; top
100
temp->data=element;
temp->next=top; For Next element
10 NULL
top temp(100)
top=temp; 200
}
20 100 10 NULL
Temp(200) 100
int SULL::pop()
top
{ P
200 200
int k;
node *p,*q; 20 100 10 NULL
p=top; 200 100

k=top->data;
q=top; top
top=p->next; 100

free(q);
10 Null
return(k); 100
}
Void SULL:: display()
{
node *p;
p=top;
printf("\nelements in the stack are\n");
if(top!=NULL)
{
while(p!=NULL)
{
printf("%d->",p->data); out put :
p=p->next;
10
}
}
else
printf("QUEUE IS EMPTY");
}
Class QULL Queue using Linked List
{
struct queue Queue
{ data next

int data;
struct queue *next;
front rea
}*front,*rear; NULL r
NULL
Public:
typedef struct queue node;
Void insert();
Int delete();
Void display();
QULL(); };
QULL::QULL()
{ front=NULL; rear=NULL; }
Void QULL:: insert()
{ front rear
NULL NULL
node *temp;
Int element;
temp=new node;
Cout<<"enter the inserted element in to the queue";
Cin>>element; front rear
if(front==NULL) 100 100
{
temp->data=element;
temp->next=front; 10 NULL
front=rear=temp; temp(100)
}
else
{ front rear
temp->data=element; 100 200
temp->next=rear->next;
rear->next=temp;
rear=temp; 10 200 20 NULL
} 100 temp(200)
}
int QULL::delete()
front rear
{ 100 100

int k;
10 200 20 NULL
node *p; 100 200
if(front!=NULL)
After deletion:
{
k=front->data; front rear
200 200
p=front;
front=front->next; 20 NULL

free(p); 200

return(k);
}
void QULL::display()
{
node *p;
p=front;
cout<<"\nelements in the list are\n";
if(front!=NULL)
while(p!=NULL)
OUTPUT: 10
{cout<<“->“<<p->data;
p=p->next;
}
else
Cout<<"QUEUE IS EMPTY";
}

You might also like