线性表-插入-删除

#include<stdio.h>
#define error 0
#define ok 1
#define true 1
#define false 0
#define maxn 100
typedef int Status;
typedef int ElemType;

typedef struct
{
    ElemType elem[maxn];
    int length;
} SqList;

Status ListInsert(SqList &L, int i, ElemType e)///插入函数
{
    int j;
    if(L.length==maxn)return error;
    if(i<1 || i>L.length+1)return error;

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

Status ListDelete(SqList &L, int i, ElemType *e)///删除函数
{
    int j;
    if(L.length==0)return error;
    if(i<1 || i>L.length)return error;

    *e=L.elem[i-1];
    for(j=i-1; j<L.length; j++)
        L.elem[j]=L.elem[j+1];

    L.length--;
    return ok;
}

Status InitList(SqList &L)
{
    L.length=0;
    return ok;
}

void Trans(SqList L)
{
    int j;
    for(j=0; j<L.length; j++)
        printf("%d%c", L.elem[j], j==L.length-1?'\n':' ');
}
int main()
{
    SqList L;
    int a;

    ///1)初始化顺序表
    InitList(L);

    ///2)插入一些元素: 12,23,34,45
    ListInsert(L,1,12);
    ListInsert(L,1,23);
    ListInsert(L,1,34);
    ListInsert(L,1,45);
    Trans(L);
    ListDelete(L, 1, &a);

    printf("\n删除的元素:\n%d\n", a);
    Trans(L);
    return 0;
}

 

转载于:https://www.cnblogs.com/w-y-1/p/5866139.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值