STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai
STACK Using Linked List What is Stack ? Operation on Stack … ( PUSH and POP) Applications of Stack
STACK A stack is a LIFO structure and physically it can be implemented as an array or as a linked list. A stack is implemented as an array that inherits all the properties of an array and if implemented as a linked list , all characteristics of a linked list are possessed by it. top S T A C K
OPERATIONS ON STACK A stack is generally implemented with two basic operations- Push and Pop. Whatever way a stack may be implemented, insertion (push) and deletions (POP) occur at the top only. Push – Allows adding an element at the top of the Stack. Pop - Allows to remove an element from the top of the Stack.
Stack Operations (PUSH , POP) Example top 6
Stack Operations (PUSH , POP) Example top 1 6
Stack Operations (PUSH , POP) Example 7 top 1 6
Stack Operations (PUSH , POP) Example 8 7 top 1 6
Stack Operations (PUSH , POP) Example 8 7 top 1 6
Stack Operations (PUSH , POP) Example 7 top 1 6
CREATING A STACK (LINKED STACK) The creation of a stack ( as a linked list) is the same as the creation of a linked list i.e., after getting a new node, TOP point to the newly inserted node.
Creating a Node for Stack struct stack { int info; stack *next; } *top, s; Info next S
INSERTION IN A LINKED STACK (PUSH) TOP Fig A 44 55 n TOP 33 44 55 n Fig B TOP 22 33 44 55 n Fig C As a push can only occur at the TOP gets modified every time. For instance, if we have a stack as shown in fig (a) after pushing 33 , it becomes as Fig (b). After pushing another element 22, it becomes as Fig (c).
DELETION (POP) FROM A LINKED STACK TOP 22 33 44 55 n Fig A TOP 33 44 55 n Fig B TOP 44 55 n Fig C Deletion i.e. popping also require modification of TOP i.e. TOP is made to point to the next node in the sequence. For instance, if the stack is shown in Fig a; after popping 22 , it becomes as Fig b. After Popping 33 , it becomes a Fig c.
Application of Stack Reversing a Line POLISH NOTATION CONVERSION OF INFIX EXPRESSION TO POSTFIX (SUFFIX) EXPRESSION
THANK YOU