C语言顺序表增删改查

我要考研,正在复习数据结构,此贴只用作记录。。。废话不多说,直接上代码。。


# include "stdlib.h"
# include "stdio.h"

# define MAXSIZE 20
# define OK 1
# define ERROR 0

typedef int Status;//自定义数据类型 相当于int仅用作装逼

typedef struct // 自定义数据类型,用来存放顺序表中的元素值
{
    int dataA;
    int dataB;
}ElemType;

typedef struct{//顺序表
    ElemType *elem;
    int length;
}SqList;

/**将顺序表初始化*/
Status InitList(SqList *L){
    L->elem = (ElemType *)malloc(sizeof(ElemType)*MAXSIZE);
    if (!L->elem)
    {
        printf("初始化失败\n");
        exit(-1);
    }
    L->length = 0;
    printf("初始化完成\n");
    return OK;
}
//按位取元素
Status GetElem(SqList *L ,int n){
    if (L->length == 0)
    {
        printf("此表为空\n");
        return ERROR;
        /* code */
    }else if (n > L->length || n<0)
    {
        printf("不存在该序号的元素,现在顺序表中共有%d个元素\n",L->length);
        return ERROR;
        /* code */
    }else{
        printf("第%d个元素内容是:\n\tdataA:%d\n\tdataB:%d\n",n,((L->elem)+n-1)->dataA, ((L->elem)+n-1)->dataB);
    }
    
    
    return OK;
}
//查找元素
Status LocateElem(SqList *L, ElemType *e){
    if (L->length == 0)
    {
        printf("顺序表为空,没有你想要查找的元素\n");
        /* code */
        return ERROR;
    }
    for (int i = 0; i < L->length; i++)
    {
        if (e->dataA == ((L->elem)+i)->dataA && e->dataB == ((L->elem)+i)->dataB){
            printf("你要查找的元素在顺序表第%d位\n",i+1);
            return OK;
        }
        /* code */
    }
    printf("顺序表中没有你想要的元素\n");
    return ERROR;
}
//插入元素
Status ListInsert(SqList *L, int n, ElemType *e){
    if (n<1 || n>L->length+1 || n > MAXSIZE)//判断想要插入的位置是否可行
    {
        printf("插入位置不正确\n");
        return ERROR;
        /* code */
    }
    if (n == L->length+1)
    {
        ((L->elem)+n-1)->dataA = e->dataA;
        ((L->elem)+n-1)->dataB = e->dataB;
        L->length++;
        printf("插入成功\n");
        /* code */
    }else
    {
        for (int i = L->length; i > n-1; i--)
        {
            ((L->elem)+i)->dataA = ((L->elem)+i-1)->dataA;
            ((L->elem)+i)->dataB = ((L->elem)+i-1)->dataB;
            /* code */
        }
        ((L->elem)+n-1)->dataA = e->dataA;
        ((L->elem)+n-1)->dataB = e->dataB;
        L->length++;  
        printf("插入成功\n");
    }
    return OK;
}
//删除元素
Status ListDelete(SqList *L, int i){
    if(i>L->length){
        printf("你想删除的第%d个元素不存在\n");
        return ERROR;
    }
    printf("你要删除的顺序表中的第%d个元素:dataA=%d,dataB=%d\n",i,((L->elem)+i-1)->dataA,((L->elem)+i-1)->dataB);
    for (i; i < L->length; i++)
    {
        ((L->elem)+i-1)->dataA = ((L->elem)+i)->dataA;
        ((L->elem)+i-1)->dataB = ((L->elem)+i)->dataB;
        /* code */
    }
    L->length--;
    printf("删除成功\n");
    return OK;
}
//遍历顺序表
Status ListPrint(SqList *L){
    if (L->length == 0){   
        printf("此表为空\n");
        return ERROR;
        /* code */
    }
    for (int i = 0; i < L->length; i++)
    {
        printf("dataA:%d, dataB:%d\n",((L->elem)+i)->dataA,((L->elem)+i)->dataB);
        /* code */
    }
    return OK;
}
//查看表长
Status ListLength(SqList *L){
    printf("顺序表长度:%d\n",L->length);
    return OK;
}
//清空顺序表
Status ListClear(SqList *L){
    L->length = 0;
    printf("顺序表已经清空\n");
    return OK;
}
//判断顺序表是否为空
Status ListEmpty(SqList *L){
    if(L->length == 0){
        printf("顺序表为空\n");
        return OK;
    }else{
        printf("顺序表不为空\n");
        return ERROR;
    }
}
//销毁顺序表
Status ListDestroy(SqList *L){
    if (L)
    {
        free(L->elem);
        L->elem = NULL;
        printf("顺序表已被销毁\n");
        return OK;
    }else
    {
        return ERROR;
    }
}
int main(int argc, char const *argv[])
{
    ElemType A;
    ElemType B;
    ElemType C;
    ElemType D;
    A.dataA = 1;
    A.dataB = 1;
    B.dataA = 2;
    B.dataB = 2;
    C.dataA = 3;
    C.dataB = 3;
    D.dataA = 4;
    D.dataB = 4;
    SqList L;
    InitList(&L);
    ListInsert(&L,1,&A);
    ListLength(&L);
    ListInsert(&L,2,&B);
    ListLength(&L);
    ListInsert(&L,3,&C);
    ListLength(&L);
    ListInsert(&L,4,&D);
    ListLength(&L);
    ListPrint(&L);
    GetElem(&L,2);
    LocateElem(&L,&D);
    ListDelete(&L,2);
    ListPrint(&L);
    GetElem(&L,4);
    ListLength(&L);
    ListEmpty(&L);
    ListClear(&L);
    ListLength(&L);
    ListEmpty(&L);
    ListDestroy(&L);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值