初级顺序表

题引

请用顺序表实现动态存储n个数字并且对这个顺序表执行打查询,插入,删除等操作。 
函数接口定义:
顺序表基本操作,序号i从1开始 
Status createList(SqList &L, int n);  
根据用户输入,创建n个元素的顺序表 
void traverseList(SqList L);    
遍历整个顺序表,打印表中元素 
Status getElem(SqList L, int i, ElemType &e);
取值,将序号i的元素存入e 
int locateElem(SqList L, ElemType e);
查找,返回元素e的序号 
Status listInsert(SqList &L, int i, ElemType e); 
插入 ,在第 m 个位置插入元素
Status listDelete(SqList &L, int i);
删除第 m 个位置的元素 
裁判测试程序样例:
#include<stdio.h>
#include<stdlib.h> 
 
// 函数结果状态码 
typedef int Status;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
 
#define MAXSIZE 1024    // 顺序表可能达到的最大长度 
 
typedef int ElemType;    // 元素数据类型 
 
typedef  struct {
    ElemType  *elem;     //指向数据元素的基地址
    int      length;     // 当前长度
} SqList;  // 顺序表
 
// 顺序表基本操作,序号i从1开始 
Status createList(SqList &L, int n);    // 根据用户输入,创建n个元素的表 
void traverseList(SqList L);    // 遍历,打印表中元素 
Status getElem(SqList L, int i, ElemType &e);    // 取值,将序号i的元素存入e 
int locateElem(SqList L, ElemType e);        // 查找,返回元素e的序号 
Status listInsert(SqList &L, int i, ElemType e);    // 插入 
Status listDelete(SqList &L, int i);             // 删除 
 
void destroyList(SqList &L){
    free(L.elem);
    L.elem = NULL;
}
 
int main(){
    SqList list;
    int e, n, m;
    scanf("%d", &n); 
    createList(list, n);
    scanf("%d", &m);
    if(getElem(list,m,e) == OK){  //把 e 赋值为顺序表里的第 m 个数 
        printf("The value of e is %d\n",e);
    }else{
        printf("There is no m number in the list.\n");
    }
    scanf("%d", &e);
    if(locateElem(list,e)){
        printf("The index of %d is %d\n",e,locateElem(list,e));
    }else{
        printf("There is no number %d in the list.\n",e);
    }
    scanf("%d %d",&m,&e); // 插入元素 e 到第 m 个位置 
    if(listInsert(list,m,e) == OK){
        traverseList(list); 
    }else{
        printf("Insert Error\n");
    }
    scanf("%d",&m); // 删除第 m 个元素 
    if(listDelete(list,m) == OK){
        traverseList(list); 
    }else{
        printf("Delete Error\n");
    }
    destroyList(list);
}

input

一组数据
第一行输入一个 n ,表示将把 n 个数放进顺序表里
第二行输入 n 个数,建立顺序表
第三行输入一个 m ,把顺序表里第 m 个数赋值给 e
第四行输入一个 e ,在顺序表中从前往后查找第一个值为 e 的元素为第几个
第五行输入一个 m 个一个 e,在顺序表的第 m 个位置插入 e 元素
第五行输入一个m,删除顺序表里的第m个元素

print

遍历顺序表结果占一行,每个数后面跟一个空格


example
inputing

10
1 2 3 4 5 6 7 8 9 10
5
8
7 11
3

printing

The value of e is 5 
The index of 8 is 8 
1 2 3 4 5 6 11 7 8 9 10 
1 2 4 5 6 11 7 8 9 10 

 ***********************************************************************************************************************

#include<stdio.h>
#include<stdlib.h> 

// 函数结果状态码 
typedef int Status;
#define OK 1
#define ERROR 0
#define OVERFLOW -2

#define MAXSIZE 1024	// 顺序表可能达到的最大长度 

typedef int ElemType;	// 元素数据类型 

typedef  struct {
	ElemType  *elem;     //指向数据元素的基地址
	int      length;     // 当前长度
} SqList;  // 顺序表

// 顺序表基本操作,序号i从1开始 
Status createList(SqList &L, int n);	// 根据用户输入,创建n个元素的表 
void traverseList(SqList L);	// 遍历,打印表中元素 
Status getElem(SqList L, int i, ElemType &e);	// 取值,将序号i的元素存入e 
int locateElem(SqList L, ElemType e);		// 查找,返回元素e的序号 
Status listInsert(SqList &L, int i, ElemType e);	// 插入 
Status listDelete(SqList &L, int i); 			// 删除 

void destroyList(SqList &L){
	free(L.elem);
	L.elem = NULL;
}

int main(){
	SqList list;
	int e, n, m;
	scanf("%d", &n); 
	createList(list, n);
	scanf("%d", &m);
	if(getElem(list,m,e) == OK){  //把 e 赋值为链表里的第 m 个数 
		printf("The value of e is %d\n",e);
	}else{
		printf("There is no m number in the list.\n");
	}
	scanf("%d", &e);
	if(locateElem(list,e)){
		printf("The index of %d is %d\n",e,locateElem(list,e));
	}else{
		printf("There is no number %d in the list.\n",e);
	}
	scanf("%d %d",&m,&e); // 插入元素 e 到第 m 个位置 
	if(listInsert(list,m,e) == OK){
		traverseList(list); 
	}else{
		printf("Insert Error\n");
	}
	scanf("%d",&m); // 删除第 m 个元素 
	if(listDelete(list,m) == OK){
		traverseList(list); 
	}else{
		printf("Delete Error\n");
	}
	
	destroyList(list);
}
Status createList(SqList &L, int n){
	L.elem = new ElemType [n+5];
    for(int i=0;i<n;i++){
    	scanf("%d",L.elem+i);
	}
    L.length=n;
	return OK;
}	 

	
void traverseList(SqList L){
	for(int i=0;i<L.length;i++)
		printf("%d ",L.elem[i]);
	printf("\n");
	
} 
Status getElem(SqList L, int i, ElemType &e){
	if(i<1||i>L.length)
		return ERROR;
	e = L.elem[i-1];
	return OK;
}
int locateElem(SqList L, ElemType e){
	for(int i=0;i<L.length;i++)
	{
		if(L.elem[i]==e)
			return i+1;
	}
	return 0;
}
Status listInsert(SqList &L, int i, ElemType e){
	if((i<1)||(i>L.length+1))
		return ERROR;
	if(L.length == MAXSIZE)
		return ERROR;
	for(int j=L.length-1;j>=i-1;j--){
		L.elem[j+1] = L.elem[j];	
	}
	L.elem[i-1] = e;
	++L.length;		
	return OK;
}
Status listDelete(SqList &L, int i){
	if((i<1)||(i>L.length))
		return ERROR;
	for(int  j=i;j<=L.length;j++)
		L.elem[j-1] = L.elem[j];
	L.length--;
	return OK;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值