装逼太累,估计只有我才知道自己在瞎写个啥。。。
Description 编写算法,创建初始化容量为LIST_INIT_SIZE的顺序表T,并实现插入、删除、遍历操作。本题目给出部分代码,请补全内容。
#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;
}
}
}
以上是题目要求
自己瞎几把填的:
首先初始化一个空的线性表L
int InitList_Sq(SqList &L)
{
L.elem=(ElemType* )malloc(LIST_INIT_SIZE*sizeof(ElemType));//存储空间分配
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 j;
if(i<1||i>L.length+1) return ERROR;
for(j=L.length-1;j>=i-1;j–)
{
L.elem[j+1]=L.elem[j];
}
L.elem[i-1]=e;
L.length=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 j,x;
if((i<1)||(i>L.length)) return ERROR;
e=L.elem[i-1];
for(j=i-1;j<L.length-1;j++)
{
L.elem[j]=L.elem[j+1];
}
L.length=L.length-1;
return OK;
}
综上所述,
#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));
L.length=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 j;
if(i<1||i>L.length+1) return ERROR;
for(j=L.length-1;j>=i-1;j--)
{
L.elem[j+1]=L.elem[j];
}
L.elem[i-1]=e;
L.length=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 j,x;
if((i<1)||(i>L.length)) return ERROR;
e=L.elem[i-1];
for(j=i-1;j<L.length-1;j++)
{
L.elem[j]=L.elem[j+1];
}
L.length=L.length-1;
return OK;
}
int main()
{
SqList T;
int a, i;
ElemType e, x;
if(InitList_Sq(T)==OK) // 判断顺序表是否创建成功
{
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)==ERROR) printf("Insert Error!\n"); // 判断i值是否合法,请填空
else printf("The Element %d is Successfully Inserted!\n", x);
break;
case 2: scanf("%d",&i);
if(ListDelete_Sq(T,i,e)==ERROR) 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;
}
}
}