Stack Practice Programs
1) Stack general operations using List Program.
def push(a,data):
[Link](data)
print("Element Pushed Successfully...")
def pop(a):
x=[Link]()
print("Popped Element=",x)
def peek(a):
print("Peek Element=",a[-1])
def display(a):
for i in range(len(a)-1,-1,-1):
print(a[i])
#__main__
a=[]
while True:
choice=int(input("1->Push\n2->Pop\n3->Peek\n4->Display All\n5>Exit\nEnter
Your Choice:"))
if choice==1:
data=int(input("Enter Value to Push:"))
push(a,data)
elif choice==2:
if len(a)==0:
print("Stack Underflow...")
else:
pop(a)
elif choice==3:
if len(a)==0:
print("Stack Underflow...")
else:
peek(a)
elif choice==4:
if len(a)==0:
print("Stack Underflow...")
else:
display(a)
elif choice==5:
break
else:
print("Bewakoofi wali choice...")
2. 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: (3)
* 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.
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: TOM ANU BOB OM
Ans)
R={“OM”:76, “JAI”:45, “BOB”:89, “ALI”:65, “ANU”:90, “TOM”:82}
def PUSH(S,N):
[Link](N)
def POP(S):
if S!=[ ]:
return [Link]( )
else:
return None
#__main__
ST=[ ]
for k in R:
if R[k]>=75:
PUSH(ST,k)
while True:
if ST!=[ ]:
print (POP(ST), end=” “)
else:
break
3. Alarm 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.
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
N=[12,13,34,56,21,79,98,22,35,38]
def PUSH(S,N):
[Link](N)
def POP(S):
if S!=[ ]:
return [Link]( )
else:
return None
ST=[ ]
for k in N:
if k%2= =0:
PUSH(ST,k)
while True
if ST!=[ ]:
print(POP(ST), end=” “)
else:
break
4. Write a function in Python PUSH(Arr), where Arr is a list of numbers. From this
list push all numbers divisible by 5 into a stack implemented by using a list.
Display the stack if it has at least one element, otherwise display appropriate error
message.
Ans)
def PUSH(Arr,value):
s=[ ]
for x in range(0,len(Arr)):
if Arr[x]%5==0:
[Link](Arr[x])
if len(s)==0:
print("Empty Stack")
else:
print(s)
5. Write a function in Python POP(Arr), where Arr is a stack implemented by a list
of numbers. The function returns the value deleted from the stack. Ans)
def popStack(st) :
# If stack is empty
if len(st)==0:
print("Underflow")
else:
L = len(st)
val=st[L-1]
print(val)
[Link](L-1)
6. Siya has created a dictionary containing names and salary as key value pairs of 5
employees. Write a program, with seperate user defined functions to perform the
operation as Push the keys(name of the employee) of the dictionary into a stack,
where the corresponding value (salary) is greater than 25000 and also perform the
operation as Pop and display the content of the stack.
For example:
If the sample content of the dictionary is as follows:
details={"Rahul":25000,"Jai":45000,"Kinshu":20000,"Riya*:18000,"Anu":28000}
The output from the program should be:
Anu Rahul
7. Aravind has a message (string) which has a upper case alphabets in it. Write a
program, with separate user defined functions to perform the following operations:
- Push the upper case alphabets in the string into a stack.
- Pop and display the content of the stack.
For example:
If the message is "All the Best, for your Best Performance"
The output from the program should be:
PBBA
(OR)
Ajay has a list containing 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 all positive numbers into a stack.
-Pop and display the content of the stack.
For Example:
If the sample Content of the list is as follows: N=1-2, 131,-34, 56, 21, -379, 98, 22,
35, 38]
Sample Output of the code should be: 38 35 98 21 56 131
8 . Write a program to create a Stack for storing only odd numbers out of all the
numbers entered by the user. Display the content out of the Stack along with the
largest odd numbers in the Stack. ?
01235
6789
!
"
#!$
%$&7%'(697&)96*+&)(, -+,.
/ 01
23
"
$ 4
+&
"
9
59677
6
77!
7$$"
"
4 "
7
$ 4
+& +7
#"7
3
87
#!$6#"
7
28" !
7 !
7
2$
12"!3!
#
#8
2
!!3#"#"7
#!$
9 &
32"7
7#1
7""83 $ 4
+& 5
&:5
6!
7
;
!7$ ;
!7"!!37
+
2
% 0
6,((!7!
< (=
7"!!3#59676967 4
+& 59675
>96>769676>96>7
? "
$ 4
+& +
7
!!3
77#
7 1#!
!
7$
(12
!
777#"
@!2!
8
17!
3
"
$
A )
!!
8!7
$ 4
+& 6
B
6
1
!9!
#
C )
3
B
D 4
+& /$8'*6
77"! 3
8'*
$97#678
$
4$878, 1
!"! 3
878
$9
!7#678
$
E )
!# !
7878
$ 4
+& 8'* 8
/$
/$
4$9
!F3>/$ 4$9"1
!">/#9!
9$93! $ #<
<$9 82$ YKLMNOPQLRZQ[\]L^U_Q`XL[PWLabLQbLVQT_Lc
?$
GHI3! <$!7 I
GH
JKLMNOPQLRSQTULSPVTNQTWX ?$ 82/
C$ JKL_NOPQLRSQTULdTeTQTWX
C$
f 01"3
!
"
$ 4
+& (=1
!
g
##
!
2#"7#
=
1
#$
- 2
# > 1 "7!
#
#"
$
/h (1
!
"!!3#"=
"=3
"
"12 4
$/44C9<4h>i?i
+& 92 8!
/4 /4
4 /44
j A
9< A9<
4h A9<4h
> A/<
i 4h
? 4h?
i
4?
0123457898
9
95
98
8998
98998
9
1!"
031
#30$%
#"& '%&(
) *4438
89+8+
98,
9898,8+98,-88 <
998#988.9889++
999
.895
/29,898,9+
98,9898
.9
+-8899801
/28++8,9
99998
2385
398
999+
98,8.5
41&"456(%23765086
%4$365996
%!&%:65;86&%$!765<96=!:&%>65))8?
09998+@5
%4$3
%!&%:=!:&%>
1&
:8889
98)89=+9
8988.9
889++
999.89@8+99
/08-9
99998+9177@9898
/28++8,9
99998
2"385
398499998.5
4A)B)<<09CB)D11;BB<9<;E
81999
++@5
)<B);1<9
%
41&"456(%23765086
%4$36599 4A)B)<<09CB)D11;BB<9<;E
6
%!&%:65;86&%$!765<96=!:&%>65))8 +2!
$
5
?
8+
+2!
$
&5 +212
5
8+&
F4AE5
+212
5 9
F4AE5 5
9
9
5
04AE
9G'8 5
04AE IBF485
41&"5 2!
$
0
41&"AEH015 .05
2!
$
0
0F4AE5
.05 9212
0+466
0F4AE5 5
9212
0+466 @8
5
@8
B =%
$'101&
8-8+
98,9"'271=""
8+9
%7"
8, <
-8841'2% =#988.9889++
99
9.895
/29,89"'271=""9+
98,9898
.9
+-8
%7"
8998988888
/28++8,9
99998
2385
398
999+
98,8.5
%7"
456
! 3765D888886&1$30650888886&%>"":65<988886'%=% (65D98888
6&%$!765)888888?
09998+@5
! 37'%=% (&%$!7
1&
8J8-89)8@=+9
8988.9889
++
999.89@8+99
023456376896
868
8967847989663784784
9
9436576
0474896
868
896784
346
896746868
89678747
7
!"#$%%&'%&(%$'%$(%)%*%*'%+,-
.46/88
896
6796
%&'%$'%)%*'
0!. .01."23.4!13+'''''% !"#$%%&'%&(%$'%$(%)%*%*'%+,-
35/623)'''''%350783(''''% 6
4.6=.%!>
390:0!;3+''''%3506413&''''''%< .46=!>
6
4.6=.2;%.> 6
/=.>
.2;46=.>
.?"#-
6
/=.2;> 3683.=>
.2;?"#- 676
3683.2;=> 3683!6
676 .2"#-
3683!6
3!
.2"#-
A""'
3.01. 4.6=.2%>
.01.#-@''''' 96236
4.6=.2%>
.2?"#-
96236 38=/=.2>%6"33>
.2?"#- 676
38=/=.2>%6"33> 364
676
364
( 0
4945B
843
5437C64386874!63
8637= > (
45446476546437D38643B34%8976434867636
6
878
63
3896
B63487
07989667=46
896C643868>
896
84384784
%9636896
3367B546=!63
>7$336
0474896
868
896784
3646
896746
868
896
843747
7
.24"23653&'%3E4012:3$%3.4/523'%35/C4 2/!3$'%3.41:3$%<
29688
38963B34796E4012:.4/52.41:
/5
03B34634878364364784
3B5678
86B6366687
3896
6379
9436576(669
3648643B348947636
6
87
863
3896
B6348747689778
023456376896
868
8967847989663784784
9
9436576
(
0474896
868
896784
346
896746868
89678747
7
!"#(%%&'%&(%$&%$(%)%*%*'%+,-
.46/88
896
6796
(%$&%)%*'
0!. .24"23653&'%3E4012:3$% !"#(%%&'%&(%$&%$(%)%*%*'%+,-
3.4/523'%35/C4 2/!3$'% 6
4.6=.%!>
3.41:3$%< .46=!>
6
4.6=.2;%.> 6
/=.>
.2;46=.>
.?"#-
6
/=.2;> 3683.=>
.2;?"#- 676
3683.2;=> 3683!6
12314
5167589
81
5894
&
'4
584
4 215714
4
215714 586!18"
##
4 12314
586!18"
## $51%
12314
$51%
( )*+,%3-51%61"%"-6
8%5.-
86%88/8%013%8"0%53%31.1%271 %53
B
367"186323561% 5
/5%0631%5%617315"181"78-6
836
15
5061
22
8/
15%6
834
473611.38%01
61367"186
61"-6
8%5.86
%36%-15161
-
5513
8"8/1%2710%53%510
516%8562
4
%8""32%.61-
86186
6136%-27
518%0 2149613%0 21-
86186
61
"-6
8%5.3%3
22
34
:
;#:*<#45'#!=#4'#+93*#45'#*99*#4>'#*9:9#46'?
1
76765
061 5
/5%03
72"$14
*99**9:9
!:
*2%0%3%236-
86%88/@'367"18630%532A
7811"6
120-51%61% 5
/5%06
31%5%617315"181"78-6
836
15
5061
22
8/
15%6
83$%31"
8632362
45%1153161-
86186
61236%8" 7361870$153/156%8BB86
%36%-2
4
%8""32%.61-
86186
6136%-2
7
58%0 214
9613%0 21C
86186
612363%3
22
34
9
@@BB(D@566>BB>
%0 21!7676
61-
"13
72"$14
B(D566>BB>
*9 :
;#:*<#45'#!=#4' 9
@@BB(D@566>BB>
#+93*#45'#*99*#4>'#*9:9#46'? "194
"194 2% 18"9
2% 18"9 "1!4
"1!4
4
4 5167582
5167582
12314
12314 5167589
81
5167589
81
5894
58:4 BB4
:
54
215714
215714
4
4 586!18"
##
586!18"
## 12314
12314 $51%
$51%
911"6%EFGHGIFJIKLHFHMKNHOKFJPGHJKGQRSITQMHIQHJKFLHUVHMQMHWTXITQMHYQQZGHYOHIEKHKMLHQWH B
'27
563 75
3131%3$
7/6'37-$
32136
51361"%6%8%"-6
8%5.%3
$
[8%015%68/%31.1%271 %521215
76%$66
5/%8\115-
221-6
8612
6%-,%6%657-67512
3561% 5
/5%0631%5%617315"181"78-6
836
15
5061
22
8/
15%6
834
473611.3$
[8%01
61"-6
8%5.86
%36%-15161-
5513
8"8/1%271
5%68/%510
516%8>2
02345677894
5
3663
95354
954
3663
78836589593
39
!"#6$8983693%&&&'()*8669#
#569358+,33
-8
36*-5686*./)5+0
5986*159)86786*2586)567%
8*759()
0
5#
$3886%&257)3
459)56723325699.)#
037
5,66813&765)5667886*)567
&&3
2&5675'/8
#
3&4&3
43*59
3&
79
*8669#
#569358+,33-8
36*-5686*)
5+0
5986*159)86786*2586)567%
8*759)
0
5#
$3886%&257)3
459)56723325699
%7
75*
5+
59575+359&
5
89:3&673
4
85543*58
9455
&97867&68369343
3
386*3458369959736
89
89
0#5+9
3663
895674&9
689
5+86*
99
56'
598635
95
02345677894
5
3663
95
3654
954
03663
8989593
39
;<=>?@ABCDEFGHIJ>KEALMFNHIJ>OEBCPNHIJ>QACRHIJ>SERRTUFHIJ>VUWEXGHIJ>YTAXLHI>ZGXCFMH[
\54
%&4&3
379
3&
79
258)]&84)^956):3&6*)_68
`\ !"'()*8669./) 3dg6
&875g)g25&*
g)g258g)g]&84g)
5+()0
5.)#
037 g75443g)g^956g)g:3&6*g)g_68
ge
5'/8 72^\2a\)3b
72^\2a\)`b \54467a3b
\54467a`b 72%2a\b
72%2a\b 8\cde
8\cde &6\434ab
&6\434ab
9
9 &6`36
&6`36 3-89de
-89de 38863
3886 8
6a8bh'
8d8ef. 2^\2a3-89)8b
2^\2a-89)8b
8
#&
8
#& 83-89cde
8-89cde 486a2%2a3-89b)67b
486a2%2a-89b)67b
9
9 95
95
MULTIPLE CHOICE QUESTIONS
1. Process of inserting an element in stack is called ____________
a) Create
b) Push
c) Evaluation
d) Pop
Answer: b
Explanation: Push operation allows users to insert elements in the stack. If the stack is filled
completely and trying to perform push operation stack overflow can happen.
2. Process of removing an element from stack is called __________
a) Create
b) Push
c) Evaluation
d) Pop
Answer: d
Explanation: Elements in the stack are removed using pop operation. Pop operation
removes the top most element in the stack i.e. last entered element.
3. In a stack, if a user tries to remove an element from an empty stack it is called
_________
a) Underflow
b) Empty collection
c) Overflow
d) Garbage Collection
Answer: a
Explanation: Underflow occurs when the user performs a pop operation on an empty stack.
Overflow occurs when the stack is full and the user performs a push operation. Garbage
Collection is used to recover the memory occupied by objects that are no longer used.
4. Pushing an element into stack already having five elements and stack size of 5, then
stack becomes ___________
a)Over flow
b)Crash
c)Under flow
d)User flow
Answer: a
Explanation: The stack is filled with 5 elements and pushing one more element causes a
stack overflow. This results in overwriting memory, code and loss of unsaved work on the
computer.
4
5.
a) A collection of stacks is sortable
c) The entries are stored in a linked list
d) There is a Sequential entry that is one by one
Answer: d
Explanation: In stack data structure, elements are added one by one using push
operation. Stack follows LIFO Principle i.e. Last In First Out(LIFO).
6. ______ form of access is used to add/remove nodes from a stack.
a) LIFO
b) FIFO
c) Both a and b
d) None of these
Ans (a) LIFO
7.
a)[Link](10)
b)[Link](10)
c) st=10
d) [Link](10)
Ans b)[Link](10)
8. Stack follow the principle of
a) Last in first out
b) First come first serve
c) Random
d) Sorted data
Ans (a) Last in first out
9. In stack all insertions take place at ____ end(s).
a) Top
b) Front
c) Rear
d) Any
Ans (A) Top
10. Data structure stack is also known as ____________ list.
a) Ordered List
b) Random List
5
c) FIFO list
d) LIFO list
Ans (D) LIFO list
CASE STUDY BASED QUESTIONS:
Q1 KV2 Jammu Cantt has planned to keep record in the form of list but the requirement is
to keep the data in the following manner: The name of the stack is st[ ]
1. To keep the data in such a manner so that the last data would come out first, for that
data can be stored in which data type.
2. Write a code to add name of student in the list stack. (Take variable of your choice)
3. Write a code to delete name of student from the list stack. (Take variable of your
choice)
4. Write a code to display all the name of students from the list stack.
Answer: 1 Stack
2 [Link](name)
3 [Link]()
4 print(st[::-1])
Q2. Write a function push(number) and pop(number) to add a number (Accepted from the
user) and remove a number from a list of numbers, considering them act as PUSH and POP
operations of Data Structure in Python.
st=[ 5]
def push(st):
sn=input("Enter any Number")
[Link](sn)
def pop(st):
if(st==[]):
print("Stack is empty")
else:
print("Deleted Number is :",[Link]())
(i) If push function is called twice how many elements would be added in list.
(a) 2 (b) none (c) 1 (d) 3
Answer: (i) (d) 3
(ii) If push function will be called with value 10, what will be printed with print(st)
(a) [5,10] (b) [10,5] (c) [ ] (d) 5,10
Answer: (ii) [5,10]
(iii)If pop function is called without the call to the push function what will happen
(a) Element will be deleted (b) nothing happened (c)
("Stack is empty") will be printed (d) complete stack will be
deleted.
Answer: (iii) (a) Element will be deleted
(iv) To print list elements as stack the st will be printed as:
6
(a) Print(st) (b) Print(st[:]) (c) Print(st[-1:]) (d) Print(st[-1::-1])
(i) Both(a) and (d)
(ii) All the choices
(iii) Only d
(iv) Both b and c
Answer: (iii) Only d