线性表的顺序表示(C/C++语言实现)

线性表的顺序表示(C/C++语言实现)

顺序表示线性表的查找、删除、插入的时间复杂度都为O(n)
这里实现了线性表的初始化、插入、删除、查找等基本操作。

#include <iostream>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
using namespace std;
typedef int Status;//返回值类型
typedef char ElemType;//状态
typedef struct
{
    ElemType elem[MAXSIZE];//对应方式二初始化
    //ElemType* elem;对应方式一初始化
    int length;
}SqList;
Status InitList2(SqList* &L){//改变形参中的内容并且使用它则需要用引用,解析:https://blog.csdn.net/eye123456789/article/details/79794461
    //L.elem = (ElemType *)malloc(MAXSIZE * sizeof(ElemType));
    L=(SqList*)malloc(sizeof(SqList));//(SqList *)表示把这个指针转型为SqlList结构的指针
    //L=new SqList;也可以实现
    if(!L->elem){exit(OVERFLOW);}
    L->length=0;
    return OK;
}
//Status InitList(SqList &L){
    //L.elem = (ElemType *)malloc(MAXSIZE * sizeof(ElemType));
    //L.elem=new ElemType[MAXSIZE];
    //开辟一个存储空间,并把这块存储空间的基地址赋值给elem
   // if(!L.elem){exit(OVERFLOW);}
    //L.length=0;
   // return OK;
//}
void DestoryList(SqList* &L){
    //注意malloc用free,new用delete
    if(L) free(L);
        //delete L;

}
void ClearList(SqList* &L){
    L->length=0;
}
int GetLength(SqList* L){
    return L->length;
}
int IsEmpty(SqList* L){
    if(L->length==0) return 1;
    else return 0;
}
//取线性表中i号元素
int GetElem(SqList* L,int i,ElemType &e){
    if(i<1||i>L->length) return ERROR;
    e=L->elem[i-1];
    return OK;
}
//查找算法
int LocateElem(SqList* L,ElemType &e){
    for(int i=0;i<L->length;i++){
        if(L->elem[i]==e){
            return ++i;
        }
    }
    return 0;

}
Status AddElem(SqList* &L,ElemType e){
    if(L->length==MAXSIZE) return ERROR;
    L->elem[L->length++]=e;
    return OK;
}

Status ListInsert(SqList* &L,int i,ElemType e){
    if(i<1||i>(L->length+1)){
        return ERROR;
    }
    if(L->length==MAXSIZE) return ERROR;
    for(int j=L->length-1;j>=i-1;j--){
        L->elem[j+1]=L->elem[j];
    }
    L->elem[i-1]=e;
    L->length++;
    return OK;
}

void selectList(SqList* L){
    for(int i=0;i<L->length;i++){
        printf("%c ",L->elem[i]);
    }
}

Status ListDelete(SqList* &L,int i,ElemType &e){
    if(i<1||i>L->length) return ERROR;
    e=L->elem[i-1];
    for(int j=i-1;j<L->length;j++){
        L->elem[j]=L->elem[j+1];
    }
    L->length--;
    return OK;
}

int main()
{
    SqList* L;
    InitList2(L);
    //printf("长度为:%d",L->length);
    ElemType e='a';
    AddElem(L,e);
    AddElem(L,'c');
    AddElem(L,'d');
    selectList(L);
    printf("\n");
    ListInsert(L,2,'b');
    ElemType r;
    ListDelete(L,4,r);
    selectList(L);
    printf("\n被删除元素为:%c",r);
    //ElemType r;
    //GetElem(L,1,r);
    //printf("a:%c\n",r);
    //printf("%d",LocateElem(L,e));
    //printf("%d",IsEmpty(L));

    return 0;
}

线性表顺序实现回顾结束~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜菜iwi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值