LINKED LIST
IMPLEMENTATION OF
STACK
MODULE 3
DR. SINDHIA LINGASWAMY, VIT 1
DR. SINDHIA LINGASWAMY, VIT 2
• 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.
DR. SINDHIA LINGASWAMY, VIT
3
OPERATIONS
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.
LinkedStack
DR. SINDHIA LINGASWAMY, VIT
4
• 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.
8
Linkedstackafterinsertinga newnode
DR. SINDHIA LINGASWAMY, VIT
5
ALGORITHM
TOPUSH AN ELEMENT INTO A LINKED
STACK
DR. SINDHIA LINGASWAMY, VIT 6
• In Step 1, memory is allocated
for the new node.
• In Step 2, the DATA part of the
new node is initialized with the
value to be storedin the node.
• In Step 3, we check if the new
node is the first node of the
linkedlist.
• This is done by checking if TOP =
NULL. In case the IF statement
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.
Step1: Allocatememory for the new
node and name it as NEW_NODE
Step2: SET NEW_NODE -> DATA= VAL
Step3: IF TOP = NULL
SET NEW_NODE -> NEXT = NULL
SET TOP = NEW_NODE
ELSE
SET NEW_NODE -> NEXT = TOP
SET TOP = NEW_NODE
[END OF IF]
Step4: END
DR. SINDHIA LINGASWAMY, VIT 7
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
DR. SINDHIA LINGASWAMY, VIT
8
•If an attempt is made to delete a value from a stackthat
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.
Linkedstackafterdeletinga node
TOP
DR. SINDHIA LINGASWAMY, VIT 9
ALGORITHM
TOPOP AN ELEMENT INTO A
LINKED STACK
DR. SINDHIA LINGASWAMY, VIT 10
Step 1: IF TOP=NULL
PRINT “UNDERFLOW”
Goto Step 5
[END OF IF]
Step 2: SET PTR = TOP
Step 3: SET TOP = TOP->NEXT
Step 4: FREE PTR
Step 5: END
•In Step 1,we first check for
the UNDERFLOW condition.
TOP!=NULL
•In Step 2,we use a pointer
PTR that points to TOP.
•In Step 3,TOP is made to
point to the next node in
sequence.
•In Step 4,the memory
occupied by PTR is given back
to the free pool.
DR. SINDHIA LINGASWAMY, VIT 11
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.
DR. SINDHIA LINGASWAMY, VIT
12
TO PEEK AN ELEMENT FROM A
LINKED STACK
• In step 1, if TOP
• =NULL means that the stack is
empty.
• Else it will return the data on TOP,
which will be our output or Peeped
out Value.
Step 1: IF TOP= NULL
PRINT “UNDERFLOW”
ELSE
RETURN TOP->DATA
[END OF IF]
Step 2: END
DR. SINDHIA LINGASWAMY, VIT 13

Linked list implementation of Stack

  • 1.
    LINKED LIST IMPLEMENTATION OF STACK MODULE3 DR. SINDHIA LINGASWAMY, VIT 1
  • 2.
  • 3.
    • The drawbackof 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. DR. SINDHIA LINGASWAMY, VIT 3
  • 4.
    OPERATIONS Push Operation:- •The pushoperation is used to insert an element into the stack. •The new element is added at the topmost position of the stack. LinkedStack DR. SINDHIA LINGASWAMY, VIT 4
  • 5.
    • To insertan 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. 8 Linkedstackafterinsertinga newnode DR. SINDHIA LINGASWAMY, VIT 5
  • 6.
    ALGORITHM TOPUSH AN ELEMENTINTO A LINKED STACK DR. SINDHIA LINGASWAMY, VIT 6
  • 7.
    • In Step1, memory is allocated for the new node. • In Step 2, the DATA part of the new node is initialized with the value to be storedin the node. • In Step 3, we check if the new node is the first node of the linkedlist. • This is done by checking if TOP = NULL. In case the IF statement 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. Step1: Allocatememory for the new node and name it as NEW_NODE Step2: SET NEW_NODE -> DATA= VAL Step3: IF TOP = NULL SET NEW_NODE -> NEXT = NULL SET TOP = NEW_NODE ELSE SET NEW_NODE -> NEXT = TOP SET TOP = NEW_NODE [END OF IF] Step4: END DR. SINDHIA LINGASWAMY, VIT 7
  • 8.
    POP OPERATION •The popoperation 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 DR. SINDHIA LINGASWAMY, VIT 8
  • 9.
    •If an attemptis made to delete a value from a stackthat 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. Linkedstackafterdeletinga node TOP DR. SINDHIA LINGASWAMY, VIT 9
  • 10.
    ALGORITHM TOPOP AN ELEMENTINTO A LINKED STACK DR. SINDHIA LINGASWAMY, VIT 10
  • 11.
    Step 1: IFTOP=NULL PRINT “UNDERFLOW” Goto Step 5 [END OF IF] Step 2: SET PTR = TOP Step 3: SET TOP = TOP->NEXT Step 4: FREE PTR Step 5: END •In Step 1,we first check for the UNDERFLOW condition. TOP!=NULL •In Step 2,we use a pointer PTR that points to TOP. •In Step 3,TOP is made to point to the next node in sequence. •In Step 4,the memory occupied by PTR is given back to the free pool. DR. SINDHIA LINGASWAMY, VIT 11
  • 12.
    Peek Operation • ThePeek 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. DR. SINDHIA LINGASWAMY, VIT 12
  • 13.
    TO PEEK ANELEMENT FROM A LINKED STACK • In step 1, if TOP • =NULL means that the stack is empty. • Else it will return the data on TOP, which will be our output or Peeped out Value. Step 1: IF TOP= NULL PRINT “UNDERFLOW” ELSE RETURN TOP->DATA [END OF IF] Step 2: END DR. SINDHIA LINGASWAMY, VIT 13