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

前端 未结 10 1056
無奈伤痛
無奈伤痛 2021-01-30 08:27

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 doubl

相关标签:
10条回答
  • 2021-01-30 09:17

    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.

    0 讨论(0)
  • 2021-01-30 09:19

    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);
    }
    
    0 讨论(0)
  • 2021-01-30 09:23

    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.

    0 讨论(0)
  • 2021-01-30 09:26

    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.

    0 讨论(0)
提交回复
热议问题