DATA STRUCTURES IN PYTHON
By : AMIT TRIVEDI
Data Structure As the name shows It represents how
data is stored/organized in Computer’s Memory .
Implementation of Data Structures can be done in two
ways.
Simple Data Structures : Built from primitive data types
(integers, real, charater, Boolean)
Example: Arrays or Linear List
Compound Data Structures: Simple Data structures are
used to form more complex data structure. They are
classified into two types
• Linear: means elements are stored in sequential order
Example: Stack, Queue, Linked List
• Non Linear : Data can be stored as multilevel Structure
Example Trees , Graphs
ASSIGNMENT 2-D LIST
1. Write a program to create a 2D list that input
element in the form of given row and
columns. Display
a) the total no of element
b) sum of all elements
c) sum of even elements
d) search the input elements exist in 2D list or
not.
e) transpose of elements (inter change row
with column)
f) input two 2D List and after adding each
corresponding elements, display the
resultant list.
Introduction of Stack
Items stored in the order same as
Stack Order (Last In First Out also
called LIFO) means item stored one
above another and when we want to
remove any element from the Stack,
as it is picked from the Top of the
Stack i.e. the item that is added at
last will remove first.
HAVE A LOOK ON DIFFERENT TYPES
OF STACK IN THE REAL WORLD
Stack of pebbles Stack of Stack of Coins
Chapati/Roti
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan
THERE ARE MAINLY TWO TYPES OF
OPERATIONS THAT CAN BE DONE WITH
STACK
i. push and
ii. pop.
Push: Insertion of an element on top of the
stack is called Push
Pop: Removal an element from the top of the
stack is called POP
Push and Pop Operations are done from single
end called TOP
Let’s understand it with an Example
Suppose we have a list of items which is to be inserted in the Stack of Size 5
24 56 4 9 2
PRACTICAL
IMPLEMENTATION
OF STACK
#Stack as linear list using s=[ ]
function c="y"
while(c=="y"):
print("1 for push")
def push_fun(s,a): print("2 for pop")
s.append(a) print("3 for display")
print("4 for exit")
choice=input("enter choice in the form of 1,2
def pop_fun(s): or 3:")
l=len(s) if(choice=="1"):
a= input("enter number for push")
if(l==0):
push_fun(s,a)
print("stack empty")
else: elif(choice=="2"):
print("Deleted pop_fun(s)
element=",s.pop()) elif(choice=="3"):
display(s)
def display(s): elif(choice=="4"):
exit()
l=len(s) else:
if(l==0): print("wrong input")
print("stack empty") c=input("do you want to continue or not:y for
for i in range(l-1,-1,-1): yes, n for no:")
print(s[i])
APPLICATION OF STACKS
i. Expression Evaluation and Conversion
Stack is used to evaluate prefix, postfix and infix expressions. Stack can also be
used to convert one form of expression to another.
ii. Backtracking
Suppose we are finding a path for solving a particular problem and we have chosen a
path and after following it later we realize that we have chosen wrong path. Now we
need to go back to the beginning of the path to start with new path. This can be
done with the help of stack.
iii.Recursion When a Function call itself this process is called recursion. In each
recursive call, there is need to save the current values of parameters, local variables
and the return address (the address where the control has to return from the call).
Whenever a function calls to another function, first its arguments, then the return
address and finally space for local variables is pushed onto the stack.
iv. To Reverse String
Stack is used to reverse a string. We push the characters of string one by one into
stack and then pop character from stack.
v. Function Call
Stack is also used to keep all information about the active functions or subroutines.
APPLICATION OF STACKS
For example convert the infix expression (A+B)*(C-D)/E into
postfix expression showing stack status after every step.
Example: Evaluate the following postfix expression
showing stack status after every step
8, 2, +, 5, 3, -, *, 4 /
ASSIGNMENTS
1. Convert the following infix expressions to
postfix expressions using stack
1. A + (B * C) ^ D – (E / F – G)
2. A * B / C * D ^ E * G / H
3. ((A * B) - ((C - D) * E / F) * G
2. Evaluate the following postfix expression E
given below; show the contents of the stack
during the
evaluation
1. E= 5,9,+2,/,4,1,1,3,_,*,+ 2
2. E= 80,35,20,-,25,5,+,-,*
3. E= 30,5,2,^,12,6,/,+,-
4. E=15, 3, 2, +, /, 7, + 2, *
INTRODUCTION OF QUEUE
Items stored in the order same as Queue (Fast In First Out
also called FIFO) means item stored one after another and the
when we want to remove an item from the Queue the Item
inserted first will remove first .
As if waiting in a queue for the train tickets, the first
one to stand in line is the first one to buy a ticket and get the
reservation.
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan
QUEUE EXAMPLES
Queue is just like as
queue in
• Cinema Hall
• Railway Booking
Counter
• In Bank Counter etc.
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan
APPLICATION OF QUEUE
Queue, as the name suggests is used whenever we need to manage
any group of tasks/objects in an First Come First Serve order for
Example:
Serving multiple requests on a single shared resource, like a printer,
CPU task scheduling etc.
Handling of interrupts in real-time systems. The interrupts are
handled in the same order as they arrive i.e First come first
served.
In real life scenario, Call Center phone systems uses Queues to hold
the calls of people , in an order, until a service representative is
free.
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan
INSERTION IS DONE IN THE QUEUE FROM REAR
END AND DELETION OPERATIONS ARE DONE
FROM FRONT END.
In Queue there are mainly two types of
operations
Insertion : Insertion of an element is done at
REAR end, Insertion operation is also called
ENQUEUE
and Deletion: Deletion of an element is done
at FRONT end, Deletion operation is also
called DEQUEUE
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan
LETS UNDERSTAND THROUGH AN EXAMPLE.
Suppose we have a list of items which is to be inserted in the Queue of Size 5
24 56 4 9 2
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan
SOME FUNCTIONS OF LIST THAT ARE
REQUIRED TO IMPLEMENT
STACKS/QUEUES
Python List .append() Add Single Element to The List
Removes Element at Given
Python List .pop(index)
Index
Python List .pop()
Removes last element
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan
PRACTICAL
IMPLEMENTATION
OF QUEUE
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan
STACK USING CLASS
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan
QUEUE USING CLASS
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan
www.pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan