Circular Linked List

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 59

Department of

Computer Science and Engineering

DATA STRUCTURES
Unit 1-Linear structures
Year/Sem : II/III
Subject Code: 1151CS102
Topic : Circular Linked list
Faculty Name : Mrs.S.Vijitha
Date : 13.08.2020
School of Computing
Vel Tech Rangarajan Dr. Sagunthala R&D Institute of
Science and Technology
Contents

 Linked list - Definition


 Circular linked list
 Circular linked list uses
 Operations of circular linked list
 Types of circular linked list
 Circular singly linked list
 Circular doubly linked list

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Linked list - Definition
Definition:

 A linked list is a sequence of data structures, which


are connected together via links.

 Linked List is a sequence of links which contains


items.

 Each link contains a connection to another link.

 Unlike arrays, Management


linked
and Project
list elements are not stored at a
contiguous location;
(SEPM)
the elements are linked using
pointers.
07/09/2021 Department of Computer Science and Engineering
Linked list - Definition

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Circular Linked list
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.

 Circular Linked List is a variation of Linked list in


which the first element points to the last element
and the last element points to the first element.

 That means circular linked list is similar to the single


linked list except that the last node points to the first
node in the list.
07/09/2021
Department of Computer Science and Engineering
Circular Linked list

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Circular Linked list uses

 To represent arrays that are naturally circular, e.g.

the corners of a polygon, a pool of buffers.

 With a circular list, a pointer to the last node gives

easy access also to first node, by following one link.

 Some problems are circular and a circular data

structure would be more natural when used to


07/09/2021
Department of Computer Science and Engineering
Operations of Circular Linked list
In a circular linked list, we perform the following
operations...

1.Insertion
2.Deletion
3.Display

 Before we implement actual operations, first we need


to setup empty list.

 First perform the following steps before implementing


actual operations.
07/09/2021
Department of Computer Science and Engineering
Operations of Circular Linked list
Step 1 - Include all the header files which are used in
the program.

Step 2 - Declare all the user defined functions.

Step 3 - Define a Node structure with two


members data and next.

Step 4 - Define a Node pointer 'head' and set it NULL.

Step 5 - Implement the main method by displaying


operations.
07/09/2021
Department of Computer Science and Engineering
Insertion Operation of Circular Linked list

Insertion:

In a circular linked list, the insertion operation can be


performed in three ways. They are as follows.

 Inserting At Beginning of the list


 Inserting At End of the list
 Inserting At Specific location in the list

07/09/2021
Department of Computer Science and Engineering
Insertion Operation of Circular Linked list
Inserting At Beginning of the list:

07/09/2021
Department of Computer Science and Engineering
Insertion Operation of Circular Linked list
Inserting At Beginning of the list:

Step 1 - Create a newNode with given value.

Step 2 - Check whether list is Empty (head == NULL)

Step 3 - If it is Empty then,


set head = newNode and newNode→next = head .

Step 4 - If it is Not Empty then, define a Node pointer


'temp' and initialize with 'head'.

07/09/2021
Department of Computer Science and Engineering
Insertion Operation of Circular Linked list
Inserting At Beginning of the list:

Step 5 - Keep moving the 'temp' to its next node until it


