Data STR
Data STR
1.
def f_l(l, v):
if v in l:
return l.index(v)
else:
return -1
def pop(stack):
if not stack:
print("Stack is empty, nothing to pop")
else:
element = stack.pop()
print(f"{element} popped from stack")
stack = []
while True:
print("\nChoose an operation:")
print("1. Push element to stack")
print("2. Pop element from stack")
print("3. Exit")
choice = int(input("Enter your choice (1/2/3):
"))
if choice == 1:
element = input("Enter element to push: ")
push(stack, element)
elif choice == 2:
pop(stack)
elif choice == 3:
print("Exiting program.")
break
else:
print("Invalid choice, please choose again.")
4.
def r_d(t):
s = []
for c in t:
s.append(c)
r = ''
while s:
c = s.pop()
r += c * 2
return r
# Example usage
s = [10, 20, 30, 40]
v = p(s)
print("Popped value:", v)
print("Updated stack:", s)
6.
def push(item, stack):
stack += [item]
return stack
def pop(stack):
if stack:
item = stack[-1]
stack.remove(item)
return item
stack = [n for n in range(1, 11)]
for x in range(10, 21):
push(x, stack)
for _ in range(len(stack)):
print(pop(stack))
7.
def push(stack, elem):
stack.insert(0, elem)
def pop(stack):
if not stack:
print("Stack is empty, nothing to pop.")
return None
return stack.pop(0)
def display(stack):
print("Stack:", stack)
stack = []
while True:
print("\nChoose an operation:")
print("1. Push element to stack")
print("2. Pop element from stack")
print("3. Display stack")
print("4. Exit")
choice = int(input("Enter your choice (1/2/3/4):
"))
if choice == 1:
elem = input("Enter element to push: ")
push(stack, elem)
elif choice == 2:
print("Popped element:", pop(stack))
elif choice == 3:
display(stack)
elif choice == 4:
print("Exiting program.")
break
else:
print("Invalid choice, please choose again.")
9.
tack = [ ]
while True :
print()
print("Enter your choice as per given -")
print("1 = For insert data Enter insert ")
print("2 = For delete data enter delete ")
print("3 = For Exit enter exit ")
print()
user = input("Enter your choice :- ")
if user == "insert" :
pin = int(input("Enter the pin code of city :-
"))
city = input("Enter name of city :- ")
data = [ pin , city ]
stack.append(data)
elif user == "delete" :
if stack == [ ]:
print("Under Flow")
else :
stack.pop()
else :
break
print("Now our stack = ",stack)
10.
def push(b):
n = input("Enter the book name: ")
b.append(n)
def pop(b):
if not b:
print("Books is empty")
else:
print("Deleted Book", b.pop())
b = []
push(b)
push(b)
push(b)
print("Initial Books Stack:", b)
pop(b)
print("Books Stack after deletion:", b)
11.
def PUSH(Arr):
stack = [x for x in Arr if x % 5 == 0]
if stack:
print("Stack:", stack)
else:
print("No number”)
12.
def add(Books):
bookname = input("Enter the book name: ")
Books.append(bookname)
def delete(Books):
if Books == []:
print("Books is empty")
else:
print("Deleted Book", Books.pop())
Books = []
add(Books)
add(Books)
add(Books)
print("Initial Books Stack:", Books)
delete(Books)
print("Books Stack after deletion:", Books)