顺序表的建立,插入,与删除

测试版

#include<stdio.h>
#include<string.h>
#define N 100
int ans=0;
typedef struct
{
    int elem[N];
    int last;
} seqlist;
void inslist(seqlist *L,int i,int e)
{
    if(i<1||i>L->last+2||L->last>=N-1)//1<=i<=L->last+2
    {
        printf("插入错误!\n\n");
        ans=1;
        return;
    }
    for(int k=L->last; k>=i-1; k--)
        L->elem[k+1]=L->elem[k];
    L->elem[i-1]=e;
    L->last++;
    return;
}
void del(seqlist *L,int i)  //1<=i<<L->last+1
{
    if((i<1)||(i>L->last+1))
    {
        printf("删除错误!\n\n");
        ans=1;
        return;
    }
    for(int k=i; k<=L->last; k++)
        L->elem[k-1]=L->elem[k];
    L->last--;
    return;
}
void Operation()
{
    printf("首先输入元素m,然后输入m个元素\n");
    printf("在第x个位置前插入元素y请输入:I x y\n");
    printf("删除第x个元素请输入: D x\n输入'E'结束\n");
}
void prin(seqlist *L)
{
    for(int i=0; i<=L->last; i++)
        printf("%d ",L->elem[i]);
    printf("\n\n");
}
int main()
{
    char Q;
    Operation();
    int n,m,i,j,x,y;
    seqlist *L,test;
    scanf("%d",&m);
    for(i=0; i<m; i++)
        scanf("%d",&test.elem[i]);
    test.last=m-1;
    L=&test;
    getchar();
    while(~scanf("%c",&Q))
    {
        ans=0;
        if(Q=='E')break;
        if(Q=='D')
        {
            scanf("%d",&x);
            del(L,x);
            if(ans) continue;
            printf("删除第%d个元素得:\n",x);
            prin(L);
            continue;
        }
        if(Q=='I')
        {
            scanf("%d%d",&x,&y);
            inslist(L,x,y);
            if(ans) continue;
            printf("在第%d个位置前插入元素%d得:\n",x,y);
            prin(L);
        }
    }
    return 0;
}
/*
sample input:
10
1 2 3 4 5 6 7 8 9 10
I 3 999
I 2 998
D 999
D 2
D 9
D 999
E
*/

#include<stdio.h>
#include<string.h>
#define N 100
int ans=0;
typedef struct
{
    int elem[N];
    int last;
} seqlist;
void inslist(seqlist *L,int i,int e)
{
    if(i<1||i>L->last+2||L->last>=N-1)//1<=i<=L->last+2
    {
        printf("Insert Error\n");
        ans=1;
        return;
    }
    for(int k=L->last; k>=i-1; k--)
        L->elem[k+1]=L->elem[k];
    L->elem[i-1]=e;
    L->last++;
    return;
}
void del(seqlist *L,int i)  //1<=i<<L->last+1
{
    if((i<1)||(i>L->last+1))
    {
        printf("Delete Error\n\n");
        ans=1;
        return;
    }
    for(int k=i; k<=L->last; k++)
        L->elem[k-1]=L->elem[k];
    L->last--;
    return;
}
void Operation()
{
    printf("First enter element m then enter m elements\n");
    printf("To insert element y before the xth position enter:I x y\n");
    printf("To delete the xth element please enter: D x\nEnd of input 'E'\n");
}
void prin(seqlist *L)
{
    for(int i=0; i<=L->last; i++)
        printf("%d ",L->elem[i]);
    printf("\n\n");
}
int main()
{
    char Q;
    Operation();
    int n,m,i,j,x,y;
    seqlist *L,test;
    scanf("%d",&m);
    for(i=0; i<m; i++)
        scanf("%d",&test.elem[i]);
    test.last=m-1;
    L=&test;
    getchar();
    while(~scanf("%c",&Q))
    {
        ans=0;
        if(Q=='E')break;
        if(Q=='D')
        {
            scanf("%d",&x);
            del(L,x);
            if(ans) continue;
            printf("Delete the %dth element\n",x);
            prin(L);
            continue;
        }
        if(Q=='I')
        {
            scanf("%d%d",&x,&y);
            inslist(L,x,y);
            if(ans) continue;
            printf("Insert element %d before the %dth position\n",y,x);
            prin(L);
        }
    }
    return 0;
}
/*
sample input:
10
1 2 3 4 5 6 7 8 9 10
I 3 999
I 2 998
D 999
D 2
D 9
D 999
E
sameple output:

Insert element 999 before the 3th position
1 2 999 3 4 5 6 7 8 9 10

Insert element 998 before the 2th position
1 998 2 999 3 4 5 6 7 8 9 10

Delete Error

Delete the 2th element
1 2 999 3 4 5 6 7 8 9 10

Delete the 9th element
1 2 999 3 4 5 6 7 9 10

Delete Error

E
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值