heap堆内存顺序表

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 typedef int type;
  5 
  6 int max = 10;
  7 
  8 struct List
  9 {
 10     type *head;
 11     int length;
 12 };
 13 
 14 int init(struct List *a);
 15 int insert(struct List *a, type x);
 16 void print(struct List a);
 17 int delete_index(struct List *a, int index);
 18 int update_index(struct List *a, int index, type x);
 19 int quary(struct List a, type x);
 20 void empty(struct List *a);
 21 int in_index(struct List *a, type index, type x);
 22 int main()
 23 {
 24     int i, ret;
 25     struct List a;
 26     ret = init(&a);
 27     if (-1 == ret)
 28     {
 29         return -1;
 30     }
 31     for (i = 0; i < 20; i++)
 32     {
 33         ret = insert(&a, i);
 34         if (-1 == ret)
 35         {
 36             return -1;
 37         }
 38     }
 39     print(a);
 40     delete_index(&a, 2);
 41     print(a);
 42     delete_index(&a, 22);
 43     update_index(&a, 2, 99);
 44     print(a);
 45     update_index(&a, 22, 99);
 46     ret = quary(a, 5);
 47     if (-1 == ret)
 48     {
 49         return -1;
 50     }
 51     printf("%d\n", ret);
 52     ret = in_index(&a, 10, 34);
 53     if (-1 == ret)
 54     {
 55         return -1;
 56     }
 57     print(a);
 58     empty(&a);
 59 
 60     return 0;
 61 }
 62 
 63 int init(struct List *a)
 64 {
 65     a->head = (type*)malloc(max*(sizeof(type)));
 66     if (NULL == a->head)
 67     {
 68         return -1;
 69     }
 70     a->length = 0;
 71 
 72     return 1;
 73 }
 74 
 75 int insert(struct List *a, type x)
 76 {
 77     if (max == a->length)
 78     {
 79         a->head = (type*)realloc(a->head,sizeof(type)*(max + max/2));
 80         if (NULL == a->head)
 81         {
 82             return -1;
 83         }
 84         max += max/2;
 85         printf("after realloc max = %d\n", max);
 86     }
 87     *(a->head + a->length) = x;
 88     a->length++;
 89 }
 90 
 91 int delete_index(struct List *a, int index)
 92 {
 93     if (index < 0 || index >= a->length)
 94     {
 95         printf("delete index out of rage\n");
 96         return -1;
 97     }
 98     int i;
 99     for (i = 0; i < a->length - index; i++)
100     {
101         *(a->head + index + i) = *(a->head + index + i +1);
102     }
103     a->length--;
104     return 1;
105 }
106 
107 int update_index(struct List *a, int index, type x)
108 {
109     if (index < 0 || index >= a->length)
110     {
111         printf("update index out of rage\n");
112         return -1;
113     }
114     *(a->head + index) = x;
115 }
116 
117 int quary(struct List a, type x)
118 {
119     int i;
120     for (i = 0; i < a.length; i++)
121     {
122         if (*(a.head + i) == x)
123         {
124             return i;
125         }
126     }
127     return -1;
128 
129 }
130 
131 int in_index(struct List *a, type index, type x)
132 {
133     int i;
134     if (index < 0 || index >= a->length)
135     {
136         printf("update index out of rage\n");
137         return -1;
138     }
139     if (max == a->length)
140     {
141         printf("jia leicun\n");
142         a->head = (type*)realloc(a->head,sizeof(type)*(max + max/2));
143         max += max/2;
144     }
145     for (i = a->length - 1; i >= index; i--)
146     {
147         *(a->head + i + 1) = *(a->head + i);
148     }
149     *(a->head + index) = x;
150     return 1;
151 }
152 void empty(struct List *a)
153 {
154     free(a->head);
155     a->head = NULL;
156     a->length = 0;
157     max = 10;
158 }
159 void print(struct List a)
160 {
161     int i;
162     for (i = 0; i < a.length; i++)
163     {
164         printf("%d ",*(a.head + i));
165     }
166     printf("\n");
167 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值