.L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType *))会出现什么问题
时间: 2023-11-19 18:06:34 浏览: 96
这段代码可能会出现以下问题:
1. 在分配内存时,可能会分配失败,导致返回空指针;
2. 在这个代码中,使用的是sizeof(ElemType *),如果ElemType本身就是指针类型,那么实际上分配的内存大小可能会出现问题;
3. 使用malloc分配的内存必须手动释放,否则可能会出现内存泄漏的问题。
相关问题
修改代码:status InitSqlist(Sqlist &L){ L.elem = (Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype)); if(! L.elem) return error; L.length = 0; L.listsize = LIST_INIT_SIZE; return ok; } void PrintSqlist(Sqlist &L){ for(int i=0;i<L.length;i++) printf(" %2d ",L.elem[i]); printf("\n"); } void Get(Sqlist &L,int n) { int i; for(i=1 ; i<n ;i++) { scanf("%d",&(L.elem[i].key)); L.length++; } } int Search(Sqlist L, keyType key) { L.elem[0].key = key; int i; int count; for(i = L.length; !EQ(L.elem[i].key,key); i--) { count++; } return count; } int main() { int n,i; int key,count; Sqlist L; InitSqlist(L); printf("请输入顺序表的长度(顺序查找)\n"); scanf("%d",&n); printf("请输入一批数据\n"); Get(L,i); printf("以下为输入的顺序表(顺序查找)\n"); PrintSqlist(L); printf("请输入你想查找的元素(顺序查找)\n"); scanf("%d",&key); count = Search(L,key); if(count == 0) { printf("查找失败%d",&key); } else { printf("关键字 %d 的位置是 %d,查找次数是 %d\n", key, n - count + 1, count); } return 0; }
修改后的代码如下:
```
status InitSqlist(Sqlist &L){
L.elem = (Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype));
if(! L.elem)
return ERROR;
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
void PrintSqlist(Sqlist L){
for(int i=0;i<L.length;i++)
printf(" %2d ",L.elem[i].key);
printf("\n");
}
void Get(Sqlist &L,int n) {
for(int i=1; i<=n; i++)
{
scanf("%d",&(L.elem[i].key));
L.length++;
}
}
int Search(Sqlist L, keyType key) {
int i = L.length-1;
int count = 0;
while(i>=0 && L.elem[i].key!=key){
i--;
count++;
}
if(i<0){
return 0;
}else{
count++;
return count;
}
}
int main() {
int n,key,count;
Sqlist L;
InitSqlist(L);
printf("请输入顺序表的长度(顺序查找)\n");
scanf("%d",&n);
printf("请输入一批数据\n");
Get(L,n);
printf("以下为输入的顺序表(顺序查找)\n");
PrintSqlist(L);
printf("请输入你想查找的元素(顺序查找)\n");
scanf("%d",&key);
count = Search(L,key);
if(count == 0)
{
printf("查找失败%d",key);
}
else
{
printf("关键字 %d 的位置是 %d,查找次数是 %d\n", key, L.length-count+1, count);
}
return 0;
}
```
修改说明:
1. Get 函数中的循环条件应该是 `i<=n` 而不是 `i<n`,这样才能正确读入 n 个元素。
2. Search 函数中的循环条件应该是 `i>=0` 而不是 `!EQ(L.elem[i].key,key)`,这样才能正确查找到元素。
3. 在找到元素时,应该返回查找次数 count,而不是 n-count+1。因为 n 是输入的长度,可能大于实际存储的元素个数,而实际存储的元素个数是 L.length。
#include<stdio.h> #include<malloc.h> #define OK 1 #define ERROR 0 #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 #define ElemType int typedef struct { int *elem; int length; int listsize; }SqList; int InitList_Sq(SqList &L) { // 算法2.3,构造一个空的线性表L,该线性表预定义大小为LIST_INIT_SIZE // 请补全代码 } int Load_Sq(SqList &L) { // 输出顺序表中的所有元素 int i; if(_________________________) printf("The List is empty!"); // 请填空 else { printf("The List is: "); for(_________________________) printf("%d ",_________________________); // 请填空 } printf("\n"); return OK; } int ListInsert_Sq(SqList &L,int i,int e) { // 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e // i的合法值为1≤i≤L.length +1 // 请补全代码 } int ListDelete_Sq(SqList &L,int i, int &e) { // 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值 // i的合法值为1≤i≤L.length // 请补全代码 } int main() { SqList T; int a, i; ElemType e, x; if(_________________________) // 判断顺序表是否创建成功 { printf("A Sequence List Has Created.\n"); } while(1) { printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n"); scanf("%d",&a); switch(a) { case 1: scanf("%d%d",&i,&x); if(_________________________) printf("Insert Error!\n"); // 执行插入函数,根据返回值判断i值是否合法 else printf("The Element %d is Successfully Inserted!\n", x); break; case 2: scanf("%d",&i); if(_________________________) printf("Delete Error!\n"); // 执行删除函数,根据返回值判断i值是否合法 else printf("The Element %d is Successfully Deleted!\n", e); break; case 3: Load_Sq(T); break; case 0: return 1; } } }
下面是完整的代码,包含了填空部分:
```c
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int
typedef struct {
int *elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList &L) {
// 算法2.3,构造一个空的线性表L,该线性表预定义大小为LIST_INIT_SIZE
L.elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem) return ERROR; // 存储分配失败
L.length = 0; // 空表长度为0
L.listsize = LIST_INIT_SIZE; // 初始存储容量
return OK;
}
int Load_Sq(SqList &L) {
// 输出顺序表中的所有元素
int i;
if(L.length == 0) printf("The List is empty!");
else
{
printf("The List is: ");
for(i = 0; i < L.length; i++) printf("%d ",L.elem[i]);
}
printf("\n");
return OK;
}
int ListInsert_Sq(SqList &L,int i,int e) {
// 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
// i的合法值为1≤i≤L.length +1
int *newbase, *p, *q;
if(i < 1 || i > L.length + 1) return ERROR; // i值不合法
if(L.length >= L.listsize) // 当前存储空间已满,增加分配
{
newbase = (int *)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(int));
if (!newbase) return ERROR; // 存储分配失败
L.elem = newbase; // 新基址
L.listsize += LISTINCREMENT; // 增加存储容量
}
q = &(L.elem[i - 1]); // q为插入位置
for(p = &(L.elem[L.length - 1]); p >= q; --p) *(p + 1) = *p; // 插入位置及之后的元素右移
*q = e; // 插入e
++L.length; // 表长增1
return OK;
}
int ListDelete_Sq(SqList &L,int i, int &e) {
// 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
// i的合法值为1≤i≤L.length
int *p, *q;
if(i < 1 || i > L.length) return ERROR; // i值不合法
p = &(L.elem[i - 1]); // p为被删除元素的位置
e = *p; // 被删除元素的值赋给e
q = &(L.elem[L.length - 1]); // 表尾元素的位置
for(++p; p <= q; ++p) *(p - 1) = *p; // 被删除元素之后的元素左移
--L.length; // 表长减1
return OK;
}
int main() {
SqList T;
int a, i;
ElemType e, x;
if(InitList_Sq(T)) // 判断顺序表是否创建成功
{
printf("A Sequence List Has Created.\n");
}
while(1)
{
printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d%d",&i,&x);
if(ListInsert_Sq(T, i, x)) printf("The Element %d is Successfully Inserted!\n", x);
else printf("Insert Error!\n"); // 执行插入函数,根据返回值判断i值是否合法
break;
case 2: scanf("%d",&i);
if(ListDelete_Sq(T, i, e)) printf("The Element %d is Successfully Deleted!\n", e);
else printf("Delete Error!\n"); // 执行删除函数,根据返回值判断i值是否合法
break;
case 3: Load_Sq(T);
break;
case 0: return 1;
}
}
}
```
顺序表的基本操作包括初始化、插入、删除和输出元素。其中,插入和删除操作需要判断插入/删除位置的合法性。在代码中,使用了动态分配内存的方法来实现顺序表的存储,同时也考虑了存储空间的动态增加。