数据结构 - 分配空间问题
程序代码:
# include <stdio.h> # include <malloc.h> # include <stdlib.h> # define SIZE 5 # define ADD 1 typedef struct { int * pHead; int len; int size; }Sqlist; void init(Sqlist * L) { L->pHead = (int *)malloc(sizeof(int)*SIZE); if (NULL == L->pHead) { printf ("初始化失败\n"); exit(-1); } L->len = 0; L->size = SIZE; printf ("初始化成功\n"); } void destroy(Sqlist * L) { free(L->pHead); printf ("内存已经释放!\n"); } void get(Sqlist * L) { int i; int length; printf ("请输入线形表长度:"); scanf ("%d", &length); for (i=0; i<length; ++i) { printf ("请输入第%d个数据:", i+1); scanf ("%d", &L->pHead[i]); L->len++; } } void insert(Sqlist * L) { int i; int pos; int insert; printf ("请选择插入序号:"); scanf ("%d", &pos); if ( pos < 1 || pos > L->len + 1 ) { printf ("不合法!\n"); exit(-1); } if (L->len >= L->size) { L->size = L->size + ADD; L->pHead = (int *)realloc( L->pHead, sizeof(int) * L->size ); if (NULL == L->pHead) { printf ("插入重分配失败!\n"); exit(-1); } } printf ("请输入插入数值:"); scanf ("%d", &insert); for (i=L->len; i>=pos; --i) L->pHead[i] = L->pHead[i-1]; L->pHead[pos-1] = insert; ++L->len; } void sort1(Sqlist * L) { int i, j; int temp; for (i=0; i<L->len-1; ++i) { for (j=0; j<L->len-1-i; ++j) { if (L->pHead[j] > L->pHead[j+1]) { temp = L->pHead[j]; L->pHead[j] = L->pHead[j+1]; L->pHead[j+1] = temp; } } } } void sort2(Sqlist * L) { int i, j; int temp; for (i=0; i<L->len-1; ++i) { for (j=0; j<L->len-1-i; ++j) { if (L->pHead[j] < L->pHead[j+1]) { temp = L->pHead[j]; L->pHead[j] = L->pHead[j+1]; L->pHead[j+1] = temp; } } } } void append(Sqlist * L) { int ap; if (L->len > L->size) { L->size = L->size + ADD; L->pHead = (int *)realloc(L->pHead, sizeof(int) * L->size); if (NULL == L->pHead) { printf ("追加重分配失败!\n"); exit(-1); } } printf ("请输入您需要追加的数值:"); scanf ("%d", &ap); L->pHead[L->len] = ap; L->len++; } void deleted(Sqlist * L) { int i; int dn; int pos; printf ("请输入你需要删除的序号:"); scanf ("%d", &pos); if (pos < 1 || pos>L->len + 1) { printf ("不合法!\n"); exit(-1); } dn = L->pHead[pos-1]; for (i=pos; i<=L->len; ++i) L->pHead[pos-1] = L->pHead[pos]; --L->len; printf ("删除值为%d\n", dn); } void traverse(Sqlist * L) { int i; if (!L->pHead) printf ("内存混乱!\n"); printf ("数据为:"); for (i=0; i<L->len; ++i) printf ("%6d", L->pHead[i]); printf ("\n"); } void locate1(Sqlist * L) { int i; int pos; printf ("请输入您需要查询的序号:"); scanf ("%d", &pos); if (pos<1 || pos>L->len) { printf ("不合法!\n"); exit(-1); } printf ("该序号数值为%d\n", L->pHead[pos-1]); } void locate2(Sqlist * L) { int i; int same; printf ("请输入您需要查找的数据:"); scanf ("%d", &same); for (i=0; i<L->len; ++i) { if (L->pHead[i] == same) printf ("序号:%d - 数值:%d", i+1, L->pHead[i]); } } /* bool full(Sqlist * L) { if (L->len == sizeof(L->pHead)/sizeof(int)) return true; else return false; } bool empty(Sqlist * L) { if (L->len == 0) return true; else return false; } */ int main (void) { //变量的定义 Sqlist L; int i;//循环 int c;//switch 选择 char q;//结束整个程序 printf ("****************************************\n"); printf (" 欢迎使用第五集团线形表\n"); printf ("****************************************\n"); printf (" 1.初始化 2.赋值\n"); printf (" 3.插入 4.删除\n"); printf (" 5.遍历 6.追加\n"); printf (" 7.正序 8.倒序\n"); printf (" 9.序号定位 10.数值定位\n"); printf (" 11.销毁\n"); printf ("****************************************\n\n"); do { printf ("请选择您的操作:"); scanf ("%d", &c); switch(c) { case 1: init(&L); break; case 2: get(&L); break; case 3: insert(&L); break; case 4: deleted(&L); break; case 5: traverse(&L); break; case 6: append(&L); break; case 7: sort1(&L); break; case 8: sort2(&L); break; case 9: locate1(&L); break; case 10: locate2(&L); break; case 11: destroy(&L); break; } printf ("\n是否继续使用(Y/y):"); scanf (" %c", &q); }while ('Y' == q || 'y' == q); return 0; }
问题:
首先我输入1初始化,然后输入2进行赋值,如果我赋值个数为10个,但是我的初始化是只有5个int空间的,为什么最后我输入5遍历的时候还能输出10个数值,这样会导致什么问题么?