Lab 6
Lab 6
Objectives:
Equipment:
Personal Computer with Python (IDLE, Jupyter Notebook, or any Python IDE).
Introduction:
This lab session introduces the idea of circular and doubly linked lists.
A circularly linked list is the same as a singly linked list but the last node of a circularly linked list does
not point to NULL, rather it points to the first node in the linked list. A typical circularly linked list can be
visualized as:
Task 1
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
1
Data Structures and Algorithms Lab Lab 6
new_node = Node(data)
if not self.head:
self.head = new_node
new_node.next = self.head
else:
temp = self.head
while temp.next != self.head:
temp = temp.next
temp.next = new_node
new_node.next = self.head
self.head = new_node
def print_list(self):
if not self.head:
return
temp = self.head
while True:
print(temp.data, end=' -> ')
temp = temp.next
if temp == self.head:
break
print("(head)")
A doubly linked list is the one in which there are two pointers in each node, one pointing in the forward
direction to the next node in the list and the other pointing in the backward direction to the previous node
in the list. Observe the structure defined for the node of a doubly linked list. It has two pointers, next and
previous. A doubly linked list can be visualized as:
NULL
Task 2
class DNode:
def __init__(self, data):
self.data = data
2
Data Structures and Algorithms Lab Lab 6
self.next = None
self.prev = None
class DoublyLinkedList:
def __init__(self):
self.head = None
def print_list(self):
temp = self.head
while temp:
print(temp.data, end=' <-> ')
temp = temp.next
print("None")
LAB ASSIGNMENTS
3
Data Structures and Algorithms Lab Lab 6
Implement methods to delete a node from the start, end, or in between a circular linked list.
Call these functions in the main method and print the updated list after each deletion.
Implement a method print_reverse() to display the doubly linked list in reverse order.
Call this function in the main method to display the list in reverse order.
Lab Report
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
4
Data Structures and Algorithms Lab Lab 6
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________