DS Practical
DS Practical
Practical No:1
Code-
class Node:
#constructor
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
if (self.head):
current = self.head
while(current.next):
current = current.next
current.next = newNode
else:
Karishma Jadhav 213
self.head = newNode
current = self.head
while (current):
print (current.data)
current = current.next
LL = LinkedList()
LL.insert(3)
LL.insert(4)
LL.insert(5)
LL.printLL()
Output-
Karishma Jadhav 213
Practical No:2
Code-
class Node:
self.prev = None
self.data = my_data
self.next = None
class double_list:
self.head = None
self.tail = None
if(self.head==None):
self.head=self.tail = new_node;
self.head.previous = None;
self.tail.next = None;
else:
self.tail.next = new_node;
new_node.previous = self.tail;
self.tail = new_node;
self.tail.next = None;
Karishma Jadhav 213
curr= self.head
if (self.head == None):
return
print(curr.data)
curr = curr.next
my_instance = double_list()
my_instance.add_data(10)
my_instance.add_data(24)
my_instance.add_data(54)
my_instance.add_data(77)
my_instance.add_data(92)
my_instance.print_it()
Output-
Karishma Jadhav 213
Practical No:3
1.Insertion in stack.
Code-
class Stack:
def __init__(self):
self.stack = []
self.stack.append(dataval)
return True
else:
return False
def peek(self):
return self.stack[-1]
AStack = Stack()
AStack.add("Mon")
AStack.add("Tue")
AStack.peek()
Karishma Jadhav 213
print(AStack.peek())
AStack.add("Wed")
AStack.add("Thu")
print(AStack.peek())
Output-
2.Deletion in stack.
Code-
class Stack:
def __init__(self):
self.stack = []
self.stack.append(dataval)
return True
else:
return False
def remove(self):
if len(self.stack) <= 0:
Karishma Jadhav 213
else:
return self.stack.pop()
AStack = Stack()
AStack.add("Mon")
AStack.add("Tue")
AStack.add("Wed")
AStack.add("Thu")
print(AStack.remove())
print(AStack.remove())
Output-
Karishma Jadhav 213
Practical No. 4
Q1] implementation of queue
Code:
#python program to demonstrate queue implemention using list
#intilization a quue
queue=[]
queue.append('a')
queue.append('b')
queue.append('c')
queue.append('d')
print("Initial queue")
print(queue)
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))
print(queue)
Karishma Jadhav 213
#Intialization a queue
q=Queue(maxsize=4)
print("size of Queue",q.qsize())
q.put('a')
q.put('b')
q.put('f')
q.put('d')
print("\n Full:",q.full())
print(q.get())
print(q.get())
print(q.get())
print(q.get())
print("\n Empty:",q.empty())
q.put(1)
Karishma Jadhav 213
print("\n Empty",q.empty())
print("\n Full:",q.full())
Practical NO. 5
Aim: Implemantation of a Tree.
1.Creation of the tree
class Node:
self.left=None
self.right=None
self.data=data
def printTree(self):
print(self.data)
root=Node(10)
root.printTree()
self.left=None
self.right=None
self.data=data
if self.data:
if data< self.data:
if self.left is None:
self.left=Node(data)
else:
self.left.insert(data)
if data> self.data:
if self.right is None:
self.right=Node(data)
else:
self.right.insert(data)
else:
self.data=data
if self.left:
self.left.printTree()
print(self.data)
if self.right:
self.right.printTree()
root=Node(12)
root.insert(6)
root.insert(14)
root.insert(34)
Karishma Jadhav 213
root.printTree()
Practical no. 6
Aim: Implementation of graph
write a python code to display adjacent list in graph.
Code:
graph= {0:[1,3],1:[0,2,3],2:[4,0,1],4:[2,3,5],5:[2,4]}
print(graph)
graph={0:[1,3],1:[0,2,3],2:[4,0,1],4:[2,3,5],5:[2,4]}
print(graph)
vertices=set(graph.keys())
print(vertices)
Karishma Jadhav 213