You are on page 1of 16

Department of Computer Science & Engineering

UIT-RGPV

Topic: Stack Data Structure

Course: Data Structures (CS303)

By:
Mr. Praveen Yadav
Assistant Professor
UIT-RGPV, Bhopal
Stack Data Structure
• A stack is a list of elements in which an element
may be inserted or deleted only at one end,
called the TOP of the stack.
• A Stack is a linear Data Structure.
• Stacks are also called Last-In-First-Out (LIFO).
• Elements are removed from a stack in the reverse
order of that in which the were inserted into the
stack.
• Stack is said to be in Overflow state when it is
completely full and is said to be
in Underflow state if it is completely empty.
Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 2
Basic Operations Associated with Stack
• Two Basic Operations are:
1. PUSH Operation
2. POP Operation

PUSH Operation: It is used to insert an element


into a stack.

POP Operation: It is used to delete an element


from a stack.
Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 3
Procedure Insert an Item into Stack

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 4


Insertion (PUSH Operation)
• Suppose the following 5 elements are
pushed, in order, onto an empty stack:
• A, B, C, D, E

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 5


Insertion (PUSH Operation)
• After Insertion of all 5 elements stack
becomes:

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 6


Procedure to delete an Item from Stack

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 7


Deletion (POP Operation)
• Delete an element from the stack

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 8


Implementation of Stack
• Stack can be easily implemented using an
Array or a Linked List.
1. Implement Stack using array.
2. Implement Stack using Linked List.
• Arrays are quick, but are limited in size.
• Linked List requires overhead to allocate, link,
unlink, and deallocate, but is not limited in
size.

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 9


Implement Stack using array
• Already study in previous slides

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 10


Implement Stack using Linked List
• Linked Stack: Linked Stack is a stack that is
implemented using a singly linked list.
• The INFO field of the node hold the element
of the stack.
• LINK fields hold address of the next element in
the stack.
• The START pointer of the linked list behaves as
the TOP pointer variable of the stack.
• NULL pointer of the last node in the list signal
the bottom of stack.
Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 11
Linked Representation of Stack

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 12


PUSH Operation into STACK

• PUSH “WWW” into Stack (Inserting a Node).

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 13


POP Operation into STACK
• Deleting a Node from Stack.

• Delete a Node from Stack.

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 14


Applications of Stack
• Stack Frames (Used in Recursion).
• Reversing a String.
• Parsing (Used in Compiler).
• Arithmetic Expression Conversion(Conversion
of Infix to Prefix and Postfix Expressions) and
Expression Evaluation.

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 15


Thank You

Data Structures By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 16

You might also like