Data structure (two) circular linked list and doubly linked list

Circular linked list

Changing the pointer end of the terminal node in the singly-linked list from a null pointer to the head node makes the entire singly-linked list form a ring. This singly-linked list is called a singly-linked list, or circular linked list for short. ). Like a singly linked list, a circular linked list does not necessarily have to have a head node, but a circular linked list with a head node makes the processing of an empty linked list and a non-empty linked list consistent.
1

In this case, access to the first element requires O (1) O (1)O ( 1 ) time, but to access the last element requiresO (n) O(n)O ( n ) . The successor pointer of the circular linked list with the head node points to the head node.

Doubly linked list

There is a problem with singly linked lists and circular linked lists. When we visit an element, the time complexity is O (1) O(1)O ( 1 ) , the time to visit again next time isO (n) O(n)O ( n ) In order to solve this problem, a doubly linked list was invented.
2
If it is an empty doubly linked list, the front and back pointers of the head node point to the same head node.

A double linked list is to set a pointer field to its predecessor node in each node of the singly linked list. The doubly linked list has two pointer fields, one to the predecessor and one to the successor. Its structure can be designed as:

typedef struct DulNode
{
    
    
	int data;
	struct DulNode * prev;
	struct DulNode * next;
}DulNode,

typedef DulNode * pDuLinkList;

The doubly linked list has the following equation:

p->next->prev=p=p->prior->next;

The doubly linked list makes reverse traversal easy, but it also brings storage space and operational complexity. For example, insert operation:
3
assuming that p points to node N2, then p->next points to N3, and s points to the node where N is to be inserted.

s->next=p->next;
s->prev=p->next->prev;
p->next=s;
p->next->prev=s;

Note that the information of the original node is first sent to the new node for processing.
4
Assuming that p points to the node B to be deleted, then p->next points to C, and p->prev points to A.

p->prev->next=p->next;
p->next->prev=p->prev;
free(p);

Guess you like

Origin blog.csdn.net/weixin_39258979/article/details/115096340