C语言基础:链表

示例代码:

  1. struct node{            //使用递归结构处理链表
  2.     int data;
  3.     struct node *next;
  4. }; 
  5.  
  6. struct node *creatlist(){             //创建链表的函数*createlist()
  7.     struct node *h,*p,*q;             //h头节点,p插入的节点,q尾节点
  8.     int a;
  9.     h=(struct node *)malloc(sizeof(struct node));      //h头节点,链表的起始地址
  10.     p=q=h;
  11.     scanf("%d",&a);
  12.     while(a!=0){
  13.         p=(struct node *)malloc(sizeof(struct node));  //p插入节点的起始位置
  14.         p->data=a;
  15.         q->next=p;                                      //插入节点的地址存入当前节点的next
  16.         q=p;                                                //当前节点作为新节点的尾节点
  17.         scanf("%d",&a);
  18.     }
  19.     p->next=NULL;                 //p的next为空,p就是尾节点
  20.     return h;
  21. }
  22.  
  23. void printlist(struct node *h){         //打印链表的函数printlist()
  24.     struct node *p;                           
  25.     p=h->next;                                  //p时头节点的下一个节点
  26.     while(p!=NULL){
  27.         printf("-%d ",p->data);
  28.         p=p->next;                             //取下一个节点
  29.     }
  30.     printf("\n");
  31.     return;
  32. }
  33. main(){
  34.     struct node *head;
  35.     head=creatlist();              //创建链表并保存链表起始地址
  36.     printlist(head);                 //输出链表
  37. }
  38. ------------------------------------------------------------------------------------------------
  39. //链表的增删改查
  40. struct Data{        //定义节点的数据类型
  41.     int num;
  42.     struct Data *next;
  43. };
  44. struct Data *insert(struct Data *head,struct Data *p0){          //插入节点 函数 *insert()
  45.     struct Data *p1;                        //*p0 插入p1的前边,p2的后边
  46.     struct Data *p2;
  47.     p1=head;
  48.     
  49.     if(head==NULL){          //链表为空
  50.         head=p0;
  51.         p0->next=NULL;
  52.     }else{
  53.         while((p0->num>p1->num)&&(p1->next!=NULL)){   //查找待插入的位置
  54.             p2=p1;
  55.             p1=p1->next;
  56.         }
  57.         if(p0->num<=p1->num){           //按照num的大小决定p0的插入位置
  58.             if(p1==head){                        //p1是头节点,p0插入p1前边
  59.                 head=p0;
  60.                 p0->next=p1;
  61.             }else{                                     //插入p2的后边,p1的前边
  62.                 p2->next=p0;
  63.                 p0->next=p1;
  64.             }
  65.         }else{                                       //插入尾节点的后边
  66.             p1->next=p0;
  67.             p0->next=NULL;
  68.         }
  69.     }        
  70.     return head;
  71. }
  72. void print(struct Data *head){        //输出链表的数据
  73.     printf("链表中的数据:\n");   
  74.     if(!head) printf("链表为空:\n");
  75.     while(head){
  76.         printf("%5d -> ",head->num);
  77.         head=head->next;
  78.     }
  79.     printf("NULL\n");
  80. }
  81. struct Data *release(struct Data *head){       //释放链表空间
  82.     struct Data *p=head;
  83.     while(p){
  84.         head=p->next;
  85.         free(p);                             //free()释放链表空间
  86.         p=head;
  87.     }
  88.     printf("释放成功:\n");
  89.     return head;
  90. }
  91. struct Data *EraseNode(struct Data *head,int n){      //删除节点
  92.     struct Data *p1;             //要删除的节点
  93.     struct Data *p2;             //p1的前一个结点
  94.     
  95.     if(head=NULL){                       //链表为空
  96.         printf("\n链表是空表:\n");
  97.         return head;
  98.     }
  99.     
  100.     p1=head;
  101.     while((n!=p1->num)&&(p1->next!=NULL)){                //查找要删除的节点
  102.         p2=p1;
  103.         p1=p1->next;
  104.     }
  105.     if(n==p1->num){                        //找到要删除的节点
  106.         if(p1==head){                       //要删除的是头节点
  107.             head=p1->next;
  108.         }else{
  109.             p2->next=p1->next;           //要删除的不是头节点
  110.         } 
  111.         free(p1);                                //释放空间
  112.         printf("已删除:%d\n");
  113.         
  114.     }else{                                      //没有找到
  115.         printf("未找到\n");
  116.     }
  117.     return head;
  118. }
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值