Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
126 views

Stack Practice Programs

1. Stacks are a linear data structure that follows LIFO (Last In First Out) order for insertion and removal of elements. Common stack operations include push, pop, peek and display. 2. The document provides examples of programs implementing stacks using lists and dictionaries. Functions are defined for push, pop, peek and display operations. 3. Several examples are given demonstrating how to use stacks to store and retrieve elements from lists and dictionaries based on certain criteria like even/odd numbers or salary amount.

Uploaded by

Rad Brad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

Stack Practice Programs

1. Stacks are a linear data structure that follows LIFO (Last In First Out) order for insertion and removal of elements. Common stack operations include push, pop, peek and display. 2. The document provides examples of programs implementing stacks using lists and dictionaries. Functions are defined for push, pop, peek and display operations. 3. Several examples are given demonstrating how to use stacks to store and retrieve elements from lists and dictionaries based on certain criteria like even/odd numbers or salary amount.

Uploaded by

Rad Brad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Stack Practice Programs

1) Stack general operations using List Program.

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)

R={“OM”:76, “JAI”:45, “BOB”:89, “ALI”:65, “ANU”:90, “TOM”:82}


def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[ ]:
return S.pop( )
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):
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:

- 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


+& 5967 5 >96>76967 6>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 9 5
98  8998  98998  9
1!"
031
#30$%
#"& '%&(

) *4438 89+8+ 98 , 9898, 8+9  8,-8 8  <
 998# 98  8.98 89  ++  99  9
. 895
/2 9,898, 9+ 98 ,9898 . 9
 +-8   89 9801
/28++8,9 99998 
2 385
398 999+ 98 ,8.5

41&"456(%23765086
%4$365996
%!&%:65;86&%$!765<96=!:&%>65))8?
0 9 9 9  8 +@5

%4$3
%!&%:=!:&%>
1&
: 8889 98)89 = +9 898  8.9
8 89  ++  99  9. 89@8+99
/0 8- 9 99998+ 9177 @ 9898 
/28++8,9 99998 
2 "385
398499998.5
4A)B)<<09CB)D11;BB<9<;E

81 9 99 + +@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
 9 G'8  5

04AE IBF485
 
41&"5 2!
$
0

41&"AEH015 .0 5
2!
$
0 
0F4AE5
.0 5  9212
0+466

0F4AE5 5
 9212
0+466 @ 8
5

@ 8
B =%
$'101&
8-8+ 98 ,9   "'271=""
8+9 
%7"
8, <
-8 8 41'2% =# 98  8.98 89  ++  99  
9. 895
/2 9,89"'271=""9+ 98 ,9898 . 9
 +-8 
%7"
 89 98988888
/28++8,9 99998 
2 385
398 999+ 98 ,8.5

%7"
456
! 3765D888886&1$30650888886&%>"":65<988886'%=% (65D98888
6&%$!765)888888?
0 9 9 9  8 +@5
! 37'%=% (&%$!7
1&

8 J8-89)8 @ = +9 898  8.98 89  
++  99  9. 89@8+99
023456376896
86 8 896784 79896 637 8 4784
9
9436576

0 4 74896
86 8 896784

 346
 896746 86 8 89678747  7
!"#$%%&'%&(%$'%$(%)%*%*'%+,-
.46/88 896
679 6
%&'%$'%)%*'
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 3 8=/=.2>%6 "33>
 .2?"#- 676
3 8=/=.2>%6 "33> 364
676
364


( 0
4 945 B
8 43 543 7C64386 874 !63 
8637= > (
45446476546437D38643 B34%8976434867636  6 
8 78 
63 3896   B 6348 7
07989667= 46 896C64386 8> 896
8 43 8 4784
%9636896

3367  B546=!63  >7$ 3 36
0 4 74896
86 8 896784

 3646
 896746
86 8 896
8 43747  7
.24"23653&'%3E4012:3$%3.4/523'%35/C4 2/!3$'%3.41:3$%<
296 88 3 8963 B3479 6E4012:.4/52.41:
/5
03 B34634 878 364364784
 3 B56 78  86B63666 87  3896
6379
9436576(669
3648643 B348947636  6 
8 7
8 63 3896   B 6348 7476 89778
023456376896
86 8 896784 79896 637 8 4784
9
9436576
(
0 4 74896
86 8 896784

 346
 896746 86 8 89678747  7
!"#(%%&'%&(%$&%$(%)%*%*'%+,-
.46/88 896
679 6
(%$&%)%*'
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

58  4   
   4 21 5714
     4
21 5714 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'#+9 3* #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
4 5%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>
#+9 3* #45'#*99 *#4>'#*9 :9#46'? "1  94
"1  94 2% 18"9
2% 18"9 "1! 4
"1! 4   4
  4 516758 2

516758 2
 12314
12314 5167589
81
5167589
81 
 
5894

58:4 BB4
: 54   
   21 5714
21 5714   4
  4 586! 18" ##
586! 18" ## 12314
12314 $51%
$51%
 911"6%EFGHGIFJIKLHFHMKNHOKFJPGHJKGQRSITQMHIQHJKFLHUVHMQMHWTXITQMHYQQZGHYOHIEKHKMLHQWH B
'27
563 75
3131%3$
7/6'37-$

32 136
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  36 6 3 9 5354
 954
36 6 3 
78 8365 89593

39
 !"#6$8983693%&& &'()*8669# #56935 8+,3 3
-8
36*-5686*./)5+ 0 5986*159)86786*2586)567% 8*759()
0 5 # $3886%&257)3  45 9)56723 325699 .)# 037
5,66813&765)56678 86*)567 & &3 2&5675'/8
# 3& 4& 3 43*59 3&
79
*8669# #56935 8+,3 3-8
36*-5686*)
5+ 0 5986*159)86786*2586)567% 8*759)
0 5 # $3886%&257)3  45 9)56723 325699 
%7
75* 5+ 595 75+359&
5 
89 :3&67 3 
4 85 543*58 9455 
&97867&6 8369 343 3

386*345 8369959736 89


89 
0#5+9 36 6 3 
89 5674&9  6 89 5+86*
99 56' 5 986 35
9 5
02345677894
5  36 6 3 9 5
3654

 954
036 6 3 
89 89593

39
;<=>?@ABCDEFGHIJ>KEALMFNHIJ>OEBCPNHIJ>QACRHIJ>SERRTUFHIJ>VUWEXGHIJ>YTAXLHI>ZGXCFMH[
\54
%& 4& 3 379 3&
79
258 )]&84)^956):3&6*)_68 
`\  !"'()*8669./) 3dg6
&875 g)g25&* g)g258 g)g]&84g) 
5+ ()0 5 .)# 037 g75443 g)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-89 de
-89 de 38863
3886 8
6a8bh'
8d8ef. 2^\2a3-89 )8b
2^\2a-89 )8b  8
#&
 8
#& 83-89 cde
8-89 cde 486 a2%2a3-89 b)67b
486 a2%2a-89 b)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)st.insert(10)

b)st.append(10)

c) st=10
d) st.extend(10)

Ans b)st.append(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 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

(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

You might also like