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

5 linkedListCircular

A circular linked list is a special type of linked list where the last node points back to the head of the list, allowing traversal from end to beginning. It supports operations like insertion, deletion by revising node pointers and the rear pointer if adding/removing from the end. A rear pointer is often used instead of a head pointer for traversal and updating the list.

Uploaded by

yusdisti99
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)
25 views17 pages

5 linkedListCircular

A circular linked list is a special type of linked list where the last node points back to the head of the list, allowing traversal from end to beginning. It supports operations like insertion, deletion by revising node pointers and the rear pointer if adding/removing from the end. A rear pointer is often used instead of a head pointer for traversal and updating the list.

Uploaded by

yusdisti99
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/ 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
• They are also a stepping stone to
implementing graphs.
Circular Linked List Definition
#include <iostream>
using namespace std;

struct Node{
int data;
Node* next;
};
Circular Linked List Operations

insertNode(int item)
//add new node to ordered circular linked list

deleteNode(int item)
//remove a node from circular linked list

print()
//print the Circular Linked List once
Traverse the list
void print(){
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

Note 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(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(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