Data Structures
Data Structures
12
Chapter 6: Data Structures in Python For Video Lecture Click here:
What is Stack:
A stack is a linear data structure in python in which addition and deletion of elements can be done at one
end only that is called TOP. A stack is known as LIFO (Last – In, First – Out) data structure in python.
Insertion in stack is also known as a PUSH operation. Deletion from stack is also known as POP operation
in stack.
Working with stack is similar to working with list. Basic operations that we should know are :
1. How to create an empty stack?
2. How to add elements to a stack?
3. How to delete / remove elements from the stack
4. How to traverse or displaying elements of stack?
5. How to check for empty stack?
L = len(st)
for i in range(L-1, -1, -1) : #to display in reverse order
print(st[i])
Overflow: It refers to a situation, when one tries to push an element in stack/queue, that is full. In python
list OVERFLOW condition does not arise until all memory is exhausted because list can grow as per data.
Underflow: It refers to situation when one tries to pop/delete an item from an empty stack or queue.
Example:
List=[]
List.pop()
What is Queue:
Queue is a data structures that is based on First In First Out (FIFO) strategy. Elements are added at one end
called Rear and removed from other end called Front. In a queue, one end is always used to insert data
(enqueue) and the other is used to delete data (dequeue), because queue is open at both its ends.
Operations on Queue:
Enqueue: In this a new element is added at the end of list called Rear.
Is empty: It is used to check whether the queue has any element or not. It is used to avoid underflow
condition while performing Dequeue operation.
Is full: It is used to check whether any more elements can be added to the queue or not, to avoid the
overflow condition while performing Enqueue operation.
Peek: It is used to view element at the front and back of the queue, without removing it from the queue.
We can do this by: list[0] or list[-1]
L = len(st)
for i in range(0, L,)
: print(st[i])
Stack Queue
Stack is a data structures that is based on Last In Queue is a data structures that is based on First In
First Out (LIFO) strategy. First Out (FIFO) strategy.
Insertion and deletion of elements can be done Elements are added at one end called Rear
at one end only that id called TOP. and removed from other end called Front.
Insertion in stack is also known as a PUSH Insertion in Queue is also known as a Enqueue
operation. Deletion from stack is also known as operation. Deletion from Queue is also known
POP operation in stack. as Dequeue operation in stack.
Important Questions for Exams For Video Lecture Click Here:
Q1. Write a function push(),pop() and display() to add a new student name, remove a student name and
to display student names in stack format, according to Data Structure in Python.
st=[ ]
def push():
sn=input("Enter name of
student:") st.append(sn)
print(st)
def pop():
if st==[]:
print("Stack is
empty") else:
print("Deleted student name :",st.pop())
def display():
if st==[]:
print("Stack is
empty") else:
for i in st[::-1]:
print(i)
Q2. Write a function Push() which to add in a stack named "MyStack". After calling push() three times, a
message should be displayed "Stack is Full"
st=[ ]
StackSize=3
def
push():
sn=input("Enter name of
student:") if len(st)<StackSize:
st.append(sn)
else:
print("Stack is
full!") print(st)
Q3: Julie has created a dictionary containing names and marks as key value pairs of 6 students. Write a
program, with separate user defined functions to perform the following operations:
Push the keys (name of the student) of the dictionary into a stack, where the
corresponding value (marks) is greater than 75.
Pop and display the content of the stack.
def pop():
while True:
if st!=[]:
print(st.pop(),end="
") else:
break
Q4: Alam has a list containing 10 integers. You need to help him create a program with separate user
defined functions to perform the following operations based on this list.
Traverse the content of the list and push the even numbers into a stack.
Pop and display the content of the stack.
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
st=[]
def push():
for i in N:
if i%2==0:
st.append(i)
def pop():
while True:
if st!=[]:
print(st.pop(),end="
") else:
break
Q 5: Write a function push(l) to perform push opration in stack. Where a list is given:
L=[10,12,15,16,17,18,20,21,25]
Push only those element who is divisible by 5.
def push(l):
L2.append(l)
L2=[]
L1=[10,12,15,16,17,18,20,21,25]
for i in L1:
if i%5==0:
push(i)
print("Original List:\n",L1)
print("List after using push:\
n",L2)
Q 6: Write a function Addvowel(l) to perform push opration in stack. Where a list is given:
L=[‘Ansh’,’Vipin’,’Ishan’,’Devesh’,’Om’,’Upashna’]
Push only those element who is started from vowels
def
Addvowel(l)
:
L2.append(l)
L2=[]
L1=['Ansh','Vipin','Ishan','Devesh','Om','Upashna']
for i in L1:
if i[0] in
'aeiouAEIOU':
Addvowel(i)
print("Original List:\n",L1)
print("List after using push:\
n",L2)