Review
6 BEST (Micro, Small, Mini) GPS Trackers | Reviews for 2021
GPS trackers are the devices used to track the precise location of a vehicle, person, pets,...
A circular linked list is a sequence of nodes arranged such a way that each node can be retraced to itself. Here a "node" is a self-referential element with pointers to one or two nodes in iI'simmediate vicinity.
Below is a depiction of a circular linked list with 3 nodes.
Here, you can see that each node is retraceable to itself. The example shown above is a circular singly linked list.
Note: The most simple circular linked list, is a node which traces only to itself as shown
In this circular linked list tutorial, you will learn:
The basic operations on a circular linked list are:
In the next section, you will understand insertion of a node, and the types of insertion possible in a Circular Singly Linked List.
Initially, you need to create one node which points to itself as shown in this image. Without this node, insertion creates the first node.
Next, there are two possibilities:
For inserting at the beginning/end of the circular linked list, that is at the position where the first-ever node was added,
NOTE: The pointer that is the token master or the beginning/end of the circle can be changed. It will still return to the same node on a traversal, discussed ahead.
Steps in (a) i-iii are shown below:
(Existing node)
STEP 1) Break the existing link
STEP 2) Create a forward link (from new node to an existing node)
STEP 3) Create a loop link to the first node
Next, you will try insertion after a node.
For example, let us insert "VALUE2" after the node with "VALUE0". Let us assume that the starting point is the node with "VALUE0".
NOTE: Since there is a cyclic arrangement, inserting a node involves the same procedure for any node. The pointer that completes a cycle completes the cycle just like any other node.
This is shown below:
(Let us say there are only two nodes. This is a trivial case)
STEP 1) Remove the inner link between the connected nodes
STEP 2) Connect the left-hand side node to the new node
STEP 3) Connect the new node to the right hand side node.
Let us assume a 3-node circular linked list. The cases for deletion are given below:
Deletion at the beginning/end:
(Existing setup)
STEP 1) Remove the circular link
STEPS 2) Remove the link between the first and next, link the last node, to the node following the first
STEP 3) Free /deallocate the first node
Deletion after a node:
STEP 1) Let us say that we need to delete a node with "VALUE1."
STEP 2) Remove the link between the previous node and the current node. Link its previous node with the next node pointed by the current node (with VALUE1).
STEP 3) Free or deallocate the current node.
To traverse a circular linked list, starting from a last pointer, check if the last pointer itself is NULL. If this condition is false, check if there is only one element. Otherwise, traverse using a temporary pointer till the last pointer is reached again, or as many times as needed, as shown in the GIF below.
Some of the advantages of circular linked lists are:
You are encouraged to attempt to read and implement the code below. It presents the pointer arithmetic associated with circular linked lists.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int item;
struct node *next;
};
struct node* addToEmpty(struct node*,int);
struct node *insertCurrent(struct node *, int);
struct node *insertAfter(struct node *, int, int);
struct node *removeAfter(struct node *, int);
struct node *removeCurrent(struct node *);
void peek(struct node *);
int main()
{
...
Explanation of code:
int main()
{
struct node *last = NULL;
last = insertCurrent(last,4);
last = removeAfter(last, 4);
peek(last);
return 0;
}
struct node* addToEmpty(struct node*last, int data)
{
struct node *temp = (struct node *)malloc(sizeof( struct node));
temp->item = data;
last = temp;
last->next = last;
return last;
}
struct node *insertCurrent(struct node *last, int data)
Explanation of code:
struct node *insertCurrent(struct node *last, int data)
{
if(last == NULL)
{
return addToEmpty(last, data);
}
struct node *temp = (struct node *)malloc(sizeof( struct node));
temp -> item = data;
temp->next = last->next;
last->next = temp;
return last;
}
struct node *insertAfter(struct node *last, int data, int item)
{
struct node *temp = last->next, *prev = temp, *newnode =NULL;
…
Explanation of code
...
struct node *insertAfter(struct node *last, int data, int item)
{
struct node *temp = last->next, *prev = temp, *newnode =NULL;
if (last == NULL)
{
return addToEmpty(last, item);
}
do
{
prev = temp;
temp = temp->next;
} while (temp->next != last && temp->item != data );
if(temp->item != data)
{
printf("Element not found. Please try again");
...
Explanation of code:
...
if(temp->item != data)
{
printf("Element not found. Please try again");
return last;
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->item = item;
prev->next = newnode;
newnode->next = temp;
}
return last;
}
struct node *removeCurrent(struct node *last)
...
Explanation of code:
struct node *removeCurrent(struct node *last)
{
if(last == NULL)
{
printf("Element Not Found");
return NULL;
}
struct node *temp = last->next;
last->next = temp->next;
free(temp);
return last;
}
struct node *removeAfter(struct node *last, int data)
Explanation of code
struct node *removeAfter(struct node *last,int data)
{
struct node *temp = NULL,*prev = NULL;
if (last == NULL)
{
printf("Linked list empty. Cannot remove any element\n");
return NULL;
}
temp = last->next;
prev = temp;
do
{
prev = temp;
temp = temp->next;
} while (temp->next != last && temp->item != data );
if(temp->item != data)
{
printf("Element not found");
...
Explanation of code
if(temp->item != data)
{
printf("Element not found");
return last;
}
else
{
prev->next = temp->next;
free(temp);
}
return last;
}
void peek(struct node * last)
{
struct node *temp = last;
if (last == NULL)
{
return;
Explanation of program
...
void peek(struct node * last)
{
struct node *temp = last;
if (last == NULL)
{
return;
}
if(last -> next == last)
{
printf("%d-", temp->item);
}
while (temp != last)
{
printf("%d-", temp->item);
temp = temp->next;
}
}
Explanation of code
GPS trackers are the devices used to track the precise location of a vehicle, person, pets,...
Dailymotion is a popular platform for watching videos online. Because of network connectivity or...
Torrent client is a software for downloading files that utilize a peer to peer system. This...
$20.20 $9.99 for today 4.6 (115 ratings) Key Highlights of Java Programming Language PDF 265+...
Summary of a variable is important to have an idea about the data. Although, summarizing a...
What is Continuous Monitoring? Continuous monitoring is a process to detect, report, respond all...