Stack
Stack
5
the list. rusn : Insertion of new element (from the
(10, 15, 20,30,15, 23,34] in stack. beginning)
(a) remove() : It scarches the given clement(as a
List
matching
List = [10, 15,20,,30, 15,23,34] element. L21Push(2)SPush(5) Push(4)
4Top
5-Top
For example: [Link](15), 2
CHAPTER
The output will be [10,20,30,15,23,341
(b) pop):It accepts the index number (optional) as Pop: Deletion of element( from the beginning) in
stack.
TOPICS COVERED and removes the element ín the list.
arguments
Assertion:
Stacks are uscd in the List [Link]() 25. Python program to generate groups of 6 consecutive
hgeda
af
Computer
Science
commonly
usedto
i m p l e m e n t
15.
recursion.
following
the a stack Output:
Which of on
7. pop)
operations
Science
Book. append (name)
Computer
e def pop(Book):
118
Tagtker
choice: ")}) Data Structure: Stack 119
your
choice-int(input("Enter
at
if Book== ): # main
if (choicez1): end\n 2.
Insert
print ("Under Flow") Enp=(] [Link] (N)
Insert at the Players=[)
print("1.
value") while True:
Index else: P=['AMAR', 'GAGAN', "KAMAL
particular choice")) nrint("\n1. Push\n2. Pop \n3. Display\ JATIN' 'LALAN' ]
the (input('Enter
your print("Book Deleted",Book. pop) n4. Exit") for i in range(0, len(P),2):
n=int
if n=l: for
insertion:")
def display (Book) : ch=int(input("Enter your choice") push(Players, P[i])
any number
a-input("Enter
if Book=z[): if ch==1: prínt (Players)
S. append(a)
print ("Underflow! !!") push(Emp)
elif n==2: insertion:") 35. Write a function pop) to delete the element from the
for elif ch==2:
a=input("Enter any number else: stack created in the previous question and provide
Index")) print("Books in Stack")
("Enter the pop(Emp) the given output. Also write the function calling
ind=int (input
if len(s)<ind : l=len(Book) elif ch==3: statement.
exist, Will add at
print ("Index not for i in range(1-1, -1,-1): display (Emp) LALAN
else:
the last")
S. append(a)
print (Book[i),"<=", end="") KAMAL
break
# main
else:
[Link](ind, a) Book-[) 33. #Write the functions push and display implemented AMAR
elif (choices=2): while True: through stack. ASsuming the given list insert the Stack is Emptied
elements into the stacks which start from A.
if (s=()): DrÍnt("\n1. Push\n2. Pop \n3. Displayn4. Erien Ans. def pop(Players):
print ("List Empty") Ans. l=["Amit", "Abhinav","Deb", "Raj", "Ankit", "Naveen"]
ch=int(input ("Enter your choice")) if Players-- []:
else:
print("1. Deletion from the end\n 2.
if ch==1: st=[) return "Stack is Emptied"
push (Book) def push(st, 1): else:
Deletion of particular Value") for i in l:
n=int (input("Enter your choice")) elifch==2: return Players. pop()
if i[0]=-'A': for i in range(len (Players) ·1) :
if n==1: pop (Book)
print ("Deleted element is: [Link](1) print (pop (Players) )
elifch==3:
",[Link]() def display (st) :
display (Book) 36. Assuming the given dictionary, Write the function to
else: if st--[]:
else: print ("underflow")s6 push add new element in the stack. Add only those
de=input("Enter the element to
be removed") break else:0 element from the dictonary whose value consist of
[Link] (de) for i in range(len(st) -1,-1,-1): 'A, 'E' or 'I.
32. Write a menu driven python program using function Ans. d={"INDIA": "DELHI", "PAKISTAN":"LAHORE"}
elif (choice--3): print (st[i])
l-len(s) Push (), Pop ) and Display) to implement the stack push(st, 1) c=[]
for i in range(0,1): The program will store the Employee details ie def push(c):
display(st)
print(s[i]) Employee number, Employee name and Salary. for i in d:
else: 34. Write a function push) to implement the stack and if "A in d[i] or "E" in d[i] or 'I' in d[il:
Ans. def push(Emp): add the alternate element of list to the stack. Also
print("Wrong Input") [Link](d[i])
C=input ("Do you want to continue or not? ") eno=int (input("Enter the Empolyee Number")) write the function calling statement.
print(c)
30. Write Push(Book) and Pop(Book) methods in ename=input ("Enter the Employee Name") Ans. def
to Add a new Book and Remove a Book Python push(c)
from a List sal=input ("Enter the Salary") push(Players,N):
of Books, considering them to act as PUSH
operations of the data structure Stack.
and POP Emp. append( [eno, ename, sal])
Ans. def push(Book): def pop(Emp): PRACTICE OUESTION BANK
a=input("Enter any number :") if Emp=-[0: 4, Choose the correct output from the given output options
print("Under Flow") I. Write the single statemnent that reverse the list.
[Link](a) for the following code.
def pop(Book): else: bo Write a Python program to select the even items of a a= (1, 2, 3, 4, 5]
if (Book==)): list
Print ("Enployee Record Deleted",[Link])) for i in range(1, 5):
print ("Stack Empty") def display(Emp): 3. What is the output of the following code?
else: a[i-1] a[i)
print ("Deleted element if Emp==[): def unpack(a, b, c, d): for i in range(0, 5):
31. Write a menu
driven python is:",[Link]()) print("Underflow!!!") print(a+d) print (a[i],end = " ")
Push), Pop) and Display) toprogram using function else: X = [1,2.3.4] i. 5 5 1 2 3
The program will implement the stack. unpack(*x)
Ans. def push(Book): store the name of the books. print("Employee record in Stack\n i. Error
ii. 5 1 2 3 4
1-len(Emp) iii. 2 3 4 5 1
name=input("Enter the book name" for i in ii, [1,4].
range(1-1, -1,-1): iii. [5]. iv. 2 3 45 5
print (Enp[i]) iv. 5
(Pthon)-12
agetker uctk Computer Science
9. Assertion (A): The time complexity
of push
120 using an P
code?
of the following onerations on a stack implemented
5. What is the output
def change (var, lst): O(1).
var = 1 Reason (R): The push and pop operations on an array take
lst[0] = 44 execute, hence O(1) time
k =3
constant time to
[1,2,3]
iii. 3 Long Answer
[1,2,3].
Type Questionsatg 5 Marks
iv. 1
[44,2,3]. PUSH(insert) operation in stak
given program: 11. Write algorithm for
6. Write the output for the operation in stack
a=[10,20,30,40, 50) 12. Write algorithm for POP(Remove)
as parameter and
for i in range (0, 5) : 13. WAP using Function to take an list
a[i]"-5 elemennt.
print Second largest
print(a) will store the student information
statements: 14. Write a program which
7. Write the output for the given list as follows.
a-[10,20, 30, 40, 50] in the form of a nested
number, avg marks )
i. print (a[0) *for first item (ie: student name, roll
item
ii. print (a[-1]) #FoI Last Example:
iii. print (a[1:]) #FoI Printing the list having 77.5),("Sonali", 103,
2 i.e. student=[ ["Rahul", 100,
all the items from position no. ["Pravit", 102,
a1] onwards 35.5], ["Hrisikesh", 101, 88.0],
having 87.5], ["Tanmaya", 103, 45.5] ]
iv. print (a:1]) #For printing the list initialized the list and
number 1 i.e Your program will
all the items upto position details, those are poor
upto a[o] display the students
From performer (Secured less
than 50% as average
V. print (al:3]) #For printing the List
a[o] to a[2] mark
print (a[1:4]) #For printing the list write a function to
vi. 15. Given a list of numbers (integers),second minimum in
starting from a[1] to a[3] return the second maximum
and
list
vii. print (a[4:1: -1]) #For printing the this list.
fIOm a[4] to a[2]
in reverse starting contains numbers separaleu
viii.
print(a[:0:-1]) #For printing the list in Input: The first line
reverse order upto a[1] by a space.
miniu"
ix. print(a[::-1]) #For printing the entire Output: Print second
maximum and second
list in reverse order
separated by a space
8. Assertion (A): A stack implemented using a linked list Example:
has a fixed size.
Input: 2,7,5,9,1,6
Reason (R): Linked lists are not resizable, hence the
Output: 2 7
stack imnplemented using linked list has a fixed size.