顺序表的实现——动态分配

 1 #include <stdlib.h>
 2 #define InitSize = 10;
 3 
 4 typedef struct{
 5     int *data;
 6     int length;
 7     int maxSize;   
 8 }SeqList;
 9 
10 void InitList(SeqList &L){
11     L.data = (int*)malloc(InitSize * sizeof(int)); //malloc会返回一个指针,所以一定要和data的一样
12     L.length = 0;
13     L.maxSize = InitSize
14 }
15 
16 void IncreaseSize(SeqList &L, int len){
17     int *p = L.data;
18     L.data = (int*)malloc((L.maxSize + len) * sizeof(int));
19     for(int i = 0; i<L.length; i++)
20     {
21          L.data[i] = p[i];
22     }
23     L.maxSize = L.maxSize+ len;
24     free(p);
25 }
26     

 

posted @ 2021-05-27 14:34  蘑菇王国大聪明  阅读(92)  评论(0编辑  收藏  举报