7 Data Structure Stack Questions
7 Data Structure Stack Questions
7 Data Structure Stack Questions
DATA STRUCTURE
STACK (PUSH + POP)
LINEAR
x =[1,2,3,5,7,89,3,62,34,13,5, 7, 9]
data = 89; found =0
for i in x:
if i ==data:
found =1
if(found ==0):
print("Data not found")
else:
print("Data found")
BINARY
x =[1,2,3,5,7,8,9,10,12,14,15,18,20,22] if(x[mid]>data):
data = 9; found =0 last = mid-1
first=0; last =13 if(found ==0):
while (first<=last and found ==0): print("Data not found")
mid = int((first+last)/2) else:
print("M : ",mid," ,F: ", first ,",L: ",last) print("Data found")
if(x[mid]==data):
found =1
if(x[mid]<data):
first=mid+1
I: DATA=[1,2,3,4,5,6,7]
print("D:",DATA)
length = len(DATA)
print( "Len:", length)
II: e=int(input("e= ")) #12
III: DATA.append(None)
IV: END
le=len(DATA)
print("Len:", le)
print("D:",DATA)
DATA[le-1]=e
print("Len:", le)
print("D:",DATA)
IV: SORTED
D: [11, 22, 43, 44, 55, 61, 77]
e=int(input("e= ")) #47
DATA.append(None)
pos=0; le =len(DATA)
for i in range(le):
if DATA[i]>e:
pos=i; break
print("Pos:",pos)
for i in range(le-1,pos,-1):
DATA[i]=DATA[i-1]
DATA[pos]=e
print("Len:", len(DATA))
print ("D:", DATA)
OPERATIONS ON STACK
As explained in the previous section, a STACK is a mechanism that implements LIFO arrangement
hence elements are added and deleted from the STACK at one end only. The end from which
elements are added or deleted is called TOP
of the STACK. Two fundamental
operations performed on the STACK are
PUSH and POP. In this section, we will
learn about them and implement them using
Python. PUSH adds a new element at the
TOP of the STACK. It is an insertion
operation. We can add elements to a
STACK until it is full. A STACK is full when
no more elements can be added to it. Trying
to add an element to a full STACK results in
an exception called
‘overflow’.
POP operation is used to remove the top
most element of the STACK, that is, the
Page 4 of 22 EDUCATION FOR EVERYONE
DotPyEdu PLAY WITH PYTHON
element at the TOP of the STACK. It is a delete operation. We can delete elements from a STACK
until it is empty i.e. there is no element in it. Trying to delete an element from an empty STACK
results in an exception called ‘underflow’.
1. Suppose STACK is allocated 6 memory denotes an empty location. Display the STACK S as
locations and initially STACK is empty (Top = the following operations
0). take place :
Give the output of the program segment : PUSH(S, Athens);
aaa = 2 bbb = 5 POP(S,ITEM);
push(STACK,aaa); POP(S,ITEM);
push(STACK,4); PUSH(S, Madrid);
push(STACK,bbb +2); PUSH(S, Moscow);
push(STACK,9); POP(S,ITEM);
push(STACK,aaa+bbb); Answer:
while Top > 0: 2. (i) London, Berlin, Rome, Paris, _______, _______.
pop(STACK,ITEM) (ii) PUSH(S, Athens)
print(ITEM) London, Berlin, Rome, Paris, Athens, _______.
Answer: (iii) POP(S, ITEM)
The output is : London, Berlin, Rome, Paris, _______, _______.
7 (iv) POP(S, ITEM)
9 London, Berlin, Rome, _______, _______, _______.
7 (v) PUSH(S, Madrid)
4 London, Berlin, Rome, Madrid, _______, _________.
2 (vi) PUSH(S, Moscow)
2. Consider the STACK S containing city names : London, Berlin, Rome, Madrid, Moscow, _________.
London, Berlin, Rome, Paris, ______, ______ (vii)POP(S, ITEM)
one name per location and where ______ London, Berlin, Rome, Madrid, _________,
_________.
Page 10 of 22 EDUCATION FOR EVERYONE
DotPyEdu PLAY WITH PYTHON
QUESTIONS SET 1
Q 1. Write a function in python named PUSH (STACK, SET) where STACK is list of some numbers forming a stack
and SET is a list of some numbers. The function will push all the ODD elements from the SET into a STACK
implemented by using a list. Display the stack after push operation.
For Example:
If the sample Content of the list is as follows: N = [12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
Sample Output of the code should be: 13 21 79 35
Q 2. Write a function in python named POP (STACK) where STACK is a stack implemented by a list of numbers.
The function will display the popped element after function call.
Q 3. Write push (rollno) and pop () method in python: Push (rollno) –add 5 roll number in Stack, pop ()- remove 2
roll number from Stack.
QUESTIONS SET 2
Q 1. Write a function in python named PUSH (STACK, ITEMS) where STACK is list of some +VE numbers forming a
stack and ITEMS is a list value as numbers. The function will push all the +ve elements into STACK implemented by
PUSH operation. Display the stack after push operation.
For Example:
If the sample Content of the list is as follows: N= [12, -13, 34, 56, -21, -79, 98, 22, -35, 38]
Sample Output of the code should be: [38, 22, 98, 56, 34, 12]
Q 9. Write a function PUSH AddBookNames (Stack, Book_Name_List) in Python to add a new Book Name from
Books_Name_List , where Book names Length more than 10.
For Example: If the sample Content of the list is as follows:
Books_Name_List = ['Lost Time' , 'Ulysses' , 'Don Quixote' , 'The Great Gatsby', 'Madame Bovary', 'Moby Dick'
,'War and Peace' ]
Sample Output of the code should be:
Q 8. Write a function PUSH AddBookNames (Stack, Books_Name_List) in Python to add a new Book Name from
Books_Name_List, where Book names Length less than 10.
For Example: If the sample Content of the list is as follows:
Books_Name_List =
['Lost Time' , 'Ulysses' , 'Don Quixote' , , 'The Great Gatsby', 'Madame Bovary', 'Moby Dick' ,'War and Peace' ]
Sample Output of the code should be: ['Lost Time ' , 'Ulysses ' , 'Moby Dick']
def PUSH AddBookNames (STACK, Book_Name_List):
for s in Book_Name_List:
if len(s) <10 :
STACK.append(s)
def DISPLAY(STACK):
if len(STACK)==0:
print("Empty Stack")
else:
print(STACK)
Q 9. Write a function PUSH AddBookNames (Stack, Book_Name_List) in Python to add a new Book Name from
Books_Name_List , where Book names Length more than 10.
For Example: If the sample Content of the list is as follows:
Books_Name_List = ['Lost Time' , 'Ulysses' , 'Don Quixote' , 'The Great Gatsby', 'Madame Bovary', 'Moby Dick'
,'War and Peace' ]
Sample Output of the code should be:
Page 21 of 22 EDUCATION FOR EVERYONE
DotPyEdu PLAY WITH PYTHON
['Don Quixote', 'The Great Gatsby', 'Madame Bovary', 'War and Peace']
def PUSH AddBookNames (STACK, Book_Name_List):
for s in Book_Name_List:
if len(s) >10 :
STACK.append(s)
def DISPLAY(STACK):
if len(STACK)==0:
print("Empty Stack")
else:
print(STACK)
Q 10. Reena 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 Values (marks 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.
For example: Sample Pap
For example: If the sample content of the dictionary is as follows:
R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}
The output from the program should be: [76, 89, 90, 82]
def PUSH(STACK, R):
for k in R.keys():
if R[k] >=75:
S.append(R[k])
def DISPLAY(STACK):
if len(STACK)==0:
print("Empty Stack")
else:
print(STACK)
Q 11. Write PushBook (Book, R) and PopBook (Book) methods/functions in Python to add Btitle have character size
4 only in Stack Book from R is a Book with BNo and Btitle and delete a Book from a List of Book List by BNo,
considering them to act as push and pop operations of the Stack data structure.
For example: Book,
If the sample content of the dictionary is as follows: R={3240:"C++",4051: "Python",1089: "JAVA",65: "HTML":90,
9801:"PHP",1010:"PHP+"}
The output from the program should be: ["HTML", "PHP+"]
def PUSH(Book, R):
for k in R.keys():
n=R[k]
if len(n)==4:
S.append(R[k])
def DISPLAY(STACK):
if len(STACK)==0:
print("Empty Stack")
else:
print(STACK)