DSA Lab Experiments - 5 A) and 5 B)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Ex.

No : 5 a)
Implementation of Stack ADTs
Date :

Aim:
Write a program for the implementation of Stack ADTs using python.

Algorithm:

Step 1: Start the Program.


Step 2: Create a class called Stack with the following methods:
push(): This method pushes an item onto the stack.
pop(): This method pops an item from the stack.
peek(): This method returns the top item on the stack without popping it.
is_empty(): This method returns True if the stack is empty, and False otherwise.
print_stack(): This method prints the contents of the stack to the console.
Step 3: Create an instance of the Stack class.
Step 4: Initialize a loop that will allow the user to perform operations on the stack.
• Prompt the user to enter the operation they want to perform.
• If the user enters "push", prompt the user to enter the item they want to push onto the stack, and
then call the push() method on the stack object.
• If the user enters "pop", call the pop() method on the stack object and print the popped item to
the console.
• If the user enters "peek", call the peek() method on the stack object and print the top item on
the stack to the console.
• If the user enters "is_empty", call the is_empty() method on the stack object and print the result
to the console.
• If the user enters "print", call the print_stack() method on the stack object to print the contents
of the stack to the console.
• Continue the loop until the user enters a valid operation.
Step 5: Stop the Program
Program:
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
if len(self.items) == 0:
raise Exception("Stack is empty")
return self.items.pop()
def peek(self):
if len(self.items) == 0:
raise Exception("Stack is empty")
return self.items[-1]
def is_empty(self):
return len(self.items) == 0
def print_stack(self):
print(self.items)

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

Step 1 : Start the Program.


Step 2 : Create a class called Queue with the following methods:
o enqueue(): This method adds an item to the back of the queue.
o dequeue(): This method removes an item from the front of the queue.
o peek(): This method returns the item at the front of the queue without removing it.
o is_empty(): This method returns True if the queue is empty, and False otherwise.
o print_queue(): This method prints the contents of the queue to the console.
Step 3 : Create an instance of the Queue class.
Step 4 : Initialize a loop that will allow the user to perform operations on the queue.
o Prompt the user to enter the operation they want to perform.
o If the user enters "enqueue", prompt the user to enter the item they want to enqueue,
and then call the enqueue() method on the queue object.
o If the user enters "dequeue", call the dequeue() method on the queue object and print
the dequeued item to the console.
o If the user enters "peek", call the peek() method on the queue object and print the item
at the front of the queue to the console.
o If the user enters "is_empty", call the is_empty() method on the queue object and print
the result to the console.
o If the user enters "print", call the print_queue() method on the queue object to print the
contents of the queue to the console.
Step 5 : Continue the loop until the user enters a valid operation.

Program:

class Queue:
def __init__(self):
self.items = []

def enqueue(self, item):


self.items.append(item)

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

# Get inputs from the user.


while True:
operation = input("Enter the operation you want to perform (enqueue, dequeue, peek,
is_empty, print): ")

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.

You might also like