华南农业大学数据结构实验答案(包含STL版)

时间:2025-07-11

8576 顺序线性表的基本操作

时间限制:1000MS 内存限制:1000K

提交次数:9027 通过次数:2456

题型: 编程题 语言: 无限制

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("\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

// 请补全代码

printf("%d

int main()

{

SqList T; int a, i; ElemType e, x; if(_________________________) // 判断顺序表是否创建成功 { } while(1) {

element\n3:Load all printf("A Sequence List Has Created.\n"); printf("1:Insert element\n2:Delete

elements\n0:Exit\nPlease choose:\n");

scanf("%d",&a); switch(a) { case 1: scanf("%d%d",&i,&x);

if(_________________________)

Error!\n"); // 判断i值是否合法,请填空

x);

printf("Insert else printf("The Element %d is Successfully Inserted!\n", break; case 2: scanf("%d",&i);

printf("Delete if(_________________________)

Error!\n"); // 判断i值是否合法,请填空

e);

else printf("The Element %d is Successfully Deleted!\n", break;

}

} } break; case 0: return 1;

输入格式

测试样例格式说明:

根据菜单操作:

1、输入1,表示要实现插入操作,紧跟着要输入插入的位置和元素,用空格分开

2、输入2,表示要实现删除操作,紧跟着要输入删除的位置

3、输入3,表示要输出顺序表的所有元素

4、输入0,表示程序结束

输入样例

1

1 2

1

1 3

2

1

3

输出样例

A Sequence List Has Created.

1:Insert element

2:Delete element

3:Load all elements

0:Exit

Please choose:

The Element 2 is Successfully Inserted!

1:Insert element

2:Delete element

3:Load all elements

0:Exit

Please choose:

The Element 3 is Successfully Inserted!

1:Insert element

2:Delete element

3:Load all elements

0:Exit

Please choose:

The Element 3 is Successfully Deleted!

1:Insert element

2:Delete element

3:Load all elements

0:Exit

Please choose:

The List is: 2

1:Insert element

2:Delete element

3:Load all elements

0:Exit

Please choose:

作者 yqm

解法一:(正规解法)

#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 0;

int Load_Sq(SqList &L)

{

// 输出顺序表中的所有元素

}

int ListInsert_Sq(SqList &L,int i,int e)

{

// 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e

// i的合法值为1≤i≤L.length +1

// 请补全代码

ElemType *newbase,*p,*q;

if(i<1||i>L.length+1)return ERROR;

if(L.length>=L.listsize)

{

newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));

L.elem=newbase; int i; if(L.length==0) printf("The List is empty!"); // 请填空 else { } printf("\n"); return OK; printf("The List is: "); for(i=0;i<L.length;i++) printf("%d ",L.elem[i]); // 请填空

L.listsize+=LISTINCREMENT;

}

q=&(L.elem[i-1]);

for(p=&(L.elem[L.length-1]);p>=q;--p)

*(p+1)=*p;

*q=e;

++L.length;

return OK;

}

int ListDelete_Sq(SqList &L,int i, int &e)

{

// 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值

// i的合法值为1≤i≤L.length

// 请补全代码

ElemType *p,*q;

if(i<1||i>L.length)return ERROR;

p=&(L.elem[i-1]);

e=*p;

q=L.elem+L.length-1;

for(++p;p<=q;++p)

*(p-1)=*p;

-- …… 此处隐藏:4811字,全部文档内容请下载后查看。喜欢就下载吧 ……

华南农业大学数据结构实验答案(包含STL版).doc 将本文的Word文档下载到电脑

    精彩图片

    热门精选

    大家正在看

    × 游客快捷下载通道(下载后可以自由复制和排版)

    限时特价:7 元/份 原价:20元

    支付方式:

    开通VIP包月会员 特价:29元/月

    注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
    微信:fanwen365 QQ:370150219