catalogue
Three, end
I:
The linked list is divided into eight categories. This article describes the implementation of the two-way circular linked list of the leading node;
The physical structure of such linked list is shown in the figure below:
There are two pointers in each node, pointing to the previous node and the next node respectively;
When there is no content in the linked list, the header node is as follows:
The advantage of the head node is that there is no need to consider special circumstances when inserting or deleting the first node;
Compared with the single linked list, the advantage of the leading linked list is that there is no need to use the secondary pointer when deleting the head plug;
2, Implementation:
typedef struct LIST { struct LIST* prev; struct LIST* next; int data; }LIST;
2. Initialization
In order to avoid using secondary pointers, function nesting is carried out; The initialization of the head node is completed
LIST* head = BuyHead(); LIST* BuyNode(int x)//Open up new nodes { LIST* newnode = (LIST*)malloc(sizeof(LIST)); if (newnode == NULL) return NULL; newnode->data = x; newnode->next = NULL; newnode->prev = NULL; return newnode; } LIST* BuyHead() { LIST* head = BuyNode(0); head->next = head; head->prev = head; return head; }
3. Head increase
As shown in the figure, adding a header is to add a node behind the header node and change the link of the original node;
void Hnode(LIST* phead,int x)//Head increase { assert(phead); LIST* newnode = BuyNode(x); newnode->next = phead->next; phead->next->prev = newnode; phead->next = newnode; newnode->prev = phead; }
This function can ignore the link order of nodes; You don't have to care whether there are no elements in the linked list
When the linked list is empty, head - > next is equal to itself; Reflect the benefits of taking the lead in linked lists
4. Tail increase
void Wnode(LIST* phead, int x)//tail { LIST* newnode = BuyNode(x); LIST* replica = phead->prev; replica->next = newnode; newnode->prev = replica; newnode->next = phead; phead->prev = newnode; }
5. Header deletion
void DenHnode(LIST* phead)//Header deletion { assert(phead); assert(phead->next != phead); LIST* replica = phead->next; replica->next->prev = phead; phead->next = replica->next; free(replica); replica = NULL; }
When deleting elements, you should consider whether the linked list is empty and whether the node is empty, and assert with assert;
6. Tail deletion
void DenWnode(LIST* phead)//Tail deletion { assert(phead); assert(phead->prev != phead); LIST* replica = phead->prev; phead->prev = replica->prev; replica->prev->next = phead; free(replica); replica = NULL; }
7. Find
LIST* SeekList(LIST* head, int x)//lookup { assert(head); LIST* replica = head->next; while (replica != head) { if (replica->data == x) { return replica; } replica = replica->next; } return NULL; }
8. Delete the specified element
void SpDen(LIST* cop)//Delete the specified element { LIST* prev = cop->prev; LIST* next = cop->next; prev->next = next; next->prev = prev; free(cop); cop = NULL; }
9. Add element before cop
void SpAdd(LIST* cop,int x) { LIST* newnode = BuyNode(x); LIST* replica = cop->prev; replica->next = newnode; newnode->prev = replica; newnode->next = cop; cop->prev = newnode; }
Think about 8 and 9. If you replace the cop with the first element or the last element, can you complete the head, tail deletion, head and tail addition;
The answer is yes,
Header deletion and addition; Replace cop with phead - > next,
Delete at the end, add: replace cop with phead - > prev;
10. Printing
void Print(LIST* phead)//Print { LIST* replica = phead->next; while (replica != phead) { printf("%d ", replica->data); replica = replica->next; } printf("\n"); }
Three,
Compared with the one-way linked list, the two-way linked list is much simpler. Although the structure is complex, the operation is simpler;
This article is over again. Imperfections can be pointed out or modified by yourself. Thank you