0% found this document useful (0 votes)
6 views

Lab 6

This lab focuses on circular and doubly linked lists, including their implementation in Python. It covers operations such as insertion, printing, and deletion for circular linked lists, as well as traversal in both directions for doubly linked lists. The lab assignments involve creating and manipulating these data structures with specific tasks outlined for students.

Uploaded by

f2023019021
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)
6 views

Lab 6

This lab focuses on circular and doubly linked lists, including their implementation in Python. It covers operations such as insertion, printing, and deletion for circular linked lists, as well as traversal in both directions for doubly linked lists. The lab assignments involve creating and manipulating these data structures with specific tasks outlined for students.

Uploaded by

f2023019021
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/ 5

Data Structures and Algorithms Lab Lab 6

INTRODUCTION TO CIRCULAR AND DOUBLY LINKED LISTS

Objectives:

 To understand the concept of circular and doubly linked lists.


 To implement insertion and printing operations in circular and doubly linked lists.
 To implement deletion operations in circular linked lists.
 To traverse a doubly linked list in both forward and reverse order.

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.

Circular Linked List

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

Circular Linked List Implementation in Python

class Node:
def __init__(self, data):
self.data = data
self.next = None

class CircularLinkedList:
def __init__(self):
self.head = None

def insert_front(self, data):

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)")

# Creating and printing a circular linked list


cll = CircularLinkedList()
cll.insert_front(3)
cll.insert_front(5)
cll.insert_front(1)
cll.print_list()

Doubly Linked List

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

Doubly Linked List Implementation in Python

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 insert_front(self, data):


new_node = DNode(data)
if not self.head:
self.head = new_node
else:
new_node.next = self.head
self.head.prev = new_node
self.head = new_node

def print_list(self):
temp = self.head
while temp:
print(temp.data, end=' <-> ')
temp = temp.next
print("None")

# Creating and printing a doubly linked list


dll = DoublyLinkedList()
dll.insert_front(3)
dll.insert_front(5)
dll.insert_front(1)
dll.print_list()

LAB ASSIGNMENTS

3
Data Structures and Algorithms Lab Lab 6

Q1: Create and Print a Circular Linked List

 Create a circular linked list with 6 nodes.


 Use the print_list() method to display the circular linked list.

Q2: Implement Deletion in a Circular Linked List

 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.

Q3: Create and Print a Doubly Linked List

 Create a doubly linked list with 6 nodes.


 Use the print_list() method to display the doubly linked list.

Q4: Implement Reverse Traversal in a Doubly Linked List

 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

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy