This is the 28th day of my participation in the August Text Challenge.More challenges in August

preface

There is no essential difference between a circular list and a single list. The only difference is that the end of the linked list is no longer empty, but points to the first pointer. This is the only way to implement the loop function, so the problem is that we need to change the reuse name of the header pointer to first->next, and the count starts from 1, so that we can avoid the first step in the while loop. Without further ado.

Circular linked list

The two ends of a linked list are connected to form a circular list called a circular list.

  • This is easy to understand, but based on the single linked list we had before, the head node is associated with the tail node, so that it forms a closed loop.

  • In fact, there are no specific head and tail nodes in a circular list. Because they’re all members of a circular linked list. Time is defined as a circle by Conor the Conqueror in the popular American TV series Loki. It fits nicely with our concept of a circular list.

The entire list can only be accessed in one direction from the head to the tail, which is collectively referred to as “one-way list” or “single linked list”.

If the algorithm needs to find the nodes of a node frequently, the solution of single linked list is to traverse the whole linked list, which increases the time complexity of the algorithm and affects the overall efficiency.

In order to solve this problem quickly and conveniently, on the basis of one-way linked list, each node is equipped with an additional pointer variable, which is used to point to the direct forward element of each node. Such lists are called “bidirectional” or “double-linked” lists.

When double-linked list deletes nodes, it directly traverses the linked list to find the node to be deleted, and then completes the deletion operation by using the two pointer fields of the node.

Delete node 2 in (1, 4, 2) :

Line * delLine(line * head,int data){line * temp=head; If (temp->data==data) {temp->data ->next=temp->next; temp->next->prior=temp->prior; free(temp); return head; } temp=temp->next; } printf(" this data element is not in the list "); return head; }Copy the code

conclusion

  • The nice thing about loops is that we don’t have to worry about ending. But that’s one of our weaknesses. Be careful to create a loop