0% found this document useful (0 votes)
27 views18 pages

Lecture # 5 Circular Link List

Circular Link List in data structure

Uploaded by

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

Lecture # 5 Circular Link List

Circular Link List in data structure

Uploaded by

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

Data Structure and Algorithm

Lecture-5

University of Sialkot
Problems with Linked Lists

-3 4 -1 NULL

header
A node in the list

NULL (An empty list)

header

Two problems – we can’t get back to the beginning of the list


from the end, and we can’t go backwards through the list. So,
circular linked lists and doubly linked lists were invented.
Circular Linked List

• A linked list in which the last node points to the


first node is called a circular linked list

• In a circular linked list with more than one node, it


is convenient to make the pointer first point to
the last node of the list
A Circular Linked List

2 -3 4 -1

header

Have the last node link back to the first (or the header).
Circular Linked Lists
• Circular linked lists avoid the use of null
references in their nodes
• Can be useful for certain algorithms
• Can also make programming simpler: fewer
special cases to consider
• Successor of tail node is the head node; in
doubly-linked list
Circular linked lists
• Insertions and deletions into a circular linked
list follow the same logic patterns used in a
singly linked list except that the last node
points to the first node.
Circular linked list
• Advantage is that we can start searching from any
node of the linked list and get to any other node.

• No logical head and tail pointers. We follow the


conventions as first element inserted into the list is
given status of head and last element entered is
called as a tail.
Circular Linked Lists
• Last node references the first node
• Every node has a successor
• No node in a circular linked list contains NULL

Figure 4.25 A circular linked list


Circular Link List
Description:
• In linear linked lists if a list is traversed (all the
elements visited) a extra pointer to the list must
be preserved in order to be able to reference
the list again.
• Circular linked lists can be used to help the
traverse the same list again and again if needed.
A circular list is very similar to the linear list
where in the circular list the pointer of the last
node points not NULL but the first node
Circular Linked Lists

A Linear Linked List


Circular Linked Lists
Insert At Tail
Void insertattail(int x) Node * n = new node
{ Node *temp = head ;

While (temp->next !=head)


If(head == NULL)
{
Temp = temp->next;
{ }
Insertathead(val) ; n->data = x;
Return ; n->next = head;
Temp->next = n ;
}
}
Insert at Head
Void insertathead(int x) While(temp->nex!=Head)
{ {
Node *n = new node; Temp = temp->next;
}
n->data= x;
If(head == NULL)
Temp->next = n;
{ n->next = head ;
n->next = n ; Head = n ;
Head = n ;
Return ; }
}
Display
Void display()
Void display() {
{ node *temp = head ;
node *temp = head ; Do
While(temp->next!=head) {
{ cout<< temp->data;
cout<< temp->data; Temp= temp->next
Temp= temp->next }
}
cout<< temp->data; While(temp !=head)
cout<< endl;

}
}
Main function
int main()
{
Node * head = Null ;
Insertattail(1);
Insertattail(2);
Insertattail(3);
Display();
Insertathead(4);
Display();
}
Deletion
Void deletion(int position)
{ While(current!=pos-1);
If (pos==1) {
{ Temp = temp->next;
Deleteathead(); current++;
Return; }
}
Node *todelete = temp->next;
Temp->next= temp->next->next;
node *temp = head; delete todelete;
int current = 1 ; }
Delete At head
Void deleteathead() Node * todelete = head ;
{
Node *temp = head; temp->next = head->next ;
While(temp->next !
Head = head-> next;
=head)
{ Delete todelete;
Temp = temp->next;
} }
THANKS

You might also like