0% found this document useful (0 votes)
33 views4 pages

Stacks

The document contains Python code for implementing a stack and a queue data structure. It includes functions for pushing, popping, peeking, and displaying elements in the stack, as well as enqueueing, dequeueing, and displaying elements in the queue. The code also features user interaction through a menu-driven interface for both data structures.

Uploaded by

Sarthak Gupta
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)
33 views4 pages

Stacks

The document contains Python code for implementing a stack and a queue data structure. It includes functions for pushing, popping, peeking, and displaying elements in the stack, as well as enqueueing, dequeueing, and displaying elements in the queue. The code also features user interaction through a menu-driven interface for both data structures.

Uploaded by

Sarthak Gupta
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/ 4

STACKS:

stk=[]

top=None

def push(stk, val):

top=len(stk)

stk.append(val)

def peek(stk):

top=len(stk)-1

if top<0:

print("stack is empty")

else:

print(stk[top])

def Pop(stk):

top=len(stk)-1

if top<0:

print("stack is empty")

else:

print("the year to be deleted is=", stk[top])

stk.pop()

def display(stk):

top=len(stk)-1

if top<0:

print("stack is empty")

else:

for i in range(top,-1,-1):

print(stk[i])

ch=0

while(ch!=5):
print("\n1. push")

print("\n2. peek")

print("\n3. pop")

print("\n4. display")

print("\n5. Exit")

ch=int(input("\n your ch(1-5)"))

if ch==1:

v=int(input("\n enter the year to input:"))

push(stk,v)

elif ch==2:

peek(stk)

elif ch==3:

Pop(stk)

elif ch==4:

display(stk)

QUEUES:
que=[]
front=None
rear=None
def enq(que, val):
if len(que)==0:
front=rear=0
else:
rear=len(que)
que.append(val)
def deq(que):
if len(que)==0:
print("queue is empty")
else:
print("the Name to be removed is=", que[0])
front=0
rear=len(que)-1
if(front==rear):
que.pop(0)
front=rear=None
else:
que.pop(0)
rear=rear-1

def display(que):
if len(que)==0:
print("queue is empty")
else:
for i in range(0,len(que)):
print(que[i])
ch=0
while(ch!=4):
print("\n1. add name")
print("\n2. delete name")
print("\n3. display")
print("\n4. Exit")
ch=int(input("\n your ch(1-4)"))
if ch==1:
v=str(input("\n enter Name to be added:"))
enq(que,v)
elif ch==2:
deq(que)
elif ch==3:
display(que)

RECURSION:

You might also like