KEMBAR78
Class SULL (Struct Stack (Int Data Struct Stack Next ) Top Typedef Struct Stack Node Void Push Int Pop Void Display SULL ) SULL::SULL (Top NULL ) | PDF | Software Engineering | Computing
0% found this document useful (0 votes)
76 views8 pages

Class SULL (Struct Stack (Int Data Struct Stack Next ) Top Typedef Struct Stack Node Void Push Int Pop Void Display SULL ) SULL::SULL (Top NULL )

The document describes how to implement a stack and queue using linked lists in C++. It defines Node structures for the stack and queue with data and pointer to next elements. Methods like push(), pop(), insert(), delete() and display() are created to add/remove elements and display the elements in the stack/queue. The push() method adds an element to the top of the stack by changing the next pointer. Pop() removes the top element and returns the data. Insert() adds an element to the rear of the queue by changing the next pointers. Delete() removes elements from the front.

Uploaded by

anju_2718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views8 pages

Class SULL (Struct Stack (Int Data Struct Stack Next ) Top Typedef Struct Stack Node Void Push Int Pop Void Display SULL ) SULL::SULL (Top NULL )

The document describes how to implement a stack and queue using linked lists in C++. It defines Node structures for the stack and queue with data and pointer to next elements. Methods like push(), pop(), insert(), delete() and display() are created to add/remove elements and display the elements in the stack/queue. The push() method adds an element to the top of the stack by changing the next pointer. Pop() removes the top element and returns the data. Insert() adds an element to the rear of the queue by changing the next pointers. Delete() removes elements from the front.

Uploaded by

anju_2718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 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