数据结构 顺序表的基本操作

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
{
    ElemType *elem;//存储的是数组第一个元素的地址。
    int length;//数组长度。
    ElemType listsize;//表示储存容量。
}SqList;

ElemType InitList_Sq(SqList *L)//初始化数组,数组初始长度为0,数组存储容量大小LIST_INIT_SIZE。
{
    L->elem=(ElemType*)malloc(LIST_INIT_SIZE* sizeof(ElemType));
    if(!L->elem)
        return ERROR;
    L->length=0;
    L->listsize=LIST_INIT_SIZE;
    return OK;
}

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

ElemType ListInsert_Sq(SqList *L,int i,ElemType e)//将值为e的数据插入到第i个。
{
    if(i<1||i>L->length+1)
        return ERROR;
    int j;
    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;
}

ElemType ListDelete_Sq(SqList *L,int i, ElemType *p)//删除第i个元素,其值存放在以p为地址的内存中。
{
    if(i<1||i>L->length)
        return ERROR;
    int j;
    *p=L->elem[i-1];
    for(j=i-1;j<L->length-1;j++)
    {
        L->elem[j]=L->elem[j+1];
    }
    L->length--;
    return OK;
}

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

可以创建命名后缀为.c的文件,并成功运行。

运行结果如下:

A Sequence List Has Created.
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
1
1 2
The Element 2 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
3
The List is: 2
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
1
1 3
The Element 3 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
3
The List is: 3 2
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
2
1
The Element 3 is Successfully Deleted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
3
The List is: 2
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
0

Process returned 1 (0x1)   execution time : 91.698 s
Press any key to continue.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值