bit confused in circular linked list implementation and insertion
c - Why exactly do we need a "Circular Linked List" (singly or doubly) data structure? - Stack Overflow
Practical application of circular linked lists
Circular Linked List Question
It’s just a dynamic array. The methods make it operational (push/pop, next).
More on reddit.comI am currently learning Circular linked lists in C++ and the tutorial that I was following said:" we cant traverse like we do in normal Linked lists (using current=current->next) bcz since Linked list is circular we have no idea which node is what"
But we could do it because we have a tail pointer (the tutorial also said the tail matter more than the head in circular LL) and using a tail pointer I can go to any position I want. right?
for ex by: Node* temp = tail->next; //getting first element address
while(temp !=tail) //i.e till aggain it encounter tail
{
//traverse
}when I started searching online more about it, I found 3 things strange
-
why do all other tutorials use a struct to implement linked lists, I mean I am using class and its been much easier (as I also get to initialize newly created nodes with default values using constructors) but I didn't find anyone using class. Edit it's all in c++ code
-
it's related to 1st, why are all using malloc() for dynamic initialization of node objects in C++, again I was using a new keyword for the dynamic creation of nodes and a delete keyword in case of node deletion, but all using malloc() which is C.
-
I didn't get this syntax, can help me understand it
struct node{
int data;
struct node* next; //why we are writing struct here
//shoudnt it be just: node* next; ?
//also while writing function for insertion
struct Node *Start(struct Node *head, int data){}
//again they wrote struct in function decalaration! why?A simple example is keeping track of whose turn it is in a multi-player board game. Put all the players in a circular linked list. After a player takes his turn, advance to the next player in the list. This will cause the program to cycle indefinitely among the players.
To traverse a circular linked list, store a pointer to the first element you see. When you see that element again, you have traversed the entire list.
void traverse(CircularList *c) {
if (is_empty(c)) {
return;
}
CircularList start = c;
do {
operateOnNode(c);
c = c->next;
} while(c != start);
}
Two reasons to use them:
1) Some problem domains are inherently circular.
For example, the squares on a Monopoly board can be represented in a circularly linked list, to map to their inherent structure.
2) Some solutions can be mapped to a circularly linked list for efficiency.
For example, a jitter buffer is a type of buffer that takes numbered packets from a network and places them in order, so that (for example) a video or audio player can play them in order. Packets that are too slow (laggy) are discarded.
This can be represented in a circular buffer, without needing to constantly allocate and deallocate memory, as slots can be re-used once they have been played.
It could be implemented with a linked-list, but there would be constant additions and deletions to the list, rather than replacement to the constants (which are cheaper).