0% found this document useful (0 votes)
19 views

Stack

Uploaded by

ashigupta725
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
19 views

Stack

Uploaded by

ashigupta725
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
Mr. Rajeshwar has created a stack whose size is fixed for 10 elements and wants to perform some ‘operations on it. He wants to push certain elements and pop some elements from it. He is confused about the operations and how the clements will behave on pushing and popping? Chevrolet Suaiki Honda Mercedes RolaBoyce Help him to find the answers ofthe following questions. {) How many elements ean he push more to the stack? ws 3 4 wr (@) How many elements he needs to take out before “RolsRoyee” will come out? 2 ws wa (@t (ji) 1F3 elements are popped out and 2 pushed in what will be the strength of the stack? 2 4 oo @r (jv) IF"Tata" and “Datsun” are pushed to the stack respectively , which element will be popped ‘out as a result of a pop operation now? (a) Chevrolet (b) Datsun (e) Tata (€) RolsRoyce () 1f2 elements are popped out and 4 pushed into the Initial stack, how many times a loop needs to iterate to traverse the stack? ws (3 7 (oa Ans. (i) (b) Though the capacity of the stack i 10, so he ean push a maximum of elements i) (© Though there are elements above “RolsRoyee”, 10 4clemonts need to be taken ont (Gi) (6) Popping 3 clements means 2 remain, Now, pushing? elements means strength becomes 4 (ie) (b) Since “Datsun is pushed atthe end, twill be popped ont ist. (o) Since, there are 5 elements in stack and we ‘popped out 2 elements from i, then 3 elements remain, Now, we push 4 elements into stack, then total elements wre 7. Henee, a loop need to iterate to traverse the stack is 7 times. ial w Data Structure Data Structure is a way to enable users to manage, organize and store collections of data and perform operations on them accordingly. iia ¥ = tied snp BCCI has created a dictionary containing top players and their runs as key value pairs of cricket team. Write a program, with separate user defined functions to perform the following operations ‘© Push the keys (name of the players) of the dictionary into a stack, where the corresponding value (runs) is greater than 49 ‘* Pop and display the content of the stack. For example: Ifthe sample content of the dictionary is as follow: 'KAPIL":40, "SACHIN’:55, "SAURAV":80, "RAHUL":35, "YUVRAJ ‘The output from the program should be: SACHIN SAURAV YUVRAJ 10,} Sef PUsH(s,R) S. append (R) cet POR(s Lf Stet) eeturn §.pop() else: eturn none st=t1 SCORE=("KAPIL":40, "SACHIN":55, "SAURAV":80, "RAHUL":35, "YUVRAI":110, d for k Ln SCORE: AF SCORE [K]>4 DUSH(ST,k) while Tr Ae sTtst) print (POP(ST) ,end=" ") #pop and display break Vileram 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 lst * Traverse the content ofthe list and push the ODD 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: 13,21,89,35 det PUSH (S/N): 8 .append (N) def POP(s): ie St=t): return $.pop() sett) 12, 13, 34, 56, 21, 79, 98, 22,35, 38] PUSH (ST, k) is sti=t) print (POP (ST) ,ende! " break YASH MOTORS have a dictionary of top performer EMPLOYEES and their SALES of as key value pairs of COMPANY. Write a program, with separate user defined functions to perform. the following operations: ‘* Push the keys (name of the EMPLOYEE) of the dictionary into a stack, where the corresponding value (SALES) is greater than 500000. # Pop and display the content of the stack. For example: ary is as follows: 400000, "RAJEEV":350000, "MAYANK”: 50000, "RAHUL":1000000, } ‘The output from the program should be: SUNIL MAYANK RAHUL lee PUSH(STE,S): STK. append (S) et PoP (STR) 45 STK!= (1: eturn STK. popt) SALES={ *SUNT1" : 700000, "ROMIT" :400000, "RAJEEY":350000, °MAYANK" :750000, "RAHUL" :1000000, ) st=0 n SALES: ‘SALES [k]>500000: PUSE (ST, k) while True: ertety: print (POP(ST) ,end= Saroj have a list of 10 numbers . 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 numbers into a stack which are divisible by 5 Pop and display the content of the stack. For Example: Ifthe sample Content of the list is as follows N=[2,5,10,13,20,23,45,56,60,78) Sample Output of the code should be: 5,10,20,45,60 def push(st,num) : ST. append (nun) return ST.popQ N= (2,5,10,13,20,23,45,56,60,78] sT=(] for 4 in N: © 1e5e=0: push (ST, 1) print (pop(ST) ,end=' °) break A company having dictionary of various Departments and Number of computers (PC) available as key value pairs. Write a program, with separate user defined functions to perform the following operations: Push the keys (name of the Department) of the corresponding value (Number of PC) is 25 or more. Pop and display the content of the stack. For example: Ifthe sample content ofthe dictionary is as follows SETUP=("HR":10, "QUALITY":25, "SUPPORT":50, "PRODUCTION":20, "SUPPLY":25, } ‘The output from the program should be: QUALITY SUPPORT SUPPLY cef PUSH(ST,S) = ST.appena(s) jonary into a stack, where the det POP(ST) if 81 ST.pop() SETUP=("HR":10, "QUALITY" :25, "SUPPORT":50, "PRODUCTION" :20, SUPPLY" :25, ) st=0) for k in STUD: © SETUP[k] >=25: PUSH (ST, k) while true: af sTIst]: print (PoP(ST)) break A programmer wants to prepare a stack from given list of integer elements only for the numbers which are divisible by 3. Help him create a program with a user defined functions to perform the following operations based on this list. © Traverse the content of the list and push the numbers into a stack which are divisible by 3. ‘© Pop and display the content of the stack. For Example: Ifthe sample Content of the list is as follows: 5,10,13,21,23,45,56,60,78] Sample Output of the code should be: 3.21.45.60 ‘TRY OUT..

You might also like