顺序表的相关操作(查找、插入、删除、有序插入)

一、查找

本题要求实现一个函数,要求从顺序表中查找指定元素,并返回第一个查找成功的元素在表中的位置序号,若查找失败,则返回0;

函数接口定义:

int LocateElem(SqList L,ElemType e);

其中SqList结构定义如下:

typedef struct{
	ElemType *elem;
	int length;
 }SqList;
	

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
typedef int ElemType;
typedef struct{
	ElemType *elem;
	int length;
 }SqList;
void InitList(SqList &L);/*细节在此不表*/
int LocateElem(SqList L,ElemType e);

int main()
{
	SqList L;
	InitList(L);
	ElemType e;
	int p;
	scanf("%d",&e);
	p=LocateElem(L,e);
	printf("The position of %d in SequenceList L is %d.",e,p);
	return 0;
}

/* 请在这里填写答案 */

输入格式:

输入数据有1行,首先给出以-1结束的顺序表元素值(不超过100个,-1不属于顺序表元素),然后是待查找的元素值。所有数据之间用空格分隔。

输入样例:

2 6 4 9 13 -1 2

输出样例:

The position of 2 in SequenceList L is 1.

【代码】

int LocateElem(SqList L,ElemType e)
{
    int i;
    for(i=0;i<L.length;i++)
    {
        if(L.elem[i]==e) return i+1;
    }
    return 0;
}

 

二、插入

本题要求实现一个函数,在顺序表的第i个位置插入一个新的数据元素e,插入成功后顺序表的长度加1,函数返回值为1;插入失败函数返回值为0;

函数接口定义:

int ListInsert(SqList &L,int i,ElemType e);

其中SqList结构定义如下:

typedef struct{
	ElemType *elem;
	int length;
 }SqList;
 

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
typedef int ElemType;
typedef struct{
	ElemType *elem;
	int length;
 }SqList;
void InitList(SqList &L);/*细节在此不表*/
int ListInsert(SqList &L,int i,ElemType e);
int main()
{
	SqList L;
	InitList(L);
	ElemType e;
	int i;
	scanf("%d%d",&i,&e);
	int result=ListInsert(L,i,e);
	if(result==0){
		printf("Insertion Error.The value of i is unlawful or the storage space is full!");	
	}else if(result==1){
		printf("Insertion Success.The elements of the SequenceList L are:");	
		for(int j=0;j<L.length;j++){
			printf(" %d",L.elem[j]);
		}
	}
	return 0;
}
/* 请在这里填写答案 */

输入格式:

输入数据有1行,首先给出以-1结束的顺序表元素值(不超过100个,-1不属于顺序表元素),然后是插入位置和被插入元素值。所有数据之间用空格分隔。

输入样例:

2 6 4 -1 2 100

输出样例:

Insertion Success.The elements of the SequenceList L are: 2 100 6 4

 

顺序表的插入实现中,要注意插入e的位置,e可以插在表中第一个数之前,也可以插在最后一个数之后,因此判断的时候要注意一下。

【代码】

int ListInsert(SqList &L,int i,ElemType e)
{
    if(i<0||i>L.length+1||L.length==MAXSIZE) return 0;
    else
    {
        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 1;
}

 

三、删除

本题要求实现一个函数,要求将顺序表的第i个元素删掉,成功删除返回1,否则返回0; 

函数接口定义:

int ListDelete(SqList &L,int i);

其中SqList结构定义如下:

typedef struct{
	ElemType *elem;
	int length;
 }SqList;

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
typedef int ElemType;
typedef struct{
	ElemType *elem;
	int length;
 }SqList;
void InitList(SqList &L);/*细节在此不表*/
int ListDelete(SqList &L,int i);
int main()
{
	SqList L;
	InitList(L);
	int i;
	scanf("%d",&i);
	int result=ListDelete(L,i);
	if(result==0){
		printf("Delete Error.The value of i is illegal!");	
	}else if(result==1){
		printf("Delete Success.The elements of the SequenceList L are:");	
		for(int j=0;j<L.length;j++){
			printf(" %d",L.elem[j]);
		}
	}
	return 0;
}
/* 请在这里填写答案 */

输入格式:

输入数据有1行,首先给出以-1结束的顺序表元素值(不超过100个,-1不属于顺序表元素),然后是删除位置。所有数据之间用空格分隔。

输入样例:

2 6 4 -1 1

输出样例:

Delete Success.The elements of the SequenceList L are: 6 4

【代码】

int ListDelete(SqList &L,int i)
{
    if(i<=0||i>L.length) return 0;
    for(int j=i-1;j<L.length;j++)
    {
        L.elem[j]=L.elem[j+1];
    }
    L.length--;
    return 1;
}

 

四、有序插入

本题要求实现一个函数,要求将指定元素插入到有序表的合适位置,使得插入后仍然保持有序,若插入失败返回0;插入成功则返回1,并且顺序表的长度加1.

函数接口定义:

int SqInsert(SqList &L,ElemType e);

其中SqList结构定义如下:

typedef struct{
	ElemType *elem;
	int length;
 }SqList;

裁判测试程序样例:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
typedef int ElemType;
typedef struct{
	ElemType *elem;
	int length;
}SqList;
void InitList(SqList &L);/*函数的实现此处不再显示*/
int SqInsert(SqList &L,ElemType e);
int main()
{
	SqList L;
	InitList(L);
	ElemType e;
	scanf("%d",&e);
	int result=SqInsert(L,e);
	if(result==0){
		printf("Insertion Error.The storage space is full!");	
	}else if(result==1){
		printf("Insertion Success.The elements of the SequenceList L are:");	
		for(int j=0;j<L.length;j++){
			printf(" %d",L.elem[j]);
		}
	}
	return 0;
}

/* 请在这里填写答案 */

输入格式:

输入数据有1行,首先给出以-1结束的非递减顺序表元素值(不超过100个,-1不属于顺序表元素,),然后是被插入元素值。所有数据之间用空格分隔。

输入样例:

4 8 20 -1 10 

输出样例:

Insertion Success.The elements of the SequenceList L are: 4 8 10 20

从后往前边遍历更加方便。

【代码】

int SqInsert(SqList &L, ElemType e)
{
	int i;
    if(L.length==MAXSIZE) return 0;
    for(i=L.length-1;i>=0;i--)
    {
        if(L.elem[i]>e) L.elem[i+1]=L.elem[i];
        else break;
    }
    L.elem[i+1]=e;
    L.length++;
    return 1;
}

 

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值