0% found this document useful (0 votes)
55 views13 pages

Week4 Linked List Variants Circular Linked List 19102022 121122pm

The document discusses circular linked lists. A circular linked list is a linked list where the last node points to the first node, forming a loop. This means there is no beginning or end to the list. Popular applications of circular linked lists include task scheduling in operating systems and web browser history. The document then provides implementations for common circular linked list operations like insertion, deletion, and traversal.

Uploaded by

Shazar Naeem
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)
55 views13 pages

Week4 Linked List Variants Circular Linked List 19102022 121122pm

The document discusses circular linked lists. A circular linked list is a linked list where the last node points to the first node, forming a loop. This means there is no beginning or end to the list. Popular applications of circular linked lists include task scheduling in operating systems and web browser history. The document then provides implementations for common circular linked list operations like insertion, deletion, and traversal.

Uploaded by

Shazar Naeem
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/ 13

(CSC-221)

Data Structures &


Algorithms
(Fall 2022)
COURSE INSTRUCTOR :
MOMINA MOETESUM
Week 4:
C I RCULAR LI NKED LI S T
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 liked list has no beginning and no ending. There is no null value present in the next part of any
of the nodes.
There are many examples where the circular
linked list is being used in computer science
including:

Popular ◦ Task maintenance in operating systems

Applications
◦ Browser surfing where a record of pages
visited in the past by the user, is maintained
in the form of circular linked lists and can be
accessed again by clicking the previous
button
Circular linked list

A linked list in which the last node points to the first node is
called a circular linked list

head 9 17 22 26 34

5
Circular linked list
head Empty list

head

data
One node in list
next

head
Multiple nodes in list

data data data

next next next

6
Circular Linked list - Implementation
class List{ CircList::CircList
class Node{
public:
Node *head; {
public:
CircList(); head=NULL;
int data; void insert_beg(int);
};
void insert_end(int);
Node* next; void insert_after(int, int);
void delete_beg();
}; void delete_end();
void delete_node(int);
void traverse();
bool isEmpty();
};
Circular Linked list - Implementation
void CircList::traverse() bool list:: isEmpty(){
{
if (head==NULL)
Node *temp = head;
if (head!= NULL) return True;
{
else
do
{ return False;
cout<<temp->data;
temp=temp->next; };
}
while(temp != head);
} head 9 17 22 26 34
}

8
Circular Linked list - Implementation
void CircList::add_begin(int data){ void CircList::add_end(int data){
Node *ptr = new Node;
ptr->data = data; Node *ptr = new Node;
ptr->next = NULL; ptr->data = data;
if (head == NULL) ptr->next = NULL;
{ if (head == NULL)
head = ptr; {
ptr->next = head; head = ptr;
} ptr->next = head;
else }
{ else
Node *temp = head; {
while(temp->next!=head) Node *temp = head;
{ while(temp->next!=head)
temp=temp->next; {
} temp=temp->next;
temp->next = ptr; }
ptr->next = head; temp->next = ptr;
head = ptr; ptr->next = head;
} }
} }

9
Circular Linked list - Implementation
void CircList::add(int val,int newVal) {
Node *temp = head;
if (head == NULL)
{
cout<<“List is empty"; return;
}
else if(head!=NULL)
{if(head->data==val){Node *t=new Node; t->data=newVal; t->next=temp->next; temp->next=t;}
else if(head->data!=val){
do{
temp=temp->next;
}while{temp->data!=val && temp!=head);
if(temp==head){cout<<“Node not found”; exit(0);}
else{Node *t=new Node; t->data=newVal; t->next=temp->next; temp->next=t;}
}
return;
}

10
void CircList::del_beg(){ void CircularList::del_end(){
Node *temp; Node *temp;
temp=head; temp=head;
if(head==NULL){ if(head==NULL){
cout<<"\nList has no nodes"; cout<<"\nList has no nodes";
return; return;
} }
//Only one node in list //Only one node in list
if(head->next==head){ if(head->next==head){
head=NULL; head=NULL;
delete temp; delete temp;
return; return;
} }
else{ else{
Node *t=head; Node *t=NULL;
while(temp->next!=head) while(temp->next!=head)
{ {
temp=temp->next; t=temp;
} temp=temp->next;
head=head->next; }
temp->next=head; t->next=temp->next;
delete t; delete temp;
} }
} } 11
void CircList::del(int val)//All conditions satisfied
{
head
Node *temp=head,*t=NULL;
if (head == NULL){cout<<“List is empty"; return;}
if(head->data==val)
{ if(head->next==head){delete p; head=NULL;} v
else{
do{ head
t=temp;
temp=temp->next; head v
}while(temp!=head);
head=head->next;
delete temp;
t->next=head; head v
}
}
else{
do{ head
t=temp; v
temp=temp->next;
}while(temp->data!=val&&temp!=head);
if(temp->data==val) head
{
t-next=temp->next;
delete temp;
}
else{cout<<“Node not found”; return;}
} head 12
Credits
Some of the content of this presentation is inspired by following:

https://www.geeksforgeeks.org/types-of-linked-list/

https://www.javatpoint.com/circular-singly-linked-list

You might also like