0% found this document useful (0 votes)
28 views7 pages

Dsa 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views7 pages

Dsa 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Ex.

No: 4 Implementation of Singly Linked List and Circular


Linked List
15-08-23
Reg No: URK22CS1026

Aim:
Implement the singly linked list and circular linked list: The expected operations are Insertion
operation at the front of the list, at the back of the list and any position in the list, Deletion of
an element from the list, displaying all elements in the list.

Description:
A linked list is a linear data structure where each element is a separate object. Each element
(we will call it a node) of a list is comprised of two items- the data and a reference to the next
node. The last node has a reference to null. The entry point into a linked list is called the head
of the list. It should be noted that the head is not a separate node, but a reference to the first
node. If the list is empty then the head is a null reference. A linked list is a dynamic data
structure. The number of nodes in a list is not fixed and can grow and shrink on demand. Any
application which has to deal with an unknown number of objects will need to use a linked
list. One disadvantage of a linked list against an array is that it does not allow direct access to
the individual elements. If you want to access a particular item then you have to start at the
head and follow the references until you get to that item. Another disadvantage is that a
linked list uses more memory compared with an array - extra 4 bytes (on 32-bit CPU) to store
a reference to the next node.
Circular Linked list: Circular linked list is a linked list where all nodes are connected to
form a circle. There is no NULL at the end.

Algorithm: Linked list


1. Define a class node linked_list with attributes data,link and head respectively.
2. The insert_front() method inserts an element at the front of the linked list.
3. The insert_end() method inserts an element at the end of the linked list.
4. The insert_atpos() method inserts an element at a specific position in the linked list.
5. The delete() deletes an element from the linked list
6. The display() prints the linked list
7. Create an object to access the various methods of the linked list
Program: Linked list
class node:
def __init__(self,x):
self.data=x
self.link=None
class linked_list:
def __init__(self):
self.head=None
def insert_front(self,x):
n=node(x)
n.link=self.head
self.head=n
def insert_end(self,x):
n=node(x)
current_node=self.head
if self.head==None:
self.head=n
return
while current_node.link!=None:
current_node=current_node.link
current_node.link=n
def insert_atpos(self,pos,x):
n=node(x)
i=self.head
if pos<0:
print("invalid position")
return
elif pos==0:
n.link=self.head
self.head=n
return
num=0
while (i !=None and num<pos-1):
i=i.link
num+=1
if i is None:
print("Position out of bounds")
return
n.link=i.link
i.link=n
def delete(self,x):
if self.head is None:
print("No elements to delete")
return
if self.head.data==x:
self.head=self.head.link
return
current_node=self.head
prev_node=None
while current_node!=None:
if current_node.data==x:
prev.link=current_node.link
current_node=None
return
prev=current_node
current_node=current_node.link
else:
print("Element not present")
return
def display(self):
if self.head==None:
print("List is Empty")
return
current_node=self.head
while current_node!=None:
print(current_node.data,"-->",end=" ")
current_node=current_node.link
print("None")
l=linked_list()
l.insert_end(4)
l.insert_front(3)
l.insert_front(2)
l.insert_front(1)
l.insert_atpos(4,5)
l.display()

Output:

Program: Circular Linked list


class Node:
def __init__(self, data):
self.data = data
self.link = None
class CircularLinkedList:
def __init__(self):
self.head = None
def insert_atend(self, data):
new_node = Node(data)
if self.head==None:
self.head = new_node
self.head.link = self.head
else:
current_node = self.head
while current_node.link != self.head:
current_node = current_node.link
current_node.link = new_node
new_node.link = self.head
def insert_at_beginning(self, data):
new_node = Node(data)
if self.head==None:
self.head = new_node
self.head.link = self.head
else:
current_node = self.head
while current_node.link != self.head:
current_node = current_node.link
current_node.link = new_node
new_node.link = self.head
self.head = new_node
def insert_at_position(self,position,data):
new_node = Node(data)
if position == 0:
self.insert_at_beginning(data)
else:
current_node = self.head
for i in range(position - 1):
current_node = current_node.link
new_node.link = current_node.link
current_node.link = new_node
def delete(self, key):
if self.head==None:
print("No element to delete")
return
if self.head.data == key:
current_node = self.head
while current_node.link != self.head:
current_node = current_node.link
if self.head == self.head.link:
self.head = None
else:
current_node.link = self.head.link
self.head = self.head.link
else:
prev = None
current_node = self.head
while current_node.link != self.head:
prev = current_node
current_node = current_node.link
if current_node.data == key:
prev.link = current_node.link
break
def display(self):
if self.head==None:
print("List is empty")
return
current_node = self.head
while True:
print(current_node.data, end=" -> ")
current_node = current_node.link
if current_node == self.head:
print("->(back to head)")
break

clist = CircularLinkedList()
clist.insert_atend(3)
clist.insert_atend(4)
clist.insert_atend(5)
clist.insert_at_beginning(2)
clist.insert_at_beginning(1)
clist.insert_at_position(5,6)
clist.display()

Output:

Result:
These program successfully demonstrate the implementation of Linked list and Circular
linked list along with various methods like insert_front, insert_end, insert_pos, delete and
display.

You might also like