Stack Practice Programs
Stack Practice Programs
def push(a,data):
a.append(data)
print("Element Pushed Successfully...")
def pop(a):
x=a.pop()
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)
N=[12,13,34,56,21,79,98,22,35,38]
def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[ ]:
return S.pop( )
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:
s.append(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)
st.pop(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:
( 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
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
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
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).
a) LIFO
b) FIFO
c) Both a and b
d) None of these
Ans (a) LIFO
7.
a)st.insert(10)
b)st.append(10)
c) st=10
d) st.extend(10)
Ans b)st.append(10)
a) Top
b) Front
c) Rear
d) Any
Ans (A) Top
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 st.append(name)
3 st.pop()
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")
st.append(sn)
def pop(st):
if(st==[]):
print("Stack is empty")
else:
print("Deleted Number is :",st.pop())
(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
6
(a) Print(st) (b) Print(st[:]) (c) Print(st[-1:]) (d) Print(st[-1::-1])