reaches to the last node (until 'temp → next == head’).

Step 6 - Set 'newNode → next =head',


'head = newNode' and 'temp → next = head'.

07/09/2021
Department of Computer Science and Engineering
Insertion Operation of Circular Linked list
Inserting At end of the list:

07/09/2021
Department of Computer Science and Engineering
Insertion Operation of Circular Linked list

Inserting At End of the list:

Step 1 - Create a newNode with given value.

Step 2 - Check whether list is Empty (head == NULL).

Step 3 - If it is Empty then,


set head = newNode and newNode → next = head.

Step 4 - If it is Not Empty then, define a node


pointer temp and initialize with head.
07/09/2021
Department of Computer Science and Engineering
Insertion Operation of Circular Linked list

Inserting At End of the list:

Step 5 - Keep moving the temp to its next node until it


reaches to the last node in the list (until temp →
next == head).

Step 6 - Set temp → next = newNode and newNode →


next = head.

07/09/2021
Department of Computer Science and Engineering
Insertion Operation of Circular Linked list
Inserting At Specific location in the list (After a
Node)

07/09/2021
Department of Computer Science and Engineering
Insertion Operation of Circular Linked list
Inserting At Specific location in the list (After a
Node)

Step 1 - Create a newNode with given value.

Step 2 - Check whether list is Empty (head == NULL)

Step 3 - If it is Empty then,


set head = newNode and newNode → next = head.

Step 4 - If it is Not Empty then, define a node


pointer temp and initialize with head.
07/09/2021
Department of Computer Science and Engineering
Insertion Operation of Circular Linked list
Inserting At Specific location in the list (After a
Node)

Step 5 - Keep moving the temp to its next node until it


reaches to the node after which we want to insert the
newNode (until temp1 → data is equal to location, here
location is the node value after which we want to insert
the newNode).

Step 6 - Every time check whether temp is reached to


the last node or not. If it is reached to last node then
display 'Given node is not found in the list!!!
Insertion not possible!!!' and terminate the function.
07/09/2021
Department of Computer Science and Engineering
Insertion Operation of Circular Linked list

Otherwise move the temp to next node.

Step 7 - If temp is reached to the exact node after which


we want to insert the newNode then check whether it is
last node (temp → next == head).

Step 8 - If temp is last node then set temp →


next = newNode and newNode → next = head.

Step 8 - If temp is not last node then set newNode →


next = temp → next and temp → next = newNode.
.07/09/2021
Department of Computer Science and Engineering
Deletion Operation of Circular Linked list

Deletion:

In a circular linked list, the deletion operation can be


performed in three ways. They are as follows.

 Deleting At Beginning of the list


 Deleting At End of the list
 Deleting At Specific location in the list

07/09/2021
Department of Computer Science and Engineering
Deletion Operation of Circular Linked list
Deleting from Beginning of the list:

07/09/2021
Department of Computer Science and Engineering
Deletion Operation of Circular Linked list
Deleting from Beginning of the list:

Step 1 - Check whether list is Empty (head == NULL)

Step 2 - If it is Empty then, display 'List is Empty!!!


Deletion is not possible' and terminate the function.

Step 3 - If it is Not Empty then, define two Node


pointers 'temp1' and 'temp2' and initialize both 'temp1'
and 'temp2' with head.

Step 4 - Check whether list is having only one node


(temp1 → next == head)
07/09/2021
Department of Computer Science and Engineering
Deletion Operation of Circular Linked list

Deleting from Beginning of the list:

Step 5 - If it is TRUE then set head = NULL and


delete temp1 (Setting Empty list conditions)

Step 6 - If it is FALSE move the temp1 until it reaches to


the last node. (until temp1 → next == head )

Step 7 - Then set head = temp2 → next, temp1 →


next = head and delete temp2.

07/09/2021
Department of Computer Science and Engineering
Deletion Operation of Circular Linked list
Deleting at the end of the list:

07/09/2021
Department of Computer Science and Engineering
Deletion Operation of Circular Linked list
Deleting at the end of the list:

Step 1 - Check whether list is Empty (head == NULL)

Step 2 - If it is Empty then, display 'List is Empty!!!


Deletion is not possible' and terminate the function.

Step 3 - If it is Not Empty then, define two Node


pointers 'temp1' and 'temp2' and initialize 'temp1'
with head.

Step 4 - Check whether list has only one Node (temp1


→ next == head)
07/09/2021
Department of Computer Science and Engineering
Deletion Operation of Circular Linked list
Deleting at the end of the list:

Step 5 - If it is TRUE. Then, set head = NULL and


delete temp1. And terminate from the function.
(Setting Empty list condition)

Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and


move temp1 to its next node. Repeat the same
until temp1 reaches to the last node in the list.
(until temp1 → next == head)

Step 7 - Set temp2 → next = head and delete temp1.


07/09/2021
Department of Computer Science and Engineering
Deletion Operation of Circular Linked list

Deleting a Specific Node from the list:

07/09/2021
Department of Computer Science and Engineering
Deletion Operation of Circular Linked list

Deleting a Specific Node from the list:

Step 1 - Check whether list is Empty (head == NULL)

Step 2 - If it is Empty then, display 'List is Empty!!!


Deletion is not possible' and terminate the function.

Step 3 - If it is Not Empty then, define two Node


pointers 'temp1' and 'temp2' and initialize 'temp1'
with head.

07/09/2021
Department of Computer Science and Engineering
Deletion Operation of Circular Linked list
Deleting a Specific Node from the list:
Step 4 - Keep moving the temp1 until it reaches to the
exact node to be deleted or to the last node. And every
time set 'temp2 = temp1' before moving the 'temp1' to
its next node.

Step 5 - If it is reached to the last node then


display 'Given node not found in the list! Deletion not
possible!!!'. And terminate the function.

Step 6 - If it is reached to the exact node which we want


to delete, then check whether list is having only one
node (temp1 → next == head)
07/09/2021
Department of Computer Science and Engineering
Deletion Operation of Circular Linked list
Deleting a Specific Node from the list:

Step 7 - If list has only one node and that is the node to
be deleted then set head = NULL and
delete temp1 (free(temp1)).

Step 8 - If list contains multiple nodes then check


whether temp1 is the first node in the list (temp1 ==
head).

Step 9 - If temp1 is the first node then


set temp2 = head and keep moving temp2 to its next
node until temp2 reaches to the last node. Then
07/09/2021
Department of Computer Science and Engineering
Deletion Operation of Circular Linked list
Deleting a Specific Node from the list:

set head = head → next, temp2 → next = head and


delete temp1.

Step 10 - If temp1 is not first node then check whether it


is last node in the list (temp1 → next == head).

Step 11 - If temp1 is not first node and not last node


then set temp2 → next = temp1 → next and
delete temp1 (free(temp1)).

07/09/2021
Department of Computer Science and Engineering
Display Operation of Circular Linked list
Displaying a circular Linked List:

Step 1 - Check whether list is Empty (head == NULL)

Step 2 - If it is Empty, then display 'List is


Empty!!!' and terminate the function.

Step 3 - If it is Not Empty then, define a Node


pointer 'temp' and initialize with head.

Step 4 - Keep displaying temp → data with an arrow (---


>) until temp reaches to the last node.
07/09/2021
Department of Computer Science and Engineering
Display Operation of Circular Linked list
Displaying a circular Linked List:

Step 5 - Finally display temp → data with arrow pointing


to head → data.

07/09/2021
Department of Computer Science and Engineering
Types of circular Linked list

Types of circular linked list:

A circular linked list can be a two types:

 Singly circular linked list.


 Doubly circular linked list.

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Singly circular Linked list

Singly circular 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 a circular singly linked list until we reach


the same node where we started.

 The circular singly linked list has no beginning and no


ending. There isand no
Projectnull value present in the next part
Management
of any of the nodes.
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Implementation
Implementation:

 To implement a circular singly linked list, we take an


external pointer that points to the last node of the list.

 If we have a pointer last pointing to the last node,


then last -> next will point to the first node.

07/09/2021 Department of Computer Science and Engineering


Representation

07/09/2021 Department of Computer Science and Engineering


Why have we taken pointer that points to last node

 For insertion of node in the beginning we need


traverse the whole list.

 Also, for insertion and the end, the whole list has to
be traversed. If instead of start pointer we take a
pointer to the last node then in both the cases there
won’t be any need to traverse the whole list.

 So insertion in the beginning or at the end takes


constant time irrespective of the length of the list.

07/09/2021 Department of Computer Science and Engineering


Insertion in circular singly linked list
Insertion

A node can be added in three ways:

 Insertion at the beginning of the list


 Insertion at the end of the list
 Insertion in between the nodes

07/09/2021 Department of Computer Science and Engineering


Insertion in circular singly linked list
Insertion at the beginning of the list:

To Insert a node at the beginning of the list,

1. Create a node, say T.

2. Make T -> next = last -> next.

3. last -> next = T.

07/09/2021 Department of Computer Science and Engineering


Insertion in circular singly linked list
Insertion at the beginning of the list:

After insertion,

07/09/2021 Department of Computer Science and Engineering


Insertion in circular singly linked list
struct Node *addBegin(struct Node *last, int data)
{
if (last == NULL)
return addToEmpty(last, data);
// Creating a node dynamically.
struct Node *temp
= (struct Node *)malloc(sizeof(struct Node));
// Assigning the data.
temp -> data = data;
// Adjusting the links
temp -> next = last -> next;
last -> next = temp;
return last;
}
07/09/2021 Department of Computer Science and Engineering
Insertion in circular singly linked list
Insertion at the end of the list:

To Insert a node at the end of the list, follow these


step:

1. Create a node, say T.


2. Make T -> next = last -> next;
3. last -> next = T.
4. last = T.

07/09/2021 Department of Computer Science and Engineering


Insertion in circular singly linked list

After inserting

07/09/2021 Department of Computer Science and Engineering


Insertion in circular singly Linked list
struct Node *addEnd(struct Node *last, int data)
{
if (last == NULL)
return addToEmpty(last, data);
// Creating a node dynamically.
struct Node *temp =
(struct Node *)malloc(sizeof(struct Node));
// Assigning the data.
temp -> data = data;
// Adjusting the links.
temp -> next = last -> next;
last -> next = temp; and Project
last = temp; Management
(SEPM)
return last;
}
07/09/2021 Department of Computer Science and Engineering
Insertion in circular singly Linked list
Insertion in between the nodes:

1. Create a node, say T.

2. Search the node after which T need to be insert, say


that node be P.

3. Make T -> next = P -> next;


and Project
Management
4. P -> next = T. (SEPM)

07/09/2021 Department of Computer Science and Engineering


Insertion in circular singly Linked list
Suppose 12 need to be insert after node having value
10,

After insertion

07/09/2021 Department of Computer Science and Engineering


Circular doubly linked list
Circular Doubly Linked List:

 Circular Doubly Linked List has properties of both


doubly linked list and circular linked list.

 It has two consecutive elements are linked or


connected by previous and next pointer.

 The last node points to first node by next pointer and


also the first node points to last node by previous
and Project
Management
pointer. (SEPM)

07/09/2021 Department of Computer Science and Engineering


Circular doubly linked list
Circular Doubly Linked List:

and Project
Management
(SEPM)

07/09/2021 Department of Computer Science and Engineering


Insertion in circular doubly linked list
Insertion

A node can be added in three ways:

 Insertion at the beginning of the list


 Insertion at the end of the list
 Insertion in between the nodes

07/09/2021 Department of Computer Science and Engineering


Insertion in circular doubly linked list
Insertion at the beginning of the list: 

 To insert a node at the beginning of the list, create a


node(Say T) with data = 5, T next pointer points to
first node of the list.

 T previous pointer points to last node the list, last


node’s next pointer points to this T node.

 First node’s previous pointer also points this T node


and at last don’t forget to shift ‘Start’ pointer to this T
node.
07/09/2021 Department of Computer Science and Engineering
Insertion in circular doubly linked list
Insertion at the beginning of the list: 

07/09/2021 Department of Computer Science and Engineering


Insertion in circular doubly linked list
void insertBegin(struct Node** start, int value)
{
// Pointer points to last Node
struct Node *last = (*start)->prev;
struct Node* new_node = new Node;
new_node->data = value; // Inserting the data
// setting up previous and next of new node
new_node->next = *start;
new_node->prev = last;
// Update next and previous pointers of start and last.
last->next = (*start)->prev = new_node;
// Update start pointer
*start = new_node;
}
07/09/2021 Department of Computer Science and Engineering
Insertion in circular doubly linked list
Insertion at the end of list or in an empty list:

 Empty List (start = NULL): A node(Say N) is


inserted with data = 5, so previous pointer of N points
to N and next pointer of N also points to N.

 But now start pointer points to the first node the list.

07/09/2021 Department of Computer Science and Engineering


Insertion in circular doubly linked list

07/09/2021 Department of Computer Science and Engineering


Insertion in circular doubly linked list
Insertion in between the nodes of the list:

 To insert a node in between the list, two data values


are required one after which new node will be
inserted and another is the data of the new node.

07/09/2021 Department of Computer Science and Engineering


Insertion in circular doubly linked list
Insertion in between the nodes of the list:

07/09/2021 Department of Computer Science and Engineering


Thank You

Department of Computer Science and Engineering

You might also like