0% found this document useful (0 votes)
93 views17 pages

9.circular Linked List

A circular linked list is a linked list where the last node points back to the first node, forming a circular chain. This allows traversal of the list to begin from the last node by following the links. Operations like insertion and deletion of nodes require updating the next pointer of the preceding node and revising any rear pointer. Code implementations in C/C++ demonstrate how to perform these operations on a circular linked list.

Uploaded by

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

9.circular Linked List

A circular linked list is a linked list where the last node points back to the first node, forming a circular chain. This allows traversal of the list to begin from the last node by following the links. Operations like insertion and deletion of nodes require updating the next pointer of the preceding node and revising any rear pointer. Code implementations in C/C++ demonstrate how to perform these operations on a circular linked list.

Uploaded by

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

Circular

Linked List
Circular Linked Lists
 A Circular Linked List is a special type of Linked List
 It supports traversing from the end of the list to the
beginning by making the last node point back to the
head of the list
 A Rear pointer is often used instead of a Head pointer

10 20 40 55 70

Rear
Motivation

 Circular linked lists are usually sorted


 Circular linked lists are useful for playing
video and sound files in “looping” mode
Circular Linked List Definition
struct Node{
int data;

// record / individual data type

Node* next;
};
typedef Node* NodePtr;
Circular Linked List Operations

 insertNode(NodePtr& Rear, int item)


//add new node to ordered circular linked list

 deleteNode(NodePtr& Rear, int item)


//remove a node from circular linked list

 print(NodePtr Rear)
//print the Circular Linked List once
Traverse the list
(Sample code in C++, but required implementation in
C only)
void print(NodePtr Rear){
NodePtr Cur;
if(Rear != NULL){
Cur = Rear->next;
do{
cout << Cur->data << " ";
Cur = Cur->next;
}while(Cur != Rear->next);
cout << endl;
}
}
10 20 40 55 70

Rear
Insert Node
 Insert into an empty list

NotePtr New = new Node;


New->data = 10;

Rear = New;
Rear->next = Rear;
10

New Rear
 Insert to head of a Circular Linked List
New->next = Cur; // same as: New->next = Rear->next;
Prev->next = New; // same as: Rear->next = New;

10 20 40 55 70

New Cur Prev


Rear
 Insert to middle of a Circular Linked List
between Pre and Cur
New->next = Cur;
Prev->next = New;

10 20 55 70

40
Prev Cur Rear

New
 Insert to end of a Circular Linked List
New->next = Cur; // same as: New->next = Rear->next;
Prev->next = New; // same as: Rear->next = New;
Rear = New;

10 20 40 55 70

Cur Prev New


Rear
void insertNode(NodePtr& Rear, int item){
NodePtr New, Cur, Prev;
New = new Node;
New->data = item;
if(Rear == NULL){ // insert into empty list
Rear = New;
Rear->next = Rear;
return;
}
Prev = Rear;
Cur = Rear->next;
do{ // find Prev and Cur
if(item <= Cur->data)
break;
Prev = Cur;
Cur = Cur->next;
}while(Cur != Rear->next);
New->next = Cur; // revise pointers
Prev->next = New;
if(item > Rear->data) //revise Rear pointer if adding to end
Rear = New;
}
 Delete a node from a single-node Circular
Linked List
Rear = NULL;
delete Cur;

10

Rear = Cur = Prev


Delete Node
 Delete the head node from a Circular Linked List
Prev->next = Cur->next; // same as: Rear->next = Cur-
>next

delete Cur;

10 20 40 55 70

Rear Prev
Cur
 Delete a middle node Cur from a Circular
Linked List
Prev->next = Cur->next;
delete Cur;

10 20 40 55 70

Prev Cur Rear


 Delete the end node from a Circular Linked List
Prev->next = Cur->next; // same as: Rear->next;

delete Cur;
Rear = Prev;

10 20 40 55 70

Prev Cur
Rear
void deleteNode(NodePtr& Rear, int item){
NodePtr Cur, Prev;
if(Rear == NULL){
cout << "Trying to delete empty list" << endl;
return;
}
Prev = Rear;
Cur = Rear->next;
do{ // find Prev and Cur
if(item <= Cur->data) break;
Prev = Cur;
Cur = Cur->next;
}while(Cur != Rear->next);
if(Cur->data != item){ // data does not exist
cout << "Data Not Found" << endl;
return;
}
if(Cur == Prev){ // delete single-node list
Rear = NULL;
delete Cur;
return;
}
if(Cur == Rear) // revise Rear pointer if deleting end
Rear = Prev;
Prev->next = Cur->next; // revise pointers
delete Cur;
}
void main(){
NodePtr Rear = NULL;

insertNode(Rear, 3);
insertNode(Rear, 1);
insertNode(Rear, 7);
insertNode(Rear, 5);
insertNode(Rear, 8); Result is:
print(Rear); 13578
deleteNode(Rear, 1); 57
deleteNode(Rear, 3); 1578
deleteNode(Rear, 8);
print(Rear);
insertNode(Rear, 1);
insertNode(Rear, 8);
print(Rear);
}

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