|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
学习数据结构写了一段代码想实现数据表,在主函数中加了一个循环想输出一下表内的数据,但是运行的结果没有输出数据,代码不知道哪里有问题。
希望高人指点
运行后终端显示如下
PS E:\C项目\SeqList> cd "e:\C项目\SeqList\" ; if ($?) { gcc SeqList.c -o SeqList } ; if ($?) { .\SeqList }
(然后就没了)
代码如下:
#include<stdio.h>
#include<stdlib.h>
#define InitSize 100
typedef struct{
int *data;
int MaxSize, length;
} SeqList;
void InitList(SeqList L){
L.data = (int *)malloc(InitSize * sizeof(int));
L.length = 0;
L.MaxSize = InitSize;
}
void IncreaseSize(SeqList L,int len)
{
int *p = L.data;
L.data = (int *)malloc((L.MaxSize + len) * sizeof(int));
for (int i = 0; i < L.length;i++)
{
L.data[i] = p[i];
}
L.MaxSize = L.MaxSize + len;
free(p);
}
int main()
{
SeqList L;
InitList(L);
for (int i = 0; i < 5;i++)
{
L.data[i] = 2*i;
}
IncreaseSize(L, 5);
for (int i = 0; i < 5;i++)
{
printf("%d", L.data[i]);
}
printf("MaxSize is :%d", L.MaxSize);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define InitSize 100
typedef struct {
int* data;
int MaxSize, length;
} SeqList;
void InitList(SeqList *L) {
L->data = (int*)malloc(InitSize * sizeof(int));
L->length = 0;
L->MaxSize = InitSize;
}
void IncreaseSize(SeqList *L, int len)
{
int* p = L->data;
L->data = (int*)malloc((L->MaxSize + len) * sizeof(int));
for (int i = 0; i < L->length; i++)
{
L->data[i] = p[i];
}
L->MaxSize = L->MaxSize + len;
free(p);
}
int main()
{
SeqList L;
InitList(&L);
for (int i = 0; i < 5; i++)
{
L.data[i] = 2 * i;
L.length++;
}
IncreaseSize(&L, 5);
for (int i = 0; i < 5; i++)
{
printf("%d", L.data[i]);
}
puts("");
printf("MaxSize is :%d", L.MaxSize);
return 0;
}
|
|