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

Assignment-3 Stack Queue

The document contains a Data Structures and Algorithms assignment by Kanishk Deshpande, detailing three programming tasks involving stack, linear queue, and priority queue implementations in Python. Each task includes class definitions for nodes and their respective data structures, along with methods for pushing, popping, enqueueing, dequeueing, and displaying values. Sample outputs are provided to demonstrate the functionality of each implemented data structure.

Uploaded by

kdtg77
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)
5 views4 pages

Assignment-3 Stack Queue

The document contains a Data Structures and Algorithms assignment by Kanishk Deshpande, detailing three programming tasks involving stack, linear queue, and priority queue implementations in Python. Each task includes class definitions for nodes and their respective data structures, along with methods for pushing, popping, enqueueing, dequeueing, and displaying values. Sample outputs are provided to demonstrate the functionality of each implemented data structure.

Uploaded by

kdtg77
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

DSA Assignment no.

- 3

Name :- Kanishk deshpande


Roll no :- 2401042
Div :- A

1. Write a program to perform following operations on stack- Push, Pop, Print


Input:-
class node:
def __init__(self,val):
self.data=val
self.next=None
class stack:
def __init__(self):
self.top=None
def push(self,new_node):
if self.top is None:
self.top=new_node
else:
new_node.next=self.top
self.top=new_node
def pop(self):
if self.top is None:
print("stack is empty")
return
else:
print("The deleted node is ",self.top.data)
self.top=self.top.next

def display(self):
temp=self.top
while temp:
print("The value in the stack are",temp.data)
temp=temp.next

st=stack()
st.push(node(10))
st.push(node(20))
st.push(node(30))
st.push(node(40))
st.display()
print("After deletion ")
st.pop()
st.display()

output:-
The value in the stack are 40
The value in the stack are 30
The value in the stack are 20
The value in the stack are 10
After deletion
The deleted node is 40
The value in the stack are 30
The value in the stack are 20
The value in the stack are 10

2. Write a program to create a linear queue to perform enqueue,dequeue and


print operations
Input:-
class node:
def __init__(self,data):
self.data=data
self.next=None
class queue:
def __init__(self):
self.front=None
self.rear=None
def enqueue(self,new_node):
if self.front is None and self.rear is None:
# if self.front and self.rear is None:
self.front=self.rear=new_node
else:
self.rear.next=new_node
self.rear=new_node
def print(self):
temp=self.front
while temp is not None:
print("The value in queue is ",temp.data)
temp=temp.next

def dequeue(self):
if self.rear is None:
print("queue is empty")
return
else:
print("The deleted node is ",self.front.data)
self.front=self.front.next

q=queue()
q.enqueue(node(10))
q.enqueue(node(20))
q.enqueue(node(30))
q.enqueue(node(40))
q.print()
q.dequeue()
q.print()

output:- The value in queue is 10


The value in queue is 20
The value in queue is 30
The value in queue is 40
The deleted node is 10
The value in queue is 20
The value in queue is 30
The value in queue is 40

3. Write a program to create a priority queue to perform enqueue,dequeue and


print operations
Input:-
class node:
def __init__(self,pos,val):
self.data=val
self.next=None
self.pri=pos
class prique:
def __init__(self):
self.front=self.rear=None

def enqueue(self,new_node):
if self.front==None:
self.front=self.rear=new_node
return
if self.rear:
temp=self.front
if self.front.pri>new_node.pri:
new_node.next=self.front
self.front=new_node
return
while temp.next:
if temp.next.pri>=new_node.pri:
break
else:
temp=temp.next
new_node.next=temp.next
temp.next=new_node

return
def print(self):
temp=self.front
while temp:
print(temp.data,end="->")
temp=temp.next
print()

def dequeue(self):
if self.rear!=None:#check if rear is not empty
temp=self.front
print("node delted....",temp.data)
self.front=self.front.next
if self.front==None: #dlting last node in queue
self.rear=None
del temp
else:
print("queue is empty.....")
p1=prique()
p1.enqueue(node(30,2))
p1.enqueue(node(45,1))
p1.enqueue(node(5,3))
p1.enqueue(node(30,4))
p1.print()
p1.dequeue()
p1.dequeue()
p1.print()

Output:-
3->4->2->1->
node delted.... 3
node delted.... 4
2->1->

You might also like