Data Stucture
Data Stucture
UNIVERSITY 28101623049
ROLL NUMBER
REGISTRATION 232810120247
NUMBER
DEPARTMENT ELECTRICAL ENGINEERING
Operations on circular linked lists can be performed exactly like a singly linked list. It’s just that
we have to maintain an extra pointer to check if we have gone through the list once.
Traversal:
Traversal in a circular linked list can be achieved by creating a new struct Node*
pointer p, which starts from the head and goes through the list until it points again at
the head. So, this is how we go through this circle only once, visiting each node.
And since traversal is achieved, all the other operations in a circular linked list
become as easy as doing things in a linear linked list.
One thing that may have sounded confusing to you is that there is a head but no
starting of this circular linked list. Yes, that is the case; we have this head pointer
just to start or incept in this list and for our convenience while operating on it. There
is no first element here.
So this was introducing you all to this variant of linked lists. I would like you all to implement
other operations on your own. Our next tutorial will cover them anyway.
Thank you for being with me throughout. I hope you enjoyed the tutorial. If you appreciate my
work, please let your friends know about this course too. Don’t forget to download the notes
from the link below.
Circular Linked Lists: Operations in C Language
In the last tutorial, we learned about this new data structure, the circular
linked lists. Additionally, we discussed the difference and similarities between
a circular linked list and a linear linked list.
Let me quickly summarize some of the most important points:
We even learned traversing through the circular linked list using the do-while
approach. Today, we’ll see one of the operations, insertion in a doubly-linked
list with the help of C language.
Now, let's move on to the coding part. I have attached the snippet below.
Refer to it while understanding the steps.
3. struct Node
4. {
5. int data;
6. struct Node *next;
7. };
8. int main(){
9.
10. struct Node *head;
11. struct Node *second;
12. struct Node *third;
13. struct Node *fourth;
14.
15. // Allocate memory for nodes in the linked list in Heap
16. head = (struct Node *)malloc(sizeof(struct Node));
17. second = (struct Node *)malloc(sizeof(struct Node));
18. third = (struct Node *)malloc(sizeof(struct Node));
19. fourth = (struct Node *)malloc(sizeof(struct Node));
20.
21. // Link first and second nodes
22. head->data = 4;
23. head->next = second;
24.
25. // Link second and third nodes
26. second->data = 3;
27. second->next = third;
28.
29. // Link third and fourth nodes
30. third->data = 6;
31. third->next = fourth;
32.
33. // Terminate the list at the third node
34. fourth->data = 1;
35. fourth->next = head;
36.
37. return 0;
38. }
1. I’ll just cover the insertion part, and that too on the head. Rest of the variations, I
believe, you’ll be able to do yourselves. Things are very similar to that of singly-
linked lists.
2. Create a struct Node* function insertAtFirst which will return the pointer to the new
head.
3. We’ll pass the current head pointer and the data to insert at the beginning, in the
function.
4. Create a new struct Node* pointer ptr, and assign it a new memory location in the
heap. This is our new node pointer. Make sure you don't forget to include the header
file <stdlib.h>.
5. Create another struct node * pointer p pointing to the next of the head. p = head->
next.
6. Run a while loop until the p pointer reaches the end element and p-> next becomes
the head.
9.Return head.
p->next = ptr;
ptr->next = head;
head = ptr;
return head;
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
p->next = ptr;
ptr->next = head;
head = ptr;
return head;
int main(){
return 0;
}
As you can see, all the elements we passed into the insert At First function got added at the
beginning. So, it is indeed working.
And this was all about a circular linked list. We won’t go doing other operations. You should all
carry out other operations yourselves. Believe me, it ain’t a tough job. We have next in the
series another variant of a linked list called the doubly linked lists.