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

Stack & Queue

The document contains implementations of data structures including Stack and Queue in Python. It includes methods for pushing, popping, and displaying elements in the Stack, as well as enqueueing and dequeueing in the Queue. Additionally, it provides a function to convert infix expressions to postfix notation using a stack-based approach.
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)
4 views4 pages

Stack & Queue

The document contains implementations of data structures including Stack and Queue in Python. It includes methods for pushing, popping, and displaying elements in the Stack, as well as enqueueing and dequeueing in the Queue. Additionally, it provides a function to convert infix expressions to postfix notation using a stack-based approach.
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

Stack:

Case1:

class Stack:
def __init__(self):
self.stack = []

def is_empty(self):
return len(self.stack) == 0

def push(self, item):


self.stack.append(item)
print("pushed item:", item)

def pop(self):
if self.is_empty():
return "stack is empty"
return self.stack.pop()

def display(self):
print("stack after popping an element:", self.stack)

my_stack = Stack()
my_stack.push(1)
my_stack.push(2)
my_stack.push(3)
print("popped item:", my_stack.pop())
my_stack.display()

case2:
class Stack:
def __init__(self):
self.stack = []

def push(self, item):


self.stack.append(item)
print("Pushed:", item)

def pop(self):
return self.stack.pop() if self.stack else "Stack is empty"

def display(self):
print("Stack:", self.stack if self.stack else "Empty stack")

my_stack = Stack()

while True:
choice = input("\n1: Push | 2: Pop | 3: Display | 4: Exit\nChoose: ")
if choice == '1':
my_stack.push(int(input("Enter item: ")))
elif choice == '2':
print("Popped:", my_stack.pop())
elif choice == '3':
my_stack.display()
elif choice == '4':
break
else:
print("Invalid choice!")

Infix to Postfix:

Operators = set(['+', '-', '*', '/', '(', ')', '^'])

Priority = {'+':1, '-':1, '*':2, '/':2, '^':3}

def infixToPostfix(expression):

stack = []

output = ''

for character in expression:

if character not in Operators:


output+= character

elif character=='(':

stack.append('(')

elif character==')':

while stack and stack[-1]!= '(':

output+=stack.pop()

stack.pop()

else:

while stack and stack[-1]!='(' and Priority[character]<=Priority[stack[-1]]:

output+=stack.pop()

stack.append(character)

while stack:

output+=stack.pop()

return output

expression = input('Enter infix expression ')


print('infix notation: ',expression)

print('postfix notation: ',infixToPostfix(expression))

Queue:

class Queue():

def __init__(self, k):


self.k = k
self.queue = [None] * k
self.head = self.tail = -1

def enqueue(self, data):

if (self.tail == self.k - 1):


print("The queue is full\n")

elif (self.head == -1):


self.head = 0
self.tail = 0
self.queue[self.tail] = data
else:
self.tail = self.tail + 1
self.queue[self.tail] = data

def dequeue(self):
if (self.head == -1):
print("The queue is empty\n")

elif (self.head == self.tail):


temp = self.queue[self.head]
self.head = -1
self.tail = -1
return temp
else:
temp = self.queue[self.head]
self.head = self.head + 1
return temp

def printQueue(self):
if(self.head == -1):
print("No element in the queue")

else:
for i in range(self.head, self.tail + 1):
print(self.queue[i], end=" ")
print()

obj = Queue(5)
obj.enqueue(1)
obj.enqueue(2)
obj.enqueue(3)
obj.enqueue(4)
obj.enqueue(5)
print("Initial queue")
obj.printQueue()

obj.dequeue()
print("After removing an element from the queue")
obj.printQueue()

You might also like