Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Data Structures & Algorithms / Data Structures

Hamdard University

LAB SESSION 03
HEADER LINKED LIST &CIRCULAR LINKED LIST
Objectives:
To understand the basic concept of circular linked list.
To understand the operation performed on circular linked list.
Header Linked List:
A header linked list is a linked list which always contains a special node, called the header node, at the
beginning of the list. The following are two kinds of widely used header lists:
A grounded header list is a header list where the last node contains the null pointer.
A circular header list is a header list where the last node points back to the header node.

Circular Linked List
A linked list in which the last node of the list points back to the first node of the list is called circular
linked list. It is difficult to detect the end of a circular linked list. To overcome this problem the end of
list is detected with reference to the pointer fields of nodes. If the pointer field value of any node in the
list is equal to the starting address of the field, that node is the last node of the list.
Operations in circular linked list are similar to those of linear linked list.
Circular Link List With Create, Insert, Delete, Display Operations


Data Structures & Algorithms / Data Structures
Hamdard University

Deleting Node from the Beginning of the Circular Linked List
void delete_first()
{
if(head==NULL)
{
cout<<"Cannot delete!!! Link List is empty. It must have atleast
one node. Call create_circular_ll(int d) to create a list"<<endl;
}
else
{
ptr = head;
temp=head;
int item = head->data;

while(temp->next!=head)
temp=temp->next;
head = head->next;
temp->next=head;
free(ptr);
cout<<"Item deleted"<<endl;
}}

Count Number of Elements of a Circular List
int count()
{
ptr=head;
int c=1;
while(ptr->next!=head)
{
ptr=ptr->next;
c++;
}
cout<<"Number of nodes in circular linked list are: "<<c<<endl;
}


Data Structures & Algorithms / Data Structures
Hamdard University

Circular Double Linked List
A circular double linked list is similar to a circular single linked list. In circular double linked list the next
pointer of the last node is linked to the first node of the list and the previous pointer field of the first
node contains the address of last node.
Lab Exercise:
Instructions:
Make the appropriate functions for the following lab task. Implement the function in main(). Write
the code by hand and take the snapshot of the output screen.
1) Write a program that inserts a node at a specific location of a circular linked list.
2) Write a program that deletes a specific node from circular linked list.
3) Write a program that reverses the circular linked list.
4) Write a program that sorts the circular linked list in ascending order.
5) Write a program that transforms a single linked list into circular linked list.
6) Write a program that transforms the double linked list into a circular double linked list.

You might also like