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

Unit 2 - Linked Stack

This document describes data structures and operations on a linked stack using a C program. It discusses the push, pop, and peek operations on a linked stack. The push operation inserts a new node at the top of the stack. The pop operation deletes the top node and updates the stack. Peek returns the value at the top node without deleting it. Algorithms for each operation are provided that first check for empty/full conditions and then perform the appropriate changes by updating pointers between nodes. A sample C program implementing these operations on a linked stack is also included.

Uploaded by

genius
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Unit 2 - Linked Stack

This document describes data structures and operations on a linked stack using a C program. It discusses the push, pop, and peek operations on a linked stack. The push operation inserts a new node at the top of the stack. The pop operation deletes the top node and updates the stack. Peek returns the value at the top node without deleting it. Algorithms for each operation are provided that first check for empty/full conditions and then perform the appropriate changes by updating pointers between nodes. A sample C program implementing these operations on a linked stack is also included.

Uploaded by

genius
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Subject : Data Structure

College : VGEC
 The drawback of using array method is fixed size, but
in this method it’s removed.

The storage requirement of linked representation of


the stack with n elements is O(n) and the typical time
requirement for the operation is O(1).

In a linked stack, every node has two parts : one that
stores data and another that stores the address of the
next node.

The START pointer of the linked list is used as TOP.


OPERATIONS ON A LINKED STACK
Push Operation:-
•The push operation is used to insert an element
into the stack.
•The new element is added at the topmost position
of the stack.

Linked Stack
• To insert an element with value 8, we first check if
TOP=NULL.
• If this is the case, then we allocate memory for a new
node, store the value in its DATA part and NULL in its
NEXT part.
• The new node will then be called TOP.
• If TOP!=NULL, then we insert the new node at the
beginning of the linked stack and name this new
node as TOP.

Linked stack after inserting a new node


Algorithm
To Push an element into a linked stack
• In Step 1, memory is allocated
Step 1: Allocate memory for the new
node and name it as NEW_NODE for the new node.
• In Step 2, the DATA part of the
Step 2: SET NEW_NODE -> DATA = VAL
new node is initialized with the
Step 3: IF TOP = NULL value to be stored in the node.
SET NEW_NODE -> NEXT = NULL
SET TOP = NEW_NODE • In Step 3, we check if the new
ELSE node is the first node of the
SET NEW_NODE -> NEXT = TOP linked list.
SET TOP = NEW_NODE
[END OF IF] • This is done by checking if TOP =
NULL. In case the IF statement
Step 4: END
evaluates to true, then NULL is
stored in the NEXT part of the
node and the new node is called
TOP. However, if the new node is
not the first node in the list, then
it is added before the first node
of the list (that is, the TOP node)
and termed as TOP.
Pop Operation:-

•The pop operation is used to delete an element


into the stack.
•The element is deleted at the topmost position of
the stack.
•However, before deleting the value, we must first
check if TOP=NULL, because if this is the case, then
it means that the stack is empty and no more
deletions can be done
• If an attempt is made to delete a value from a stack that
is already empty, an UNDERFLOW message is printed.

• In case TOP!=NULL, then we will delete the node


pointed by TOP, and make TOP point to the second
element of the linked stack. Thus, the updated stack
becomes like this.

TOP

Linked stack after deleting a node


Algorithm
To Pop an element into a linked stack
• In Step 1,we first check for
Step 1: IF TOP=NULL the UNDERFLOW condition.
PRINT “UNDERFLOW”
Goto Step 5
TOP!=NULL
[END OF IF] • In Step 2,we use a pointer
Step 2: SET PTR = TOP PTR that points to TOP.
Step 3: SET TOP = TOP->NEXT
• In Step 3,TOP is made to
Step 4: FREE PTR point to the next node in
Step 5: END sequence.

• In Step 4,the memory


occupied by PTR is given back
to the free pool.
Peek Operation:-
• The Peek operation is used to see the value of the
topmost element of the stack without deleting it
from the stack.
• Peek operation first checks if the stack is empty,
i.e., if TOP=NULL, then an appropriate message is
printed, else the value is returned.
Algorithm
To Peek an element from a linked stack
• In step 1, if TOP
Step 1: IF TOP = NULL
PRINT “UNDERFLOW”
=NULL means that
ELSE the stack is empty.
RETURN TOP->DATA
[END OF IF]
• Else it will return the
Step 2: END data on TOP, which
will be our output or
Peeped out Value.
void push()
#include<stdio.h>
{printf("enter elements\n");
#include<conio.h> ptr=(node*)malloc(sizeof(node));
#include<stdlib.h> if(top==NULL)
{top=(node*)malloc(sizeof(node));
typedef struct node scanf("%d",&top->data);
{int data; top->next=NULL;
}
struct node *next;
else
}node; {scanf("%d",&ptr->data);
typedef node *list; ptr->next=top;
list top=NULL; top=ptr;
list ptr; }
}
void display()
{ ptr=top; void pop()
if(top==NULL) {if(top==NULL)
printf("Underflow\n"); printf("Underflow\n");
else else
{printf("elements are\n"); {printf("popped element
is %d\n",top->data);
while(ptr!=NULL)
top=top->next;
{
printf("%d ",ptr->data); }}
ptr=ptr->next;
}}
}
switch(ch)
void peep() { case 1:
{if(top==NULL) push();
printf("Underfloe\n"); break;
else
case 2:
{ printf("elements ts : display();
%d\n",top->data); break;
}
} case 3:
void main() pop();
break;
{int ch;
clrscr(); case 4:
do peep();
{ break;
printf("Enter switch: "); }}while(ch!=0);
scanf("%d",&ch); getch();}

You might also like