1-1 顺序表操作集

本题要求实现顺序表的操作集。

函数接口定义:

List MakeEmpty(); 
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );

其中List结构定义如下:

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

各个操作函数的定义为:

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

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

bool Insert( List L, ElementType X, Position P ):将X插入在位置P并返回true。若空间已满,则打印“FULL”并返回false;如果参数P指向非法位置,则打印“ILLEGAL POSITION”并返回false;

bool Delete( List L, Position P ):将位置P的元素删除并返回true。若参数P指向非法位置,则打印“POSITION P EMPTY”(其中P是参数值)并返回false。

程序:

List MakeEmpty()
{
    List l;
    l = (List)malloc(sizeof(struct LNode));
    l->Last = -1;//为空时last设为-1
    return l;
}
Position Find(List L, ElementType X)
{
    int i;
    for (i = 0; i <= L->Last; i++)
    {
        if (X == L->Data[i])
        {
            return i;
        }
    }
    return ERROR;//找不到返回ERROR
}
bool Insert(List L, ElementType X, Position P)
{
    int i;
    if (L->Last == MAXSIZE - 1)//判断是否满了
    {
        printf("FULL");
        return false;
    }
    if (P < 0 || P > L->Last + 1 || P > MAXSIZE - 1)//非合法位置小于0或者大于最后一个元素的位置或者超出最大值
    {
        printf("ILLEGAL POSITION");
        return false;
    }
    if (L->Last < MAXSIZE - 1)
    {
        for (i = L->Last; i >= P; i--)
        {
            L->Data[i + 1] = L->Data[i];
        }
        L->Data[P] = X;
        L->Last++;
        return true;
    }
}
bool Delete(List L, Position P)
{
    int i;
    if (P < 0 || P > L->Last)//为空判断
    {
        printf("POSITION %d EMPTY", P);
        return false;
    }
    for (i = P; i < L->Last; i++)
    {
        L->Data[i] = L->Data[i + 1];
    }
    L->Last--;
    return true;
}
### 关于C语言实现顺序表操作的数据结构示例代码 以下是基于提供的参考资料所整理的一个完整的顺序表操作的C语言实现。此代码涵盖了顺序表的基本功能,包括初始化、创建列表、显示列表、插入元素、删除元素以及获取指定索引处的元素。 #### 完整代码示例 ```c #include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 // 最大容量 #define ElemType int // 元素类型定义为int typedef struct { ElemType data[MAXSIZE]; int length; } SqList; // 初始化顺序表 void InitSqList(SqList *L) { L->length = 0; // 初始长度设为0 } // 创建顺序表填充初始数据 void CreateList(SqList *L, int n) { if (n > MAXSIZE || n < 0) { printf("超出范围或非法输入\n"); return; } for (int i = 0; i < n; ++i) { scanf("%d", &L->data[i]); } L->length = n; } // 显示顺序表的内容 void DisplayList(SqList L) { if (L.length == 0) { printf("当前顺序表。\n"); return; } for (int i = 0; i < L.length; ++i) { printf("%d ", L.data[i]); } printf("\n"); } // 插入元素到指定位置 Status InsertElement(SqList *L, int pos, ElemType value) { if (pos < 1 || pos > L->length + 1) { // 检查位置合法性 printf("插入位置不合法\n"); return -1; } if (L->length >= MAXSIZE) { // 检查是否已满 printf("顺序表已满无法插入\n"); return -1; } for (int j = L->length; j >= pos; --j) { // 向后移动元素 L->data[j] = L->data[j - 1]; } L->data[pos - 1] = value; // 插入新值 L->length++; // 长度加一 return 1; } // 删除指定位置的元素 Status DeleteElement(SqList *L, int pos, ElemType *value) { if (pos < 1 || pos > L->length) { // 检查位置合法性 printf("删除位置不合法\n"); return -1; } *value = L->data[pos - 1]; // 获取被删元素值 for (int j = pos; j < L->length; ++j) { // 前移后续元素 L->data[j - 1] = L->data[j]; } L->length--; // 长度减一 return 1; } // 主函数测试上述方法 int main() { SqList L; InitSqList(&L); printf("请输入要创建顺序表元素数量(<= %d): ", MAXSIZE); int num; scanf("%d", &num); printf("依次输入%d个元素:\n", num); CreateList(&L, num); printf("当前顺序表内容: "); DisplayList(L); int insPos, insVal; printf("请输入要插入的位置和数值(如:2 99):"); scanf("%d %d", &insPos, &insVal); if (InsertElement(&L, insPos, insVal)) { printf("插入后的顺序表内容: "); DisplayList(L); } int delPos, delVal; printf("请输入要删除的位置:"); scanf("%d", &delPos); if (DeleteElement(&L, delPos, &delVal)) { printf("删除成功,删除的元素是:%d\n", delVal); printf("删除后的顺序表内容: "); DisplayList(L); } return 0; } ``` 以上代码实现了顺序表的核心操作,通过`main()`函数进行了简单的交互演示[^4]。 --- #### 解释说明 - **初始化**:`InitSqList`用于设置顺序表的初始状态。 - **创建列表**:`CreateList`允许用户手动输入若干个元素来构建顺序表- **展示列表**:`DisplayList`负责打印顺序表中的所有元素。 - **插入元素**:`InsertElement`支持在任意合法位置插入新的元素。 - **删除元素**:`DeleteElement`可以从特定位置移除一个元素将该值返回给调用者。 这些基础的操作构成了顺序表的主要行为模式[^3]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值