[关闭]
@quinn 2015-03-20T09:27:24.000000Z 字数 933 阅读 3014

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

数据结构


循环、永远非空

  1. 头插入:head->next= head;
  2. x节点后插入t节点:t->next= x->next; x->next = t;
  3. 删除x后的节点:x->next= x->next->next;
  4. 遍历循环:(1) t =head;
  5. do{ …; t = t->next; }while(t!= head);
  6. (2)for(t= head; t != head; t = t->next) {…}
  7. 测试只有一个元素:if(head->next == head)

头指针、尾节点为空

  1. 初始化:head =NULL;
  2. x节点后插入t节点:if(x ==NULL) { head = t; t->next = NULL} //空表
  3. else{ t->next= x->next; x->next = t;}
  4. 删除x后的节点:t = x->next; x->next = t->next; free(t);
  5. 遍历循环:(1) t =head; while(t!=NULL) {…; t = t->next}
  6. (2)for(t= head; t !=NULL; t = t->next) {…}
  7. 测试表是否为空:if(head ==NULL)

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

  1. 初始化:head = (Node*)malloc(sizeof(*head)); head->next =NULL;
  2. x节点后插入t节点:if(x ==NULL) {head->next= t; t->next = NULL}//空表
  3. else{t->next = x->next; x->next= t}
  4. 删除x后的节点:t = x->next; x->next = t->next; free(t);
  5. 遍历循环:(1) t =head->next; while(t !=NULL) {…; t = t->next}
  6. (2)for(t= head->next; t !=NULL; t = t->next) {…}
  7. 测试链表为空:if(head->next ==NULL);

有哑元头节点,尾节点

  1. 初始化:head = (Node*)malloc(sizeof(*head));
  2. z= (Node*)malloc(sizeof(*z));
  3. head->next= z; z->next = z;
  4. 插入、删除、遍历同上;
  5. 测试链表为空:if(head->next == z);

《算法:C语言实现》P57

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注