天天看点

c语言链表的尾指针,链表中的头和尾节点/指针的常规用法(来自:算法:C语言实现)...

下面是基本链表处理操作的5种常规用法的实现.这类代码用于内嵌链表处理代码的简单应用中

循环,永远非空

头插入 head->next = head;

在x节点后插入t节点 t->next = x->next, x->next = t;

删除x后的节点 t = x->next, x->next = t->next, free(t);

遍历循环 t = head; do{ ... t = t->next}

while(t != head);

测试是否只有一个元素 if(head->next == head)

头指针,尾节点为空

初始化 head = NULL;

在x节点后插入t节点 if(x == NULL){head = t, head->next == NULL;}

else{t->next = x->next, x->next = t;}

删除x后的节点 t = x->next, x->next = t->next, free(t);

遍历循环 t = head; while(t){... t = t->next;}

测试表是否为空 if(head == NULL)

有哑元头结点,尾节点为空

初始化 head->next = NULL;

在x节点后插入t节点 t->next = x->next, x->next = t;

删除x节点后的节点 t = x->next, x->next = t->next, free(t);

遍历循环 t = head->next; while(t){... t = t->next;}

测试表是否为空 if(head->next == NULL)

有哑元头,尾结点

初始化 head->next = rear, rear->next = rear;

在x节点后插入t节点 t->next = x->next, x->next = t;

删除x后的节点 t = x->next, x->next = t->next, free(t);

遍历循环 t = head; while(t->next != rear){... t = t->next;}

测试表是否为空 if(head->next == rear)