SCAU数据结构——8576顺序线性表的基本操作

题目

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;
		}
	}
}

输入格式
测试样例格式说明:
根据菜单操作:
1、输入1,表示要实现插入操作,紧跟着要输入插入的位置和元素,用空格分开
2、输入2,表示要实现删除操作,紧跟着要输入删除的位置
3、输入3,表示要输出顺序表的所有元素
4、输入0,表示程序结束

输入样例
1
1 2
1
1 3
2
1
3
0

输出样例
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:

答案

#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)
{
  L.elem= new ElemType[LIST_INIT_SIZE];
  if(!L.elem)
    return ERROR;
  L.length=0;
  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;
  if(L.length==LIST_INIT_SIZE)
    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;
  return OK;

}

int ListDelete_Sq(SqList &L,int i, int &e)
{
// 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
// i的合法值为1≤i≤L.length
// 请补全代码
  int j;
  if((i<1)||(i>L.length))
    return ERROR;
    e=L.elem[i-1];
  for(j=i;j<=L.length;j++)
  L.elem[j-1]=L.elem[j];
  --L.length;
  return OK;
}

int main()
{
	SqList T;
	int a, i;
	ElemType e, x;
	if((InitList_Sq(T))==1) // 判断顺序表是否创建成功
	{
		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)==0) 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)==0) 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;
		}
	}
}

期末考死记硬背

初始化线性表(init)

 L.elem= new ElemType[LIST_INIT_SIZE];
  if(!L.elem)
    return ERROR;
  L.length=0;
  return OK;

输出顺序表中的所有元素(load)

    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;

在线性表中第i个位置之前插入新的元素(insert)

int j;
  if((i<1)||(i>L.length+1))//插入位置超出范围
    return ERROR;
  if(L.length==LIST_INIT_SIZE)//线性表已满
    return ERROR;
  for(j=L.length-1;j>=i-1;j--)
  /*从后往前,把位置为从i-1到L.length-1的元素都往后移一位*/
    L.elem[j+1]=L.elem[j];
  L.elem[i-1]=e;//第i个元素的下标(位置)为i-1
  ++L.length;//线性表长度+1
  return OK;//记得返回OK

在线性表中删除第i个位置的元素并用e返回其值(delete)

int j;
  if((i<1)||(i>L.length))//超出合法范围
    return ERROR;
    e=L.elem[i-1];//先赋值
  for(j=i;j<=L.length;j++)
  /*从前往后,把位置为从i到L.length的元素都往前移一位*/
  L.elem[j-1]=L.elem[j];
  --L.length;//线性表长度-1
  return OK;//记得返回OK

main函数中的三个if

if((InitList_Sq(T))==1) // 判断顺序表是否创建成功
if(ListInsert_Sq(T,i,x)==0) // 判断i值是否合法
if(ListDelete_Sq(T,i,e)==0) // 判断i值是否合法
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值