链表中头和尾节点/指针的常规用法:插入、删除、遍历

链表中头和尾节点/指针的常规用法

循环、永远非空

         头插入:head->next= head;

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

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

         遍历循环:(1) t =head;

                                  do{ …; t = t->next; }while(t!= head);

                              (2)for(t= head; t != head; t = t->next) {…}

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

 

头指针、尾节点为空

         初始化:head =NULL;

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

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

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

         遍历循环:(1) t =head;         while(t!=NULL) {…; t = t->next}

                          (2)for(t= head; t !=NULL; t = t->next) {…}

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

 

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

         初始化:head = (Node*)malloc(sizeof(*head));             head->next =NULL;

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

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

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

         遍历循环:(1) t =head->next;                while(t !=NULL) {…; t = t->next}

                          (2)for(t= head->next; t !=NULL; t = t->next) {…}

         测试链表为空:if(head->next ==NULL);

 

有哑元头节点,尾节点

         初始化:head = (Node*)malloc(sizeof(*head));

                            z= (Node*)malloc(sizeof(*z));

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

         插入、删除、遍历同上;

         测试链表为空:if(head->next == z);

        

 

《算法:C语言实现》P57

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值