Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 13

Karishma Jadhav 213

Practical No:1

Aim: Singly Linked list

Code-

# A single node of a singly linked list

class Node:

#constructor

def __init__(self, data = None, next=None):

self.data = data

self.next = next

# A Linked List class with a single head node.

class LinkedList:

def __init__(self):

self.head = None

# insertion method for the linked list

def insert (self, data):

newNode = Node (data)

if (self.head):

current = self.head

while(current.next):

current = current.next

current.next = newNode

else:
Karishma Jadhav 213

self.head = newNode

# print method for the linked list

def printLL (self):

current = self.head

while (current):

print (current.data)

current = current.next

# Singly Linked List with insertion and print methods

LL = LinkedList()

LL.insert(3)

LL.insert(4)

LL.insert(5)

LL.printLL()

Output-
Karishma Jadhav 213

Practical No:2

Aim: Doubly Linked List

Code-

class Node:

def __init__(self, my_data):

self.prev = None

self.data = my_data

self.next = None

class double_list:

def __init__ (self):

self.head = None

self.tail = None

def add_data (self, my_data):

new_node = Node (my_data)

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

def print_it (self):

curr= self.head

if (self.head == None):

print ("The list is empty")

return

print ("The nodes in the doubly linked list are :")

while curr != None:

print(curr.data)

curr = curr.next

my_instance = double_list()

print ("Elements are being added to the doubly linked 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

Aim: Write a program to implement stack with insertion and deletion.

1.Insertion in stack.

Code-

class Stack:

def __init__(self):

self.stack = []

def add(self, dataval):

# use list append method to add element\

if dataval not in self.stack:

self.stack.append(dataval)

return True

else:

return False

# use peek to look at the top of the stack

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 = []

def add(self, dataval):

# use list append method to add element\

if dataval not in self.stack:

self.stack.append(dataval)

return True

else:

return False

# Use list pop method to remove element

def remove(self):

if len(self.stack) <= 0:
Karishma Jadhav 213

return ("No element in the Stack")

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=[]

#Adding element to the queue

queue.append('a')

queue.append('b')

queue.append('c')

queue.append('d')

print("Initial queue")

print(queue)

#removing the elment from the queue

print("Elment dequeue from queue")

print(queue.pop(0))

print(queue.pop(0))

print(queue.pop(0))

print(queue.pop(0))

print("\nQueue after removing element")

print(queue)
Karishma Jadhav 213

Q2]Implementation of Queue by using Queue Module


#demonstrate implementation of queue using module

from queue import Queue

#Intialization a queue

q=Queue(maxsize=4)

#qsize() give the maxsize of the Queue

print("size of Queue",q.qsize())

#adding of element to queue

q.put('a')

q.put('b')

q.put('f')

q.put('d')

#return boolean for full queue

print("\n Full:",q.full())

#Removing element from queue

print(q.get())

print(q.get())

print(q.get())

print(q.get())

#Return boolean for empty queue

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:

def __init__ (self,data):

self.left=None

self.right=None

self.data=data

def printTree(self):

print(self.data)

root=Node(10)

root.printTree()

2. Write a python code to perform insert operation in tree.


class Node:

def __init__ (self,data):


Karishma Jadhav 213

self.left=None

self.right=None

self.data=data

def insert (self,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

#print the tree

def printTree (self):

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("The adjacency list representing the graph is:")

print(graph)

graph={0:[1,3],1:[0,2,3],2:[4,0,1],4:[2,3,5],5:[2,4]}

print("The adjacency list representing the graph is:")

print(graph)

vertices=set(graph.keys())

print("The vertices of the graph are::")

print(vertices)
Karishma Jadhav 213

You might also like