[牛人杂谈] 单链表

[复制链接]
513|2
 楼主| yiyigirl2014 发表于 2019-6-29 21:56 | 显示全部楼层 |阅读模式
  1. /* 1.C语言单向链表  
  2. * 2.链表的(增,删,查,遍历)  
  3. * Author: Mr.Long  

  4. */


  5. #include "stdafx.h"
  6. #include<stdlib.h>


  7. /* 结构定义 */  
  8. struct node {  
  9.     int data;  
  10.     node *next;  
  11. };  


  12. /* 函数定义 */  
  13. void printNode(struct node *head);  
  14. struct node* appendNode(struct node *head,int);  
  15. struct node* addFirstNode(struct node *head,int);  
  16. struct node* findNode(struct node *head,int);  
  17. int deletNode(struct node *head,int);


  18. int main(int argc, char* argv[])
  19. {
  20.   /* 定义一个head指针,作为链表的头节点*/  
  21.     struct node *head = NULL;  
  22.     //申请内存空间   
  23.     head = (struct node *)malloc(sizeof(struct node));  
  24.     //初始化头结点数据   
  25.     head->data = 0;  
  26.     head->next = NULL;  
  27.   
  28.     printf("*****************添加节点******************\r\n");  
  29.     /* 在链表尾部追加节点 */  
  30.     head = appendNode(head,5);  
  31.     head = appendNode(head,8);  
  32.     head = appendNode(head,4);  
  33.     head = appendNode(head,34);  
  34.     head = appendNode(head,6);  
  35.     head = appendNode(head,9);  
  36.   
  37.     /* 添加为头节点 */  
  38.     head = addFirstNode(head,12);  
  39.     head = addFirstNode(head,61);  
  40.   
  41.     printf("添加成功... \n");  
  42.       
  43.     printf("*****************查找节点******************\r\n");  
  44.     /* 查找指定节点 */  
  45.     struct node *a;  
  46.     a = findNode(head,34);  
  47.     if(a != NULL) {  
  48.         printf("查找成功... \n");  
  49.         printf("data:%d \n",a->data);  
  50.         printf("next:0x%X \n",a->next);  
  51.     } else {  
  52.         printf("没找到该节点 \n");  
  53.     }  
  54.     printf("*****************删除节点******************\r\n");  
  55.     /* 删除指定节点 成功返回1 失败返回0 */  
  56.     int dest = 0;  
  57.     bool state = deletNode(head,dest);  
  58.     if(state)  
  59.         printf("删除[%d]成功... \n",dest);  
  60.     else  
  61.         printf("删除[%d]失败... \n",dest);  
  62.   
  63.     printf("*****************所有节点******************\r\n");  
  64.       
  65.     /* 遍历节点并输出 */  
  66.     printNode(head);  
  67.   
  68.     return 0;  
  69. }


  70. int deletNode(struct node *head,int dest) {  
  71.     struct node *p = head;  
  72.     struct node *lp;  
  73.     while(p->next) {  
  74.         if(p->data == dest) {  
  75.             // 删除操作:将上一个节点的next指向被删除节点的next   
  76.             lp->next = p->next;  
  77.             //释放被删除节点的内存空间  
  78.             free(p);  
  79.             return true;  
  80.         }  
  81.         //记录上一个节点  
  82.         lp = p;  
  83.         //移动指针  
  84.         p = p->next;  
  85.     }  
  86.     return false;  
  87. }  
  88. struct node* findNode(struct node *head,int dest) {  
  89.     struct node *p = head;  
  90.     struct node *pc = NULL;//临时变量  
  91.       
  92.     while(p) { //当p不为NULL时   
  93.       
  94.         if(p->data == dest) {//找到了指定节点   
  95.             pc = p;  
  96.             break;  
  97.         }  
  98.         p = p->next; //移动指针   
  99.     }  
  100.     return pc;  
  101. }  
  102. struct node* appendNode(struct node *head,int data) {  
  103.     if(data == NULL) return head;  
  104.     if(head == NULL) { //若头结点为空,则创建一个节点作为头结点   
  105.         printf("head is NULL \n");  
  106.         head = (struct node *)malloc(sizeof(struct node));  
  107.         head->data = data;  
  108.         head->next = NULL;  
  109.         return head;  
  110.     }  
  111.     /*易错点
  112.      *  这里千万注意循环条件是p->next而不是p
  113.      *  因为当执行到链表尾节点时p已经为NULL
  114.      *  而当p为NULL时56行的空指针操作会导致程序崩溃
  115.      */  
  116.     struct node *p = head;  
  117.     while(p->next) {  
  118.         p = p->next;  
  119.     }  
  120.     struct node *pNew;  
  121.     pNew = (struct node *)malloc(sizeof(struct node));  
  122.     pNew->data = data;  
  123.     pNew->next = NULL;  
  124.   
  125.     p->next = pNew;  
  126.     /*易错点:
  127.      *  如果这里返回 p 则链表只保留了最后两项数据
  128.      *  原因在于47行的while循环移动了p的指针
  129.      */  
  130.     return head;  
  131. }  
  132. struct node* addFirstNode(struct node *head,int data) {  
  133.     struct node *p;  
  134.     p = (struct node *)malloc(sizeof(struct node));  
  135.     p->data = data;  
  136.     p->next = head;  
  137.     return p;  
  138. }  
  139. void printNode(struct node *head) {  
  140.     struct node *p = head;  
  141.     while(p) { /* 当p不为NUll时 */  
  142.         printf("self=0x%X  |  data=%2d  |  next=0x%X\n",p,p->data,p->next);  
  143.         p = p->next;  
  144.     }  


 楼主| yiyigirl2014 发表于 2019-6-29 21:57 | 显示全部楼层
wanduzi 发表于 2019-6-29 22:20 | 显示全部楼层
大家可以学习一下这种。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

230

主题

3676

帖子

10

粉丝
快速回复 在线客服 返回列表 返回顶部