i II Ds Unit II Part2
i II Ds Unit II Part2
In single linked list, every node points to its next node in the sequence and the last node points to NULL.
But in circular linked list, every node points to its next node in the sequence but the last node points to the
first node in the list.
Definition: A circular linked list is a sequence of elements in which every element has a link to its next
element in the sequence and the last element has a link to the first element.
That means circular linked list is similar to the single linked list except that the last node points to the first
node in the list
Example:
Creation:
In order to create a linked list, source code is as follows:
struct node *create_list(struct node *last)
{
int i,n,val;
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
last=NULL;
if(n==0)
return last;
printf("Enter the element to be inserted : ");
scanf("%d",&val);
last=addtoempty(last,val);
for(i=2;i<=n;i++)
{
printf("Enter the element to be inserted : ");
scanf("%d",&val);
last=insert_end(last,val);
}
return last;
}
struct node *addtoempty(struct node *last,int val)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=val;
last=temp;
last->next=last;
return last;
}
Operations:
In a circular linked list, we perform the following operations:
1. Insertion
2. Deletion
3. Display
Insertion:
Prepared by P.Sangeeta - CSE
In a circular linked list, the insertion operation can be performed in three ways. They are:
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Source code:
struct node *insert_beg(struct node *last,int data)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=val;
temp->next=last->next;
last->next=temp;
return last;
}
Inserting At End of the list
We can use the following steps to insert a new node at end of the circular linked list...
Step 1 - Create a newnode named temp.
Step 2 – Assign a value to temp->data.
Step 3 – Set temp->next= last->next and last->next=temp
Step 4 – Assign temp to last.
Step 5 - Return last
Source Code:
struct node *insert_end(struct node *last,int data)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=val;
temp->next=last->next;
last->next=temp;
last=temp;
Prepared by P.Sangeeta - CSE
return last;
}
Inserting a specific node in the list:
We can use the following steps to insert a new node after a node in the circular linked list...
Step 1 – Assign last->next to p
Step 2 - Repeat steps 3 & 4 when p!=last->next
Step 3 - If p->data = item then
Step 3.1 - Assign a value to temp->data.
Step 3.2 - Set temp>next = p->next
Step 3.3 - Set p->next=temp
Step 3.4 – If p=last, set last=temp
Step 3.5 – return last
Step 4 – Set p=p->next
Source Code:
struct node *insert_pos(struct node *last,int data,int item)
{
struct node *temp,*p;
p=last->next;
do
{
if(p->data==item)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->data=val;
temp->next=p->next;
p->next=temp;
if(p==last)
last=temp;
return last;
}
p=p->next;
}while(p!=last->next);
printf("%d not present in the list\n",item);
return last;
}
Deletion:
In a circular linked list, the deletion operation can be performed in three ways those are as follows:
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
Source Code:
struct node *del(struct node *last,int val)
{
struct node *temp,*p;
if(last==NULL)
{
printf("List is empty\n");
return last;
}
/*Deletion of only node*/
if(last->next==last && last->data==val)
{
temp=last;
last=NULL;
free(temp);
return last;
}
/*Deletion of first node*/
Prepared by P.Sangeeta - CSE
if(last->next->data==val)
{
temp=last->next;
last->next=temp->next;
free(temp);
return last;
}
/*Deletion in between*/
p=last->next;
while(p->next!=last)
{
if(p->next->data==val)
{
temp=p->next;
p->next=temp->next;
free(temp);
return last;
}
p=p->next;
}
/*Deletion of last node*/
if(last->data==val)
{
temp=last;
p->next=last->next;
last=p;
free(temp);
return last;
}
printf("\nElement %d not found\n",val);
return last;
}
Display:
Algorithm:
Step 1 - If last=NULL print list is empty.
Step 2 - Set p=last->next
Step 3 - Repeat steps 4 to 5 when p!=last->next
Step 4 - print p->data
Step 5 - Set p=p->next
Source Code:
void display(struct node *last)
{
struct node *p;
if(last==NULL){
printf("\nList is empty\n");
return; }
p=last->next;
Here, 'link1' field is used to store the address of the previous node in the sequence, 'link2' field is used to
store the address of the next node in the sequence and 'data' field is used to store the actual value of that
node.
Example
In double linked list, the first node must be always pointed by head.
Always the previous field of the first node must be NULL.
Always the next field of the last node must be NULL.
Creation:
Linked List creation can be implemented as follows:
void create()
{
int data;
temp =(struct node *)malloc(1*sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\n Enter value to node : ");
scanf("%d", &data);
temp->n = data;
count++;
}
Operations on Double Linked List:
In a double linked list, we perform the following operations...
1. Insertion
2. Deletion
3. Display
Insertion:
In a double linked list, the insertion operation can be performed in three ways as follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Inserting node at Beginning of the list:
Prepared by P.Sangeeta - CSE
We can use the following steps to insert a new node at beginning of the double linked list:
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, call create function, assign temp to head and head to temp1.
Step 3 - If it is not Empty then, assign head to temp → next and temp to head->prev.
Step 4 – Set head = temp.
Source Code:
void insert_beg()
{
if (head== NULL)
{
create();
head = temp;
temp1 = head;
}
else
{
create();
temp->next = head;
head->prev = temp;
head = temp;
}
}
Inserting node at End of the list:
We can use the following steps to insert a new node at end of the double linked list:
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, call create function, assign temp to head and head to temp1.
Step 3 - If it is not Empty then, assign temp to temp1 → next and temp1 to temp->prev.
Step 4 – Set temp1 = temp.
Source Code:
void insert end()
{
if (head == NULL)
{
create();
head = temp;
temp1 = head;
}
else
{
create();
Source Code:
void insert_pos()
{
int pos, i = 2;
printf("\n Enter position to be inserted : ");
scanf("%d", &pos);
temp2 = head;
if ((pos < 1) || (pos >= count + 1))
{
printf("\n Error: Position out of range to insert");
return;
}
if ((head == NULL) && (pos != 1))
{
printf("\n Error : Empty list cannot insert other than first position");
return;
}
if ((head == NULL) && (pos == 1))
{
create();
head = temp;
temp1 = head;
Source Code:
void delete()
{
int i = 1, pos;
printf("\n Enter position to be deleted : ");
scanf("%d", &pos);
temp2 = head;
if ((pos < 1) || (pos >= count + 1))
{
printf("\n Error : Position out of range to delete");
return;
}
if (head == NULL)
{
printf("\n Error : Empty list no elements to delete");