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

Ds 6 7

Useful for practical

Uploaded by

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

Ds 6 7

Useful for practical

Uploaded by

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

Reg.

No:-311521205007

Stack in python
PROGRAM:
def create_stack():
stack = []
return stack
def check_empty(stack):
return len(stack) == 0
def push(stack, item):
stack.append(item)
print("pushed item: " + item)
def pop_last_element(stack):
if (check_empty(stack)):
return "stack is empty"
return stack.pop()
def pop(stack,item):
if (check_empty(stack)):
return "stack is empty"
return stack.remove(item)
def empty_the_stack():
if len(stack)!=0:
print(stack)
if len(stack)==0:
print("Stack Underflow")
stack = create_stack()
a=int(input("Enter the element to be added to stack"))
push(stack,str(a))
push(stack, str(1))
push(stack, str(2))
push(stack, str(3))
Reg.No:-311521205007

push(stack, str(4))
print("popped item: " + pop_last_element(stack))
print("stack after popping an element: " + str(stack))
b=int(input("Enter the element to be removed from stack"))
pop(stack,str(b))
print(str(stack))

OUTPUT:

RESULT:
Reg.No:-311521205007

Queue in python

PROGRAM:
class Queue:
def __init__(self):
self.queue = []
def enqueue(self, item):
self.queue.append(item)
def dequeue(self):
if len(self.queue) < 1:
return None
return self.queue.pop(0)
def display(self):
print(self.queue)
def size(self):
return len(self.queue)
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.enqueue(4)
q.enqueue(5)
q.display()
q.dequeue()
print("After removing an element")
q.display()
Reg.No:-311521205007

OUTPUT:

RESULT:

You might also like