CHAPTER 3
STACK
Data structure :
A Data Structure is a group of data that have different data types which can be accessed
as a unit .
A data structure has well-defined operations , behaviour and properties.
CLASSIFICATION OF DATA STRUCTURES
[Link] Data Structures
A data structure in which elements are organized in a sequence is called linear data
structure. Eg: stack , queue, array, linked list
[Link] Linear Data Structures
These are multilevel data structures. Eg. Tree , graph
STACK
• Stack is a linear data structure
• 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.
• It follows the principle Last In First Out (LIFO). LIFO means the element
inserted last would be the first to be deleted.
Operations on Stack
There are mainly two types of operations that can be done with stack
1. Push 2. Pop
Push : Insertion of an element on top of the stack is called Push.
Pop : Removal of an element from the top of the stack is called POP.
Note: Push and Pop operations are done from single end called TOP
Eg: Suppose a list of 5 items to be inserted in the stack .
2
Push 9 9
operation 4 4 4
56 56 56 56
24 24 24 24 24
Push 24 Push 56 Push 4 Push 9 Push 2 Stack
overflow
Maya Giby
PGT Computer Sc
2
Pop 9 9
operation 4 4 4
56 56 56 56
24 24 24 24 24
Pop Pop Pop Pop Pop Stack
underflow
Stack Overflow: It refers to a situation , when one tries to push an element in stack, that
is full .(Stacks are list implemented , since list can grow, overflow condition does not
arise until all memory is exhausted.
Stack Underflow : It refers to a situation when one tries to pop an item from an empty
stack .
#implementation of stack using List
stack =[]
while True:
print("[Link]")
print("[Link]")
print("[Link] elements of stack")
choice=int(input("Enter your choice"))
if choice == 1:
a=int(input("Enter the element which you want to push"))
[Link](a)
elif choice == 2:
if stack == []:
print("Stack is empty...Underflow case...")
else:
print("Deleted element is :", [Link]())
elif choice == 3:
print("The elements in the stack =", stack)
else:
print("Wrong input...")
ch = input("Do you want to continue or not(y/n) ?")
if ch in 'nN':
break
Maya Giby
PGT Computer Sc