Week4 Linked List Variants Circular Linked List 19102022 121122pm
Week4 Linked List Variants Circular Linked List 19102022 121122pm
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
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