0% found this document useful (0 votes)
2 views1 page

Program 3

The document provides implementations of Custom Stack and Queue Abstract Data Types (ADTs) using recursion in Python. It includes methods for pushing/enqueuing items, checking if they are empty, popping/dequeuing items, and displaying their contents recursively. Sample usage of both data structures is also demonstrated.

Uploaded by

sherlin.djra
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)
2 views1 page

Program 3

The document provides implementations of Custom Stack and Queue Abstract Data Types (ADTs) using recursion in Python. It includes methods for pushing/enqueuing items, checking if they are empty, popping/dequeuing items, and displaying their contents recursively. Sample usage of both data structures is also demonstrated.

Uploaded by

sherlin.djra
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

Implement Custom Stack and Queue ADTs with Recursion and Algorithm Analysis

a) Custom Stack ADT using Recursion b) Custom Queue ADT using Recursion
class Stack: class Queue:
def __init__(self): def __init__(self):
[Link] = [] [Link] = []
def push(self, item): def enqueue(self, item):
[Link](item) [Link](item)
print(f"Pushed {item} into stack") print(f"Enqueued {item} into queue")
def is_empty(self): def is_empty(self):
return len([Link]) == 0 return len([Link]) == 0
def pop(self): def dequeue(self):
if self.is_empty(): if self.is_empty():
print("Stack Underflow!") print("Queue Underflow!")
return None return None
return [Link]() return [Link](0)
def display_recursive(self, index=0): def display_recursive(self, index=0):
if index == len([Link]): if index == len([Link]): # Base case
return return
print([Link][index]) print([Link][index])
self.display_recursive(index + 1) self.display_recursive(index + 1)
stack = Stack() queue = Queue()
[Link](10) [Link](5)
[Link](20) [Link](15)
[Link](30) [Link](25)
print("Stack elements (Recursive Display):") print("Queue elements (Recursive
Display):")
stack.display_recursive()
queue.display_recursive()
print("Popped:", [Link]())
print("Dequeued:", [Link]())

You might also like