Nil is used as the head and tail of the list, and the insertion function is used when constructing it, as long as the current pointer is not nil when it is traversed.
About the use of nil nodes:
*nil = &NIL;
About the use of memory pools:
Node Pool[max]; int Poolindex; Node* getnew () { return &pool[poolindex++];}
When inserting, operate on four pointers:
Newnode->pre = Dstnode; NewNode->next = dstnode->Next; Dstnode->next->pre = NewNode; Dstnode->next = NewNode;
Delete the operation for two pointers to the previous node and the latter node of the node to be deleted:
Node->pre->next = node->Next; Node->next->pre = node->pre;
Complete test Code:
#include <stdio.h>#include<malloc.h>#defineMAX 100000typedefstructnode{intdata; structNode *Next; structNode *Pre;} Node; Node NIL; Node*nil = &NIL; Node Pool[max];intPoolindex; Node*getnew () {return&pool[poolindex++];}voidListinit () {nil->next =Nil; Nil->pre =Nil; Nil->data =0;}voidListinsertafter (node*dstnode,node*NewNode) {NewNode->pre =Dstnode; NewNode->next = dstnode->Next; Dstnode->next->pre =NewNode; Dstnode->next =NewNode;}voidListdelete (node*node) {Node->pre->next = node->Next; Node->next->pre = node->Pre;} Node* Search (intkey) {Node*x = nil->Next; //Starting from the next element of the table header while(x! = nil&&x->data! =key) {x= x->Next; } returnX//address of data that may return nil or found}voidshowlist () {Node* x = nil->Next; //Display starting with the first element While (x! =Nil) {printf ("%d\n", x->data); X= x->Next; }}//The main function: Add 1 to 20 numbers in the list, then find the 2 position and add a 22 after it, and then delete it using the delete function. intMain () {listinit (); Node*dstnode =getnew (); Dstnode=Nil; for(inti =1; I <= -; i++) {Node* NewNode =getnew (); NewNode->data =i; Listinsertafter (Dstnode,newnode); Dstnode=NewNode; } Dstnode= Search (2); Node* NewNode =getnew (); NewNode->data = A; Listinsertafter (Dstnode, newnode); Showlist (); Dstnode= Search ( A); Listdelete (Dstnode); Showlist ();}
Linked list API implementations (INSERT, delete, find)