69,382
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 0
#define error -1
#define OVERFLOW -2
typedef int ElemType;
typedef struct _SqList
{
ElemType *elem;
int length; //表长度
int listsize; //当前分配的存储容量
}SqList,*pSqList;
//增加存储空间容量
int addcapacity(SqList *sqlist)
{
ElemType *newbase; //分配一个新的基址
newbase = (ElemType *)malloc((sqlist->listsize + LISTINCREMENT) * sizeof(ElemType));
if (newbase == NULL)
exit(OVERFLOW);
sqlist->elem = newbase;
sqlist->listsize += LISTINCREMENT;
return OK;
}
//初始化顺序表
int InitList_sq(SqList *sqlist)
{
if (sqlist == NULL)
return error;
//给线性表分配初始容量
sqlist->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!sqlist->elem)
exit(OVERFLOW);
sqlist->length = 0;
sqlist->listsize = LIST_INIT_SIZE;
return OK;
}
//在表的第i个位置插入新元素newelem
int ListInsert(SqList *sqlist, int i, ElemType newelem)
{
if (sqlist == NULL || i<1 || i>sqlist->length + 1)
{
return error;
}
if (sqlist->length > sqlist->listsize) //检查线性表是否已满,如果满了就扩充空间容量
{
if (addcapacity(sqlist) != OK)
return OVERFLOW;
}
//将第i个元素以及第i个元素后面的元素后移
for (int j = sqlist->length; j >= i; j--)
sqlist->elem[j] = sqlist->elem[j - 1];
sqlist->elem[i - 1] = newelem;
sqlist->elem++;
return OK;
}
//顺性表输出
void Print_list(SqList *sqlist)
{
int i;
for (i = 0; i < sqlist->length; i++)
printf("%d ", sqlist->elem[i]);
printf("\n");
}
int main()
{
SqList L;
InitList_sq(&L);
for (int i = 0; i < 10; i++) //将0-9插入顺序表
{
ListInsert(&L, i + 1, i);
}
Print_list(&L);
return 0;
}
//将第i个元素以及第i个元素后面的元素后移
for (int j = sqlist->length; j >= i; j--)
sqlist->elem[j] = sqlist->elem[j - 1];
这个操作也可以去掉了,冗余代码#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 0
#define error -1
#define OVERFLOW -2
typedef int ElemType;
typedef struct _SqList
{
ElemType *elem;
int length; //表长度
int listsize; //当前分配的存储容量
}SqList,*pSqList;
//增加存储空间容量
int addcapacity(SqList *sqlist)
{
ElemType *newbase; //分配一个新的基址
//newbase = (ElemType *)malloc((sqlist->listsize + LISTINCREMENT) * sizeof(ElemType));
newbase = (ElemType *)realloc(sqlist->elem, (sqlist->listsize + LISTINCREMENT) * sizeof(ElemType));
if (newbase == NULL)
exit(OVERFLOW);
sqlist->elem = newbase;
sqlist->listsize += LISTINCREMENT;
return OK;
}
//初始化顺序表
int InitList_sq(SqList *sqlist)
{
if (sqlist == NULL)
return error;
//给线性表分配初始容量
sqlist->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!sqlist->elem)
exit(OVERFLOW);
sqlist->length = 0;
sqlist->listsize = LIST_INIT_SIZE;
return OK;
}
//在表的第i个位置插入新元素newelem
int ListInsert(SqList *sqlist, int i, ElemType newelem)
{
if (sqlist == NULL || i < 1 || i > sqlist->length + 1)
{
return error;
}
if (sqlist->length >= sqlist->listsize) //检查线性表是否已满,如果满了就扩充空间容量
{
if (addcapacity(sqlist) != OK)
return OVERFLOW;
}
//将第i个元素以及第i个元素后面的元素后移
for (int j = sqlist->length; j >= i; j--)
sqlist->elem[j] = sqlist->elem[j - 1];
sqlist->elem[i - 1] = newelem;
//sqlist->elem++;
sqlist->length++;
return OK;
}
//顺性表输出
void Print_list(SqList *sqlist)
{
int i;
for (i = 0; i < sqlist->length; i++)
printf("%d ", sqlist->elem[i]);
printf("\n");
}
int main()
{
SqList L;
InitList_sq(&L);
for (int i = 0; i < 10; i++) //将0-9插入顺序表
{
ListInsert(&L, i + 1, i);
}
Print_list(&L);
return 0;
}
参考一下吧
问题1:addcapacity函数内,建议使用realloc而不能使用malloc,因为使用malloc确实把空间扩展了,但是原来的数据应该就没了,realloc就可以避免这个问题。
问题2:insert函数内,没有对length进行修改,那么比较空间是否需要扩展就没有意义了,因为length还是0.
问题3:insert函数内,sqlist->elem++;这个操作是修改了指针的位置,这个是为什么你输出的时候没有任何数据的原因,当然还跟length没有修改有关;
问题4:if (sqlist->length > sqlist->listsize) //检查线性表是否已满,如果满了就扩充空间容量
{
if (addcapacity(sqlist) != OK)
return OVERFLOW;
}
这个比较建议加上=,因为不加会越界。因为下表的值是从0~listsize-1;