仰望无边星空◎ 巴巴多斯娜nn1
关注数: 36 粉丝数: 28 发帖数: 1,985 关注贴吧数: 68
写了个顺序表,刚运行就结束,大佬帮我看看哪里有问题 #include<stdio.h> #include<malloc.h> #include<stdlib.h> #pragma warning (disable:4996) #define SIZE 20 typedef int Elemtype; typedef struct SeqList { Elemtype* data; int length, capacity; }SeqList; void InitSeqList(SeqList* p); void CreatSeqList(int n, SeqList* p); int LengthSeqList(SeqList* p); bool InsertSeqList(SeqList* p, int value, int n); int LocateSeqList(SeqList* p); int GetSeqList(SeqList* p, int n); void PrintSeqList(SeqList* p); bool IsEmptySeqList(SeqList* p); void DestroySeqList(SeqList* q); int main() { SeqList* p = NULL; InitSeqList(p); CreatSeqList(10, p); InsertSeqList(p, 4567, 11); PrintSeqList(p); printf("长度为:%d\n", LengthSeqList(p)); GetSeqList(p, 5); LocateSeqList(p); bool a = IsEmptySeqList(p); if (a == true) printf("顺序表为空\n"); else printf("顺序表不为空\n"); DestroySeqList(p); return 0; } void InitSeqList(SeqList* p) { Elemtype* q = (Elemtype*)malloc(SIZE * sizeof(Elemtype)); if (q == NULL) { printf("内存分配失败"); system("pause"); exit(1); } p->data = q; p->length = 0; p->capacity = SIZE; } void CreatSeqList(int n, SeqList* p) { if (n > p->capacity) exit(1); for (int i = 0; i < n; i++) { printf("输入第%d个数据:", i + 1); scanf("%d", &p->data[i]); getchar(); } p->length = n; } int LengthSeqList(SeqList* p) { return p->length; } bool InsertSeqList(SeqList* p, int value, int n) { if (n <= 0 || n > (p->length + 2) || p->length == p->capacity) return false; int i = p->length + 1; for (i ; i > n; i--) { p->data[i] = p->data[i - 1]; } p->data[i] = value; p->length++; return true; } //按值查找 int LocateSeqList(SeqList* p) { int value; printf("查找的值为:\n"); scanf("%d", &value); for (int i = 0; i < p->length; i++) { if (value == p->data[i]) return i; } return 0;//返回零表示找不到 } //按位查找 int GetSeqList(SeqList* p, int n) { if (n <= 0 || n > p->length) exit(1); return p->data[n]; } void PrintSeqList(SeqList* p) { for (int i = 0; i < p->length; i++) { printf(" %d", p->data[i]); } } bool IsEmptySeqList(SeqList* p) { if (p->length == 0) return true; else return false; } void DestroySeqList(SeqList* q) { free(q); q = NULL; }
1 下一页