Why exactly do we need a "Circular Linked List" (singly or doubly) data structure?

CData StructuresLinked ListCircular List

C Problem Overview


Why exactly do we need a "Circular Linked List" (singly or doubly) data structure?

What problem does it solve that is evident with simple Linked Lists (singly or doubly)?

C Solutions


Solution 1 - C

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

Solution 2 - C

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.

  1. 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).

Solution 3 - C

Something i found from google.

>A singly linked circular list is a linked list where the last node in thelist points to the first node in the list. A circular list does not contain NULL pointers. > >A good example of an application where circular linked list should be used is a timesharing problem solved by the operating system. > >In a timesharing environment, the operating system must maintain a list of present users and must alternately allow each user to use a small slice of CPU time, one user at a time. The operating system will pick a user, let him/her use a small amount of CPU time and then move on to the next user, etc. > >For this application, there should be no NULL pointers unless there is absolutely no one requesting CPU time.

Solution 4 - C

Applications

  1. We can use circular linked list any application where the entries appear in a rotating manner.
  2. Circular linked list is the basic idea of round robin scheduling algorithm.

Solution 5 - C

A circular linked list can be effectively used to create a queue (FIFO) or a deque (efficient insert and remove from front and back). See http://en.wikipedia.org/wiki/Linked_list#Circularly-linked_vs._linearly-linked

Solution 6 - C

A good example of an application where circular linked list should be used is a timesharing problem solved by the operating system.

Solution 7 - C

Circular linked lists are widely used in applications where tasks are to be repeated or in time sharing applications. Circular queue can keep a track of tasks which have been performed and which has to be performed,once the specific task is done it jumps to next one and when whole set of task is conpleted it again jumps to first task to complete the remaining job. In practical use : when you open multiple applications on your system the memory of those applications are saved in a circular fashion, you can observe this if u continuously press win+tab/alt+tab for switching applications. Also in multiplayer board games ,each player are assigned to node in the linked list and the rotation is performed

Solution 8 - C

Circular linked lists (singly or doubly) are useful for applications that need to visit each node equally and the lists could grow. If the size of the list if fixed, it is much more efficient (speed and memory) to use circular queue.

Solution 9 - C

A circular list is simpler than a normal doubly-linked list. Append is just prepend and shift forward once, Pop back is just shift back once and pop front. By tying the two ends together, you get a double-ended list for the cost of just implementing the operations of a one-ended list.

Solution 10 - C

We can use circularly linked list in resource pooling. If many users want to use a shared resource, we can allocate that resource using circularly linked list.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser366312View Question on Stackoverflow
Solution 1 - CAndrew ConeView Answer on Stackoverflow
Solution 2 - COddthinkingView Answer on Stackoverflow
Solution 3 - CPraveen SView Answer on Stackoverflow
Solution 4 - CSarin Vadakkey ThayyilView Answer on Stackoverflow
Solution 5 - Camit kumarView Answer on Stackoverflow
Solution 6 - CRahul SingalView Answer on Stackoverflow
Solution 7 - Cwolf_flowView Answer on Stackoverflow
Solution 8 - CyoonghmView Answer on Stackoverflow
Solution 9 - Cu0b34a0f6aeView Answer on Stackoverflow
Solution 10 - CNitish GoyalView Answer on Stackoverflow