6-1 顺序表操作集

作者 启迪-数据结构教研组

单位 广西科技大学

本题要求实现顺序表的基本操作集,如初始化、查找、插入、删除等操作。

函数接口定义:

List MakeEmpty(); 
void PrintL(List L);
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, int i);
bool Delete( List L, int i);

其中List结构定义如下:

typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};

各个操作函数的定义为:

(1)List MakeEmpty():创建并返回一个空的线性表。

(2)void PrintL(List L):顺序打印顺序表L里面的数据元素。打印要求:“顺序表的值为:”(按顺序打印顺序表中的数据元素,每个数据元素之后有一个空格,打印结束之后打印换行符)

(3)Position Find( List L, ElementType X ):在线性表L中查找X,若找到,则返回线性表中X的位置;若找不到则返回ERROR。

(4)bool Insert( List L, ElementType X, int i ):将X插入位序i并返回true。若空间已满,则打印“顺序表满,无法插入”并返回false;如果参数i指向非法位置,则打印“插入位置不合法”并返回false。

(5)bool Delete( List L, int i):将位序为i的元素删除并返回true。若参数i指向非法位置,则打印“位序i不存在元素”(其中i是参数值)并返回false。

裁判测试程序样例:

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

#define MAXSIZE 5
#define ERROR -1
typedef enum {false, true} bool;
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};

List MakeEmpty(); 
void PrintL(List L);
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, int i);
bool Delete( List L, int i);

int main()
{
    List L;
    ElementType X;
    Position P;
    int N;

    L = MakeEmpty();
    scanf("%d", &N);
    while ( N-- ) {
        scanf("%d", &X);
        if ( Insert(L, X, 1)==false )
            printf("插入错误:%d无法插入\n", X);
    }

    PrintL(L);
    scanf("%d", &N);
    while ( N-- ) {
        scanf("%d", &X);
        P = Find(L, X);
        if ( P == ERROR )
            printf("查找有误:%d不在表中\n", X);
        else
            printf("%d查找成功,数组下标位置为:%d\n", X, P);
    }
    scanf("%d", &N);
    while ( N-- ) {
        scanf("%d", &P);
        if ( Delete(L, P)==false )
            printf("删除有误\n");
        if ( Insert(L, 0, P)==false )
            printf("插入错误:0无法插入数组下标为%d的位置\n",P);
    }
    PrintL(L);
    return 0;
}
/* 请在这里填写答案 */

输入样例:

在这里给出一组输入。例如:

6
1 2 3 4 5 6
3
6 5 1
2
5 6

输出样例:

在这里给出相应的输出。例如:

顺序表满,无法插入
插入错误:6无法插入
顺序表的值为:5 4 3 2 1 
查找有误:6不在表中
5查找成功,数组下标位置为:0
1查找成功,数组下标位置为:4
位序6不存在元素
删除有误
顺序表满,无法插入
插入错误:0无法插入数组下标为6的位置
顺序表的值为:5 4 3 2 0 

 代码如下

List MakeEmpty() {
    List L1 = (List)malloc(sizeof(struct LNode));
    L1->Last = -1;
    return L1;
}
void PrintL(List L) {
    printf("顺序表的值为:");
    for (int i = 0; i <= L->Last; i++) {
        printf("%d ", L->Data[i]);
    }
    printf("\n");
}
Position Find(List L, ElementType X) {
    for (int i = 0; i <= L->Last; i++) {
        if (L->Data[i] == X) return i;
    }
    return ERROR;
}
bool Insert(List L, ElementType X, int i) {
    int p;
    i = i - 1;
    if (L->Last == MAXSIZE - 1) {
        printf("顺序表满,无法插入\n");
        return false;
    }
    if (i<0 || i > 4) {
        printf("插入位置不合法");
        return false;
    }
    for (p = L->Last; p >= i - 1; p--) {
        L->Data[p + 1] = L->Data[p];
    }
    L->Data[i] = X;
    L->Last++;
    return true;
}
bool Delete(List L, int i) {
    int j;
    i = i - 1;
    if (i<0 || i>L->Last) {
        printf("位序%d不存在元素\n", i+1);
        return false;
    }
    else {
        for (j = i+1 ; j <= L->Last ; j++) {
            L->Data[j - 1] = L->Data[j];
        }
        L->Last--;
        return true;
    }
    return ERROR;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值