0% found this document useful (0 votes)
2 views

3.2 Stack Using Linked List

The document describes how to implement a stack using a linked list, detailing the operations of push, pop, and display. The push operation involves creating a new node and adjusting the top pointer, while the pop operation removes the top node and handles cases for an empty stack. The display function iterates through the stack to print its elements or indicate if the stack is empty.

Uploaded by

karanam bharggav
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)
2 views

3.2 Stack Using Linked List

The document describes how to implement a stack using a linked list, detailing the operations of push, pop, and display. The push operation involves creating a new node and adjusting the top pointer, while the pop operation removes the top node and handles cases for an empty stack. The display function iterates through the stack to print its elements or indicate if the stack is empty.

Uploaded by

karanam bharggav
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/ 20

STACK USING LINKED LIST

Stack Representation
A stack can be represented using linked list
which is the dynamic representation stack.
 Operations on Stack using Linked list

 PUSH (Insertion at front-end)

 POP (Deletion at front-end)

 Display
Push Operation
Push
1. create a new node ( say temp) containing
the information to be inserted.
2. node temp;
3. temp->info=data;
4. temp->next=NULL;
Push
5. If the stack is empty i.e. top = NULL then
make the new node as the first node in the
list.
Push
5. If the stack is empty i.e. top = NULL then
make the new node as the first node in the
list
6. top=temp; top
Push
7. If the stack is not empty then
- Store the address of head node in the next
part of the new node
temp->next=top
Push

8. -Make the new node as the head node


top = temp;
Void push( )
{ node temp;
temp->info=data;
temp->next=NULL;
If(top==NULL)
top=temp;
else
{
temp->next=top;
top = temp;
}}
POP
POP
void pop(slist *top)
{
if(top==NULL) //CASE-1
printf(“STACK IS EMPTY");
else //CASE 2 & 3
{
printf("\nDeleted element is %d",top->info) ;
top=top->next ;
}
}
POP
CASE-1
1. If the stack is empty i.e. top=NULL just
display a message that stack is empty.

Print stack empty


POP
CASE-2 & 3
1. Print deleted node is top->info;

2. top=top->next
Display
Display
void display( )
{
slist * curn;
if(top==NULL) // CASE-1
printf(“STACK IS EMPTY");
else // CASE-2
{
curn=top;
while(curn !=NULL)
{
Printf(“\t%d”,curn->info );
curn =curn->next ;
}
}
}
Display
CASE-1
1. if(top==NULL)

printf(“Stack is empty”);
Display
CASE-2
1. else
2. { curn=top;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Curn is 100 its not equal to NULL, so curn->info that’s 10 is displayed and
curn moves to next node

10
Display
CASE-2
1. else
2. { curn=top;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Curn is 200 its not equal to NULL, so curn->info that’s 20 is displayed and
curn moves to next node

10 20
Display
CASE-2
1. else
2. { curn=top;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Curn is 300 its not equal to NULL, so curn->info that’s 30 is displayed and
curn moves to next node

10 20 30
Display
CASE-2
1. else
2. { curn=top;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Now curn is NULL so while is terminated.

10 20 30

You might also like