Professional Documents
Culture Documents
DSA Lab Experiments - 5 A) and 5 B)
DSA Lab Experiments - 5 A) and 5 B)
DSA Lab Experiments - 5 A) and 5 B)
No : 5 a)
Implementation of Stack ADTs
Date :
Aim:
Write a program for the implementation of Stack ADTs using python.
Algorithm:
# Example usage:
stack = Stack()
# Get inputs from the user.
while True:
operation = input("Enter the operation you want to perform (push, pop, peek, is_empty,
print): ")
if operation == "push":
item = int(input("Enter the item to push: "))
stack.push(item)
elif operation == "pop":
print(stack.pop())
elif operation == "peek":
print(stack.peek())
elif operation == "is_empty":
print(stack.is_empty())
elif operation == "print":
stack.print_stack()
else:
break
Result:
Thus, the Python program for implementation of stack ADTs was executed and verified
successfully.
Ex. No : 5 b)
Implementation of Queue ADTs
Date :
Aim:
Write a program for the implementation of Queue ADTs using python.
Algorithm:
Program:
class Queue:
def __init__(self):
self.items = []
def dequeue(self):
if len(self.items) == 0:
raise Exception("Queue is empty")
return self.items.pop(0)
def peek(self):
if len(self.items) == 0:
raise Exception("Queue is empty")
return self.items[0]
def is_empty(self):
return len(self.items) == 0
def print_queue(self):
print(self.items)
# Example usage:
queue = Queue()
if operation == "enqueue":
item = int(input("Enter the item to enqueue: "))
queue.enqueue(item)
elif operation == "dequeue":
print(queue.dequeue())
elif operation == "peek":
print(queue.peek())
elif operation == "is_empty":
print(queue.is_empty())
elif operation == "print":
queue.print_queue()
else:
break
Result:
The Python program for implementation of queue ADTs was executed and verified
successfully.