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

DS Lab Assignment4

Uploaded by

bchgpdqk57
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)
12 views4 pages

DS Lab Assignment4

Uploaded by

bchgpdqk57
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

4.

Implement Stack as an ADT and use it to evaluate a prefix/postfix


expression.
5. Implement Queue as an ADT.

QUES4.CODE
class Stack:
"""Class to implement Stack as an ADT."""
def __init__(self):
self.stack = []

def push(self, item):


"""Push an item onto the stack."""
self.stack.append(item)

def pop(self):
"""Pop an item from the stack."""
if not self.is_empty():
return self.stack.pop()
raise IndexError("Pop from empty stack")

def peek(self):
"""Peek at the top item without removing it."""
if not self.is_empty():
return self.stack[-1]
raise IndexError("Peek from empty stack")

def is_empty(self):
"""Check if the stack is empty."""
return len(self.stack) == 0

def size(self):
"""Return the size of the stack."""
return len(self.stack)

def evaluate_postfix(expression):
"""Evaluate a postfix expression using a stack."""
stack = Stack()
for char in expression.split():
if char.isdigit():
stack.push(int(char))
else:
# Pop the top two elements for the operation
b = stack.pop()
a = stack.pop()
if char == '+':
stack.push(a + b)
elif char == '-':
stack.push(a - b)
elif char == '*':
stack.push(a * b)
elif char == '/':
stack.push(a / b)
return stack.pop()

def evaluate_prefix(expression):
"""Evaluate a prefix expression using a stack."""
stack = Stack()
# Iterate in reverse order for prefix
for char in reversed(expression.split()):
if char.isdigit():
stack.push(int(char))
else:
# Pop the top two elements for the operation
a = stack.pop()
b = stack.pop()
if char == '+':
stack.push(a + b)
elif char == '-':
stack.push(a - b)
elif char == '*':
stack.push(a * b)
elif char == '/':
stack.push(a / b)
return stack.pop()

# Example usage
if __name__ == "__main__":
postfix_expr = "5 1 2 + 4 * + 3 -"
print("Postfix Evaluation:", evaluate_postfix(postfix_expr))

prefix_expr = "- + 5 * 1 + 2 4 3"


print("Prefix Evaluation:", evaluate_prefix(prefix_expr))

OUTPUT
QUES5. CODE

class Queue:
"""Class to implement Queue as an ADT."""
def __init__(self):
self.queue = []

def enqueue(self, item):


"""Add an item to the rear of the queue."""
self.queue.append(item)

def dequeue(self):
"""Remove and return an item from the front of the queue."""
if not self.is_empty():
return self.queue.pop(0)
raise IndexError("Dequeue from empty queue")

def peek(self):
"""Peek at the front item without removing it."""
if not self.is_empty():
return self.queue[0]
raise IndexError("Peek from empty queue")

def is_empty(self):
"""Check if the queue is empty."""
return len(self.queue) == 0

def size(self):
"""Return the size of the queue."""
return len(self.queue)

def display(self):
"""Display the current state of the queue."""
print("Queue:", self.queue)

# Example usage
if __name__ == "__main__":
q = Queue()
q.enqueue(10)
q.enqueue(20)
q.enqueue(30)
q.display()
print("Dequeued:", q.dequeue())
q.display()

print("Peek:", q.peek())
print("Size:", q.size())

OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy