链表首尾节点的常规方案

本表对基本链表处理操作分别以五种常用的定义方案给出其实现方法。这类代码用于简单的、使用内嵌链表处理代码的应用程序。

循环,永远非空

头插入   head->next = head;

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

删除x后的节点    x->next = x->next->next;

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

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

有头节点,尾节点为null

初始化    head = 0;

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

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

删除x后的节点    t = x->next; x->next = t->next;

遍历循环    for (t = head; t != 0; t = t->next)

测试是否为空    if (head == 0)

有哑元头节点,尾节点为null

初始化    head = new node; head->next = 0;

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

删除x后的节点    t = x->next; x->next = t->next;

遍历循环    for (t = head->next; t != 0; t = t->next)

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

                 

有哑元头节点和尾节点

初始化    head = new node;

               z = new node;

               head->next = z; z->next = z;

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

删除x后的节点    x->next = x->next->next;

遍历循环    for (t = head->next; t != z; t = t->next)

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

原文地址:https://www.cnblogs.com/ningjing213/p/11320829.html