0% found this document useful (0 votes)
17 views9 pages

Experiment No 5-1

The document describes circular linked lists and provides examples of their implementation and applications. It discusses singly and doubly circular linked lists, provides examples of inserting and deleting nodes, and lists tasks for converting a singly linked list to a circular linked list and implementing additional operations like insertion and sorting on a circular linked list.

Uploaded by

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

Experiment No 5-1

The document describes circular linked lists and provides examples of their implementation and applications. It discusses singly and doubly circular linked lists, provides examples of inserting and deleting nodes, and lists tasks for converting a singly linked list to a circular linked list and implementing additional operations like insertion and sorting on a circular linked list.

Uploaded by

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

EXPERIMENT NO 5

Practice the Concept of Circular Linked List


Objective:
➢ To be able to understand and implement Circular linked list in C++.
➢ To understand the core principles of circular linked lists, their implementation, and
manipulation.
Theory:
A Circular Linked List is a variation of the standard linked list in which the last node points
back to the first node. This results in a closed-loop, making the list circular, allowing for unique
advantages and use-cases over traditional linear linked lists.

There are generally two types of circular linked lists:

Circular singly linked list: In a circular Singly linked list, the last node of the list contains a
pointer to the first node of the list. We traverse the circular singly linked list until we reach the
same node where we started. The circular singly linked list has no beginning or end. No null value
is present in the next part of any of the nodes.

Circular Doubly linked list: Circular Doubly Linked List has properties of both doubly linked
list and circular linked list in which two consecutive elements are linked or connected by the
previous and next pointer and the last node points to the first node by the next pointer and also the
first node points to the last node by the previous pointer.
Applications of circular linked lists:

1. Multiplayer games use this to give each player a chance to play.


2. A circular linked list can be used to organize multiple running applications on an
operating system. These applications are iterated over by the OS.
3. Circular linked lists can be used in resource allocation problems.
4. Circular linked lists are commonly used to implement circular buffers,

Example:
Perform this example and show the result.
// C++ program to delete a given key from free(*head);
// linked list. *head = NULL;
#include <bits/stdc++.h> return;
using namespace std; }

// Structure for a node Node *last = *head, *d;


class Node {
public: // If head is to be deleted
int data; if ((*head)->data == key) {
Node* next;
}; // Find the last node of the list
while (last->next != *head)
// Function to insert a node at the last = last->next;
// beginning of a Circular linked list
void push(Node** head_ref, int data) // Point last node to the next of
{ // head i.e. the second node
// of the list
// Create a new node and make head last->next = (*head)->next;
// as next of it. free(*head);
Node* ptr1 = new Node(); *head = last->next;
ptr1->data = data; return;
ptr1->next = *head_ref; }

// If linked list is not NULL then // Either the node to be deleted is


// set the next of last node // not found or the end of list
if (*head_ref != NULL) { // is not reached
while (last->next != *head && last-
// Find the node before head >next->data != key) {
and last = last->next;
// update next of it. }
Node* temp = *head_ref;
while (temp->next != // If node to be deleted was found
*head_ref) if (last->next->data == key) {
temp = temp->next; d = last->next;
temp->next = ptr1; last->next = d->next;
} free(d);
else }
// For the first node else
ptr1->next = ptr1; cout << "Given node is not
found in the list!!!\n";
*head_ref = ptr1; }
}
// Function to print nodes in a given // Driver code
// circular linked list int main()
void printList(Node* head) {
{ // Initialize lists as empty
Node* temp = head; Node* head = NULL;
if (head != NULL) {
do { // Created linked list will be
cout << temp->data << // 2->5->7->8->10
" "; push(&head, 2);
temp = temp->next; push(&head, 5);
} while (temp != head); push(&head, 7);
} push(&head, 8);
push(&head, 10);
cout << endl;
} cout << "List Before Deletion: ";
printList(head);
// Function to delete a given node
// from the list deleteNode(&head, 7);
void deleteNode(Node** head, int key)
{ cout << "List After Deletion: ";
// If linked list is empty printList(head);
if (*head == NULL)
return; return 0;
}
// If the list contains only a
// single node
if ((*head)->data == key && (*head)-
>next == *head) {
Lab Task:
T1. Convert Singly Linked List to Circular Linked List:
• Take a singly linked list.
• Traverse to the end of the singly linked list.
• Make the last node's next pointer point to the head of the list.
T2. Circular Doubly Linked List:
Extend the above task to include a previous pointer in the Node class.
T3. Insertion in Circular Linked List:

• Beginning: Link the new node to the current head, traverse to the last node,
update its next to the new node, and make the new node the new head.
• End: Traverse to the last node, link the new node to the head, and update the last
node's next to the new node.
• Middle: Locate the required position and adjust the next pointers of the preceding
and subsequent nodes.
T4. Sorting in a Circular Linked List:

Implement a sorting algorithm, like bubble sort, that works specifically with the circular
linked list structure.
Questions
1. How does a circular linked list (CLL) differ from a standard singly linked list?

2. While traversing a circular linked list, what condition should you check for to ensure you

don't end up in an infinite loop?

3. In a circular linked list, what node does the next pointer of the tail node point to?

You might also like