Data Structure in Python
Data Structure in Python
By:-
Amit Yerpude
PGT(Computer Science)
Kendriya Vidyalaya, Khagaria
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria
• The various data structures in computer science are divided broadly into two
categories shown below.
• Liner Data Structures
These are the data structures which store the data elements in a sequential manner.
Array: It is a sequential arrangement of data elements paired with the index of the
data element.
Linked List: Each data element contains a link to another element along with the data
present in it.
Stack: It is a data structure which follows only to specific order of operation. LIFO(last
in First Out) or FILO(First in Last Out).
Queue: It is similar to Stack but the order of operation is only FIFO(First In First Out).
Matrix: It is two dimensional data structure in which the data element is referred by a
pair of indices.
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria
Stack in Python
• A stack is a linear data structure that stores items in a Last-
In/First-Out (LIFO) or First-In/Last-Out (FILO) manner.
• In stack, a new element is added at one end and an element is
removed from that end only.
• The insert and delete operations are often called push and pop.
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria
while True:
print("STACK OPERATION MENU")
print("1. PUSH IN STACK")
print("2. POP FROM STACK")
print("3. PEEK TOPMOST ELEMENT")
print("4. DISPLAY STACK ELEMENT")
print("5. EXIT PROGRAM")
choice=int(input("Enter Your Choice(1-5):- "))
if choice ==1:
data=int(input("Enter data to push in stack:- "))
push(stk,data)
elif choice==2:
data=pop(stk)
print("Popped element from top of stack = ",data)
elif choice==3:
data=peek(stk)
print("Top most element in stack is ",data)
elif choice==4:
display(stk)
elif choice == 5:
break
else:
print("Invalid Choice")
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria
• A queue is a first-in-first
out(FIFO) data structure. It
has three primitive operations:
enqueue: Add an element to
the queue
dequeue: Remove an element
from the queue
Peek: Get the top most element
of the queue.i.e, the element at
the front position.
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria
implementation of Queue
def isempty(Qu): def Enqueue(Qu, data): def Dequeue(Qu):
if Qu==[]: Qu.append(data) if isempty(Qu):
return True if len(Qu)==1: print("Underflow or
else: front=rear=0 Queue is Empty")
else: else:
return False
rear=len(Qu)-1
data=Qu.pop(0)
if len(Qu)==0:
front=rear=None
return data
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria
rear=None
while True:
print("QUEUE OPERATION MENU")
print("1. ENQUE")
print("2. DEQUE")
print("3. PEEK")
print("4. DISPLAY")
print("5. EXIT")
choice=int(input("Enter Your Choice(1-5):- "))
if choice ==1:
data=int(input("Enter data to insert in queue- "))
Enqueue(myque,data)
elif choice==2:
data=Dequeue(myque)
print("Dequeued element from front of queue = ",data)
elif choice==3:
data=Peek(myque)
print("Element at front of queue is ",data)
elif choice==4:
Display(myque)
elif choice == 5:
break
else:
print("Invalid Choice")
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria
Presented By: - Amit Yerpude, 6/25/2020
PGT(CS), KV Khagaria
Thank you!!!