0% found this document useful (0 votes)
13 views11 pages

i II Ds Unit II Part2

Uploaded by

pkwarrior089
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)
13 views11 pages

i II Ds Unit II Part2

Uploaded by

pkwarrior089
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/ 11

Circular Linked List

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

Inserting At Beginning of the list:


We can use the following steps to insert a new node at beginning 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 –Return last

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

Deleting a node from Beginning of the list:

Prepared by P.Sangeeta - CSE


Algorithm:
Step 1- If last=NULL return last
Step 2 – If there is only one node in linked list, if last->next==last && last->data==val then
Step 2.1 - Set temp=last and last=NULL
Step 2.2 - Release space allocated for temp and return last
Step 3 – To delete node at beginning of linked list, if last->next->data=val then
Step 3.1 - Set temp=last->next
Step 3.2 - Set-last->next=temp->next
Step 3.3 - Release space allocated for temp and return last.

Deleting a node from end of the list:


Algorithm:

Step 1 - To delete node at end of linked list, repeat steps 6 to 8 if last->data==val


Step 1.1 - Set temp=last
Step 1.2 - Set p->next=last->next
Step 1.3 - Set last=p
Step 1.4 - Release space allocated for temp and return last.

Deleting a specific node from the list:


Algorithm:
Step 1 - To delete node at any position in linked list, assign last->next to p
Step 2 - Repeat steps 2 to 4 when p->next!=last
Step 3 - If p->next->data==val then
Step 3.1 - Set temp=p->next
Step 3.2 - Set p->next=temp->next
Step 3.3 - Release space allocated for temp and return last.
Step 4 - Assign p->next to p

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;

Prepared by P.Sangeeta - CSE


do
{ printf("%d ",p->data);
p=p->next;
}while(p!=last->next);
printf("\n");}
Double Linked List
To overcome the disadvantage of single linked list where back traversing is not possible, double
linked list will be used.
Definition: Double linked list is a sequence of elements in which every element has links to its previous
element and next element in the sequence. So, we can traverse forward by using the next field and can
traverse backward by using the previous field. Every node in a double linked list contains three fields, they
are: previous(link1), data and next(link2).

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();

Prepared by P.Sangeeta - CSE


temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
Inserting at specific location in the list:
We can use the following steps to insert a new node after a node in the double linked list:
Step 1 - Set i=2
Step 2 - Read position in which element is to be inserted.
Step 3 - Set temp2 = head
Step 4 - If pos < 1 || pos >= count + 1 then print out of range error
Step 5 - If head == NULL && pos != 1 then print cannot insert other than first position
Step 6 - If head == NULL && pos == 1 then
Step 6.1 - call create function
Step 6.2 – Set head = temp and temp1 = head
Step 7 – If not Set temp2 = temp2->next when i<pos
Step 7.1– Call Create function
Step 7.2– Set temp->prev = temp2
Step 7.3 – Set temp->next = temp2->next
Step 7.4 – Set temp2->next->prev = temp
Step 7.5 - Set temp2->next = temp

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;

Prepared by P.Sangeeta - CSE


return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
create();
temp->prev = temp2;
temp->next = temp2->next;
temp2->next->prev = temp;
temp2->next = temp;
}
}
Deletion:
In a double linked list, the deletion operation of any node can be performed as:
Step 1 - Set i=1
Step 2 - Read position in which element is to be deleted.
Step 3 - Set temp2 = head
Step 4 - If pos < 1 || pos >= count + 1 then print out of range error.
Step 5 - If head == NULL then print list is empty.
Step 6 – If not Set temp2 = temp2->next when i<pos
Step 7– If i=1 and temp2->next=NULL then
Step 7.1 – Release the space allocated for temp2
Step 7.2 – Set temp2 and head to NULL
Step 8 – If temp2->next=NULL then
Step 8.1 – Set temp2->prev->next = NULL;
Step 8.2 - Release the space allocated for temp2
Step 9 - Set temp2->next->prev = temp2->prev
Step 10 - if i = 1 then set head = temp2->next
Step 11 - Release the space allocated for temp2

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");

Prepared by P.Sangeeta - CSE


return;
}
else
{
while (i < pos){
temp2 = temp2->next;
i++; }
if (i == 1)
{
if (temp2->next == NULL)
{
printf("Node deleted from list");
free(temp2);
temp2 = head = NULL;
return;
}
}
if (temp2->next == NULL)
{
temp2->prev->next = NULL;
free(temp2);
printf("Node deleted from list");
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next; /* Might not need this statement if i == 1 check */
if (i == 1)
head = temp2->next;
printf("\n Node deleted");
free(temp2);
}
count--;
}
Displaying a Double Linked List:
We can use the following steps to display the elements of a double linked list:
Step 1 - Check whether list is Empty (temp2 == NULL)
Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function.
Step 3 - If it is not Empty, then define a Node pointer 'temp2' and initialize with head.
Step 4 - Keep displaying temp2 → data with an arrow (<===>) until temp2 reaches to the last node
Step 5 - Finally, display temp2 → data with arrow pointing to NULL (temp → data ---> NULL).
Source Code:
void display()
{
temp2 = head;
if (temp2 == NULL)
{
printf("Error : Linked List is empty\n");
return;

Prepared by P.Sangeeta - CSE


}
printf("\n Elements in Linked List are :\n");
while (temp2->next != NULL)
{
printf(" %d ", temp2->n);
temp2 = temp2->next;
}
printf(" %d ", temp2->n);}

Prepared by P.Sangeeta - CSE

You might also like