双向链表 创建头 节点 头插 输出

  1 #include "head.h"                                                        |  1 #ifndef ww                                                               |  1 #include "head.h"                                                                                                                                                     
  2 //创建双向链表                                                           |  2 #define ww                                                               |  2 int main(int argc, const char *argv[])
  3 p create_double()                                                        |  3 #include <stdio.h>                                                       |  3 {
  4 {                                                                        |  4 #include <string.h>                                                      |  4     p h=create_double();
  5     p h=(p)malloc(sizeof(stu));                                          |  5 #include <stdlib.h>                                                      |  5     insert_head(h,10);
  6     if(h==0)                                                             |  6 typedef struct stu                                                       |  6     insert_head(h,20);
  7     {                                                                    |  7 {                                                                        |  7     insert_head(h,30);
  8         printf("空间申请失败\n");                                        |  8     int data;                                                            |  8     show(h);
  9         return 0;                                                        |  9     struct stu*pri;                                                      |  9 
 10     }                                                                    | 10     struct stu*next;                                                     | 10      return 0;
 11     h->data=0;                                                           | 11 }stu,*p;                                                                 | 11 }
 12     h->next=0;                                                           | 12                                                                          |~                                                                                                                                                                         
 13     h->pri=0;                                                            | 13 //创建双向链表                                                           |~                                                                                                                                                                         
 14 }                                                                        | 14 p create_double();                                                       |~                                                                                                                                                                         
 15 //创建双向链表节点                                                       | 15 //创建双向链表节点                                                       |~                                                                                                                                                                         
 16 p create_node(int n)                                                     | 16 p create_node(int n);                                                    |~                                                                                                                                                                         
 17 {                                                                        | 17 //头插                                                                   |~                                                                                                                                                                         
 18     p new=(p)malloc(sizeof(stu));                                        | 18 void insert_head(p h,int n);                                             |~                                                                                                                                                                         
 19     if(new==0)                                                           | 19 //输出                                                                   |~                                                                                                                                                                         
 20     {                                                                    | 20 void show(p h);                                                          |~                                                                                                                                                                         
 21         printf("空间申请失败\n");                                        | 21                                                                          |~                                                                                                                                                                         
 22     return 0;                                                            | 22                                                                          |~                                                                                                                                                                         
 23     }                                                                    | 23                                                                          |~                                                                                                                                                                         
 24     new->data=n;                                                         | 24                                                                          |~                                                                                                                                                                         
 25     return new;                                                          | 25                                                                          |~                                                                                                                                                                         
 26 }                                                                        | 26                                                                          |~                                                                                                                                                                         
 27 //判空                                                                   | 27                                                                          |~                                                                                                                                                                         
 28 int empty(p h)                                                           | 28 #endif                                                                   |~                                                                                                                                                                         
 29 {                                                                        |~                                                                            |~                                                                                                                                                                         
 30     if(h==0)                                                             |~                                                                            |~                                                                                                                                                                         
 31     {                                                                    |~                                                                            |~                                                                                                                                                                         
 32         printf("入参为空\n");                                            |~                                                                            |~                                                                                                                                                                         
 33         return -1;                                                       |~                                                                            |~                                                                                                                                                                         
 34     }                                                                    |~                                                                            |~                                                                                                                                                                         
 35     return h->next==0?1:0;                                               |~                                                                            |~                                                                                                                                                                         
 36 }                                                                        |~                                                                            |~                                                                                                                                                                         
 37 //头插                                                                   |~                                                                            |~                                                                                                                                                                         
 38 void insert_head(p h,int n)                                              |~                                                                            |~                                                                                                                                                                         
 39 {                                                                        |~                                                                            |~                                                                                                                                                                         
 40     if(h==0)                                                             |~                                                                            |~                                                                                                                                                                         
 41     {                                                                    |~                                                                            |~                                                                                                                                                                         
 42         printf("入参为空\n");                                            |~                                                                            |~                                                                                                                                                                         
 43         return;                                                          |~                                                                            |~                                                                                                                                                                         
 44     }                                                                    |~                                                                            |~                                                                                                                                                                         
 45     //定义新指针接收新建节点首地址                                       |~                                                                            |~                                                                                                                                                                         
 46     p new=create_node(n);                                                |~                                                                            |~                                                                                                                                                                         
 47     new->next=h->next;                                                   |~                                                                            |~                                                                                                                                                                         
 48     if(h->next!=0)//可能只有头                                           |~                                                                            |~                                                                                                                                                                         
 49     {                                                                    |~                                                                            |~                                                                                                                                                                         
 50         h->next->pri=new;                                                |~                                                                            |~                                                                                                                                                                         
 51     }                                                                    |~                                                                            |~                                                                                                                                                                         
 52     new->pri=h;                                                          |~                                                                            |~                                                                                                                                                                         
 53     h->next=new;                                                         |~                                                                            |~                                                                                                                                                                         
 54 }                                                                        |~                                                                            |~                                                                                                                                                                         
 55 //输出                                                                   |~                                                                            |~                                                                                                                                                                         
 56 void show(p h)                                                           |~                                                                            |~                                                                                                                                                                         
 57 {                                                                        |~                                                                            |~                                                                                                                                                                         
 58     if(h==0)                                                             |~                                                                            |~                                                                                                                                                                         
 59     {                                                                    |~                                                                            |~                                                                                                                                                                         
 60         printf("入参为空\n");                                            |~                                                                            |~                                                                                                                                                                         
 61         return;                                                          |~                                                                            |~                                                                                                                                                                         
 62     }                                                                    |~                                                                            |~                                                                                                                                                                         
 63     if(empty(h))                                                         |~                                                                            |~                                                                                                                                                                         
 64     {                                                                    |~                                                                            |~                                                                                                                                                                         
 65         printf("链表为空\n");                                            |~                                                                            |~                                                                                                                                                                         
 66         return;                                                          |~                                                                            |~                                                                                                                                                                         
 67     }                                                                    |~                                                                            |~                                                                                                                                                                         
 68     p p1=h->next;                                                        |~                                                                            |~                                                                                                                                                                         
 69     while(p1!=0)                                                         |~                                                                            |~                                                                                                                                                                         
 70     {                                                                    |~                                                                            |~                                                                                                                                                                         
 71         printf("%d->",p1->data);                                         |~                                                                            |~                                                                                                                                                                         
 72         p1=p1->next;                                                     |~                                                                            |~                                                                                                                                                                         
 73     }                                                                    |~                                                                            |~                                                                                                                                                                         
 74                                                                          |~                                                                            |~                                                                                                                                                                         
 75 }  

还没写完

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值