经过预习写的链表的几个接口函数与测试函数及头文件

 //测试接口

  1 # include <stdio.h>
  2 # include "chain.h"
  3 # include <stdlib.h>
  4
  5 void show(Type e)
  6 {
  7     printf("%d ", e);
  8 }
  9
 10
 11 int main()
 12 {
 13     Linklist head;
 14     int num = 8, ret, posit, i;
 15     Type e;
 16
 17     ret = creatlist(&head, num);
 18     if(Fail == ret)
 19     {
 20         printf("Creat a list failure!\n");
 21     }
 22     else
 23     {
 24         printf("Creat a list success!\n");
 25     }
 26
 27     posit = 3;
 28     ret = Getelem(head, posit, &e);
 29     if(Fail == ret)
 30     {
 31         printf("Get a element of position failure!\n");
 32     }
 33     else
 34     {
 35         printf("get the element of %dth is %d\n", posit, e);
 36     }
 37 
 38     ret =  print(head,show);
 39
 40     for(i = 0;i < 3;i++)
 41     {
 42         ret = amendlink(head, i + 1, i + 1);
 43         if(Fail == ret)
 44         {
 45             printf("amend %d failure!\n", i+1);
 46         }
 47         else
 48         {
 49             printf("amend %d successful\n", i + 1);
 50         }
 51     }
 52
 53     ret =  print(head,show);
 54     if(Fail == ret)
 55     {
 56         printf("print chain_list is failure!\n");
 57     }
 58     else
 59     {
 60         printf("print chain_list is successful!\n");
 61     }
 62
 63     e = 30;
 64     posit = 9;
 65     ret =  insert(head, e, posit);
 66     if(Fail == ret)
 67     {
 68         printf("insert %dth element %d  is failure!\n", posit, e);
 69     }
 70     else
 71     {
 72         printf("insert %dth element %d is successful!\n", posit, e);
 73     }
 74
 75     ret =  print(head,show);
 76
 77     posit = 6;
 78     ret = delete(head, &e, posit);
 79     if(Fail == ret)
 80     {
 81         printf("delete %dth element %d  is failure!\n", posit, e);
 82     }
 83     else
 84     {
 85         printf("delete %dth element %d is successful!\n", posit, e);
 86     }
 87     ret =  print(head,show);
 88
 89     ret = releaselink(&head);
 90     if(Fail == ret)
 91     {
 92         printf("release  is failure!\n");
 93     }
 94     else
 95     {
 96         printf("release is successful!\n");
 97     }
 98
 99     ret =  insert(head, e, posit);
100     if(Fail == ret)
101     {
102         printf("insert %dth element %d  is failure!\n", posit, e);
103     }
104     else
105     {
106         printf("insert %dth element %d is successful!\n", posit, e);
107     }
108
109     return 0;
110 }

 

//自定义的头文件

  1 #ifndef _CHAIN_H
  2 #define _CHAIN_H
  3
  4 #define Fail 0
  5 #define Success 1
  6
  7 typedef int Type;
  8 typedef struct Node
  9 {
 10     Type data;
 11     struct Node *next;
 12 }Node;
 13 typedef struct Node *Linklist;
 14
 15 int creatlist(Linklist *l, int n);
 16 int Getelem(Linklist l, int i, Type *e);
 17 int amendlink(Linklist p, int n,Type e);
 18 int print(Linklist p, void (*fp)(Type));
 19 int insert (Linklist h, Type e, int n);
 20 int delete(Linklist h,Type *e, int n);
 21 int releaselink(Linklist *h);
 22
 23 #endif

//接口函数
  1 # include <stdio.h>
  2 # include "chain.h"
  3 # include <stdlib.h>
  4
  5
  6 int creatlist(Linklist *l, int n)
  7 {
  8     Linklist p;
  9     int j;
 10
 11     srand(time(0));
 12     *l = (Linklist)malloc(sizeof(Node) * 1);
 13     (*l)-> data = n;
 14     (*l)->next = NULL;
 15
 16     for(j = 0;j < n;j++)
 17     {
 18         p = (Linklist)malloc(sizeof(Node) * 1);
 19         p->data = rand() % 100 + 1;
 20         p->next = (*l)->next;
 21         (*l)->next = p;
 22     }
 23     if(*l == NULL || (*l)->next == NULL || n < 1)
 24     {
 25         return Fail;
 26     }
 27 }
 28
 29 int Getelem(Linklist l, int i, Type *e)
 30 {
 31     int j;
 32     Linklist p;
 33
 34     p = l->next;
 35     j = 1;
 36     while(p && j < i)
 37     {
 38         p = p->next;
 39         j++;
 40     }
 41     if(p == NULL || j > i)
 42     {
 43         return Fail;
 44     }
 45     else
 46     {
 47         *e = p->data;
 48         return Success;
 49     }
 50 }
 51
 52 int amendlink(Linklist h,Type e, int n)
 53 {
 54     int i;
 55     Linklist p = h;
 56
 57     if(n > (h->data) + 1 || h == NULL || (h->next) == NULL)
 58     {
 59         return Fail;
 60     }
 61
 62     for(i = 0;i < n;i++)
 63     {
 64         p = p->next;
 65     }
 66     p->data = e;
 67
 68     return Success;
 69 }
 70
 71 int print(Linklist p, void (*fp)(Type))
 72 {
 73     int i, len = p->data;
 74
 75     if(p == NULL || p->data == 0)
 76     {
 77         return Fail;
 78     }
 79
 80     for(i = 0; i < len;i++)
 81     {
 82         p = p->next;
 83         printf("%d ", p->data);
 84     }
 85     printf("\n");
 86
 87     return Success;
 88 }
 89 int insert (Linklist h, Type e, int n)
 90 {
 91     int i = 1;
 92     Linklist p,s;
 93     p = h;
 94     while(p && i < n)
 95     {
 96         p = p->next;
 97         i++;
 98     }
 99     if(p == NULL || i > n )
100     {
101         return Fail;
102     }
103     s = (Linklist)malloc(sizeof(Node) * 1);
104     s->data = e;
105     s->next = p->next;
106     p->next = s;
107
108     (h->data)++;
109
110     return Success;
111
112 }
113
114 int delete(Linklist h, Type *e, int n)
115 {
116     int i;
117     Linklist p = h, s;
118
119     if(n > (h->data) || h == NULL)
120     {
121         return Fail;
122     }
123
124     for(i = 1;i < n;i++)
125     {
126         p = p->next;
127     }
128     s = p->next;
129     *e = p->data;
130     p->next = s->next;
131     free(s);
132     s = NULL;
133     (h->data)--;
134
135     return Success;
136 }
137
138 int releaselink(Linklist *h)
139 {
140     int i, len = (*h)->data;
141     Linklist p ;
142
143     if(len < 0 || *h == NULL)
144     {
145         return Fail;
146     }
147
148     for(i = 0;i < len;i++)
149     {
150         p = (*h)->next;
151         free(*h);
152         *h = p;
153     }
154     free(*h);
155     *h = NULL;
156
157     return Success;
158 }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值