JS circular linked list

insert image description here


Introduction

A circular linked list is a special linked list data structure, in which the last node points to the head node of the linked list, forming a circular ring structure. Unlike ordinary linked lists, circular linked lists have no clear end point, and the entire linked list can be traversed from any node.

The concept of circular linked list

A circular linked list is a variant of a linked list in which the last node in the linked list points to the head node of the linked list, forming a loop or ring structure.

Unlike ordinary linked lists, circular linked lists do not have a clear end point. It provides a convenient way to traverse the entire linked list, because you can start from any node and traverse along the next pointer to the original starting node to complete the traversal of the entire circular linked list.

In a circular linked list, each node still contains a data element and a pointer to the next node. However, special care needs to be taken when linking nodes to point the pointer of the last node to the first node to form the closure of the loop.

The application scenarios of circular linked list include circular list in game development, carousel display, Joseph ring problem, etc.

In JavaScript, we can use objects or classes to represent circular linked lists. Create a linked list node object, build a circular linked list through assignment and pointer operations, and ensure that the pointer of the last node points to the head node, forming a loop.

A circular linked list has the following characteristics:

Circularity: A circular linked list is a closed structure that forms a loop by pointing the last node to the head node. This means that there is no clear end point in the linked list, and the entire linked list can be traversed from any node until it returns to the original starting node.

Flexibility: Since the circular linked list is circular, nodes can be inserted or deleted at any position without modifying the pointers of other nodes. This makes the circular linked list more flexible and efficient in some scenarios, such as implementing circular lists, carousels, etc.

Scenario application: Circular linked lists are often used in scenarios that require loop traversal. For example, in game development, a circular linked list can be used to implement a circular list to traverse the queue of player characters; in a carousel or looping scene, a circular linked list can be used to manage the order of the displayed content.

Additional pointers are required: Compared with ordinary linked lists, circular linked lists require additional pointers to record the tail node (ie, the last node) of the linked list or the starting node for convenient access. This makes it easier to perform operations such as insertion, deletion, and traversal.

Pay attention to the processing of the circular linked list: the circular linked list needs to pay special attention to the circular situation during operation, so as to avoid the situation of infinite loop or infinite loop. In the programming implementation, it is necessary to ensure that the pointer of the last node is correctly set to point to the head node.

These characteristics make the circular linked list a flexible and powerful data structure, which can provide convenient and efficient operation methods in some scenarios. Of course, you also need to be careful with circularity and termination conditions when working with circular linked lists to avoid unexpected behavior.

implement a circular list

In JavaScript, a circular linked list is a special linked list structure in which the last node points to the head node, forming a cycle. This data structure can be used to handle scenarios that require continuous loop traversal.

The following is a sample code to implement a circular linked list in JavaScript:

javascript
class Node {
    
    
  constructor(data) {
    
    
    this.data = data;
    this.next = null;
  }
}

class CircularLinkedList {
    
    
  constructor() {
    
    
    this.head = null;
    this.tail = null;
  }

  // 在链表末尾添加节点
  append(data) {
    
    
    const newNode = new Node(data);
    
    if (!this.head) {
    
    
      this.head = newNode;
      this.tail = newNode;
    } else {
    
    
      this.tail.next = newNode;
      this.tail = newNode;
    }
    
    // 循环链接
    this.tail.next = this.head;
  }

  // 遍历链表
  traverse() {
    
    
    if (!this.head) {
    
    
      console.log("链表为空。");
      return;
    }

    let current = this.head;
    
    do {
    
    
      console.log(current.data);
      current = current.next;
    } while (current !== this.head);
  }
}

// 使用示例
const list = new CircularLinkedList();

list.append(1);
list.append(2);
list.append(3);

list.traverse();  // 输出: 1 2 3

In the above example, we first define the Node class as the template of the linked list node, which contains a data attribute and a next pointer pointing to the next node. Then the CircularLinkedList class is defined as the template of the circular linked list, which has the functions of adding nodes and traversing the linked list. In the append method, we add the new node to the end of the linked list and make sure that the last node points to the head node to form a circular link. In the traverse method, we traverse the linked list starting from the head node until we get back to the head node.

Guess you like

Origin blog.csdn.net/weixin_48998573/article/details/131320767