Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
23 views10 pages

Python Stack and List Operations

Uploaded by

ss55.singtamkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views10 pages

Python Stack and List Operations

Uploaded by

ss55.singtamkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Data structures:

1.
def f_l(l, v):
if v in l:
return l.index(v)
else:
return -1

l = eval(input("Enter a list: "))


v = int(input("Enter a value to be checked: "))
print(f_l(l, v))
2.
def u(l):
c=0
for i in range(0, len(l)):
if l[i] not in l[i+1:]:
c += 1
return c

l = eval(input("Enter the list: "))


print(u(l))
3.
def push(stack, element):
stack.append(element)
print(f"{element} pushed to stack")

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

t = input("Enter a line of text: ")


result = r_d(t)
print("Reversed and doubled string:", result)
5.
def p(a):
if not a:
return None
return a.pop()

# 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)

You might also like