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

Stack Program2

The document covers data structures and algorithms for a BTech 3rd semester course, focusing on stack-based validation of balanced parentheses and queue implementations using both arrays and linked lists in Python. It provides code examples for each implementation, demonstrating enqueue, dequeue, peek, isEmpty, and size functionalities. The document concludes with sample outputs for the queue operations.

Uploaded by

ankush.dhawan178
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 Program2

The document covers data structures and algorithms for a BTech 3rd semester course, focusing on stack-based validation of balanced parentheses and queue implementations using both arrays and linked lists in Python. It provides code examples for each implementation, demonstrating enqueue, dequeue, peek, isEmpty, and size functionalities. The document concludes with sample outputs for the queue operations.

Uploaded by

ankush.dhawan178
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

Data Structure and Algorithm (BTech-3rd Sem)

Unit-1
1. Check for balanced parentheses using a Stack
(Python)

s = "{[()()]}"
st = []
for c in s:
# Push opening brackets onto the stack
if c in '({[':
[Link](c)
# Check for matching closing bracket
elif c in ')}]':
if not st or (c == ')' and st[-1] != '(') or (c == '}' and st[-1] != '{') or (c == ']' and st[-1] != '['):
print(False) # Mismatched bracket
break
[Link]() # Pop matched opening bracket
else:
print(True if not st else False) # Balanced if stack is empty

Output
True

[Link] Implementation using Arrays


class Queue:
def __init__(self):
[Link] = []

def enqueue(self, element):


[Link](element)

def dequeue(self):
if [Link]():
return "Queue is empty"
return [Link](0)

def peek(self):
if [Link]():
return "Queue is empty"
return [Link][0]

def isEmpty(self):
return len([Link]) == 0

def size(self):
return len([Link])

# Create a queue
myQueue = Queue()
[Link]('A')
[Link]('B')
[Link]('C')
print("Queue: ", [Link])
print("Dequeue: ", [Link]())
print("Peek: ", [Link]())
print("isEmpty: ", [Link]())
print("Size: ", [Link]())
3. Queue Implementation using Linked Lists
class Node:
def __init__(self, data):
[Link] = data
[Link] = None

class Queue:
def __init__(self):
[Link] = None
[Link] = None
[Link] = 0

def enqueue(self, element):


new_node = Node(element)
if [Link] is None:
[Link] = [Link] = new_node
[Link] += 1
return
[Link] = new_node
[Link] = new_node
[Link] += 1

def dequeue(self):
if [Link]():
return "Queue is empty"
temp = [Link]
[Link] = [Link]
[Link] -= 1
if [Link] is None:
[Link] = None
return [Link]

def peek(self):
if [Link]():
return "Queue is empty"
return [Link]
def isEmpty(self):
return [Link] == 0

def size(self):
return [Link]

def printQueue(self):
temp = [Link]
while temp:
print([Link], end=" ")
temp = [Link]
print()

# Create a queue
myQueue = Queue()
[Link]('A')
[Link]('B')
[Link]('C')
print("Queue: ", end="")
[Link]()
print("Dequeue: ", [Link]())
print("Peek: ", [Link]())
print("isEmpty: ", [Link]())
print("Size: ", [Link]())

You might also like