Last updated on Apr. 28, 2024, created on Apr. 28, 2024.
注意:使用链表需要调用头文件stdlib.h
使用结构体定义一个双向链表的形态:
struct node {
struct node *pre;
int value;
struct node *post;
};
首先构建一个初始节点:struct node init_node={NULL,0,NULL};
构建头指针定位初始节点:struct node *head=&init_node;
构建指针定位当前节点(例如当前节点为头节点):struct node *current=head;
在链表末端插入节点,并定位到当前节点:
pointer->post=calloc(1,sizeof(struct node));
pointer->post->pre=pointer;
pointer->post->value=0;
pointer->post->post=NULL;
pointer=pointer->post;
在链表头节点前插入节点:
head->pre=calloc(1,sizeof(struct node));
head->pre->post=head;
head->pre->pre=NULL;
head->pre->value=0;
head=head->pre;
在链表中部插入新节点(例如在current与current->post之间):
struct node *tmp=current->post;current->post=calloc(1,sizeof(struct node));
current->post->pre=current;
current->post->value=0;
current->post->post=tmp;
current->post->post->pre=current->post;
在链表中部删除节点(例如删除节点current):
current->pre->post=current->post;
current->post->pre=current->pre;