数据结构(1):线性表的顺序表示和实现

// 线性表的顺序表示和实现
#include <iostream>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
// Status是函数返回值类型,其值是函数结果状态代码
typedef int Status;
// ElemType是自定义数据类型
typedef int ElemType;
// 循环变量
int i,j; 
using namespace std;

//顺序表的存储结构
typedef struct
{
    ElemType *elem; // 存储空间的基地址
    int length;     // 当前长度
}SqList;            // 顺序表的结构类型为SqList

// 顺序表的初始化
Status InitList(SqList &L)
{   // 构造一个空的顺序表L
    // 为顺序表分配一个大小为MAXSIZE的数组空间
    L.elem = new ElemType[MAXSIZE]; 
    if(!L.elem) exit(OVERFLOW);     // 存储分配失败退出
    L.length = 9;
    return OK;
}

// 顺序表的取值
Status GetElem(SqList L,int i,ElemType &e)
{
    // 判断i值是否合理,若不合理,返回ERROR
    if(i<1 || i>L.length) return ERROR; 
    e = L.elem[i-1];            // L.elem[i-1]存储第i个数据元素
    return OK;  
}

// 顺序表的查找
int LocateElem(SqList L,ElemType e)
{   // 在顺序表L中查找值为e的数据元素,返回其序号
    for(i=0; i < L.length; ++i)
        if(L.elem[i] == e) return i+1;  // 查找成功,返回序号i+1
    return ERROR;                       // 查找失败,返回0
}

// 顺序表的插入
Status ListInsert(SqList &L,int i,ElemType e)
{   // 在顺序表L中第i个位置之前插入新的元素e,i值的合法范围是
    //  1<=i<=L.length+1
    if(i<1 || i>L.length+1) return ERROR;   // i值不合法
    if(L.length == MAXSIZE) return ERROR;   // 当前存储空间已满
    for(j=L.length; j >= i-1; --j)
        L.elem[j+1] = L.elem[j];    // 插入位置及之后的元素后移         
    L.elem[i-1] = e;                // 将新的元素e放入第i个位置
    ++L.length;                             // 表长加1
    return OK;
}

// 顺序表的删除
Status ListDelete(SqList &L,int i)
{   // 在顺序表L中删除第i个元素,i值的合法范围是1<=i<=L.length
    if(i<1 || i>L.length) return ERROR; // i值不合法
    for(j=i; j <= L.length-1; --j)
        L.elem[j-1] = L.elem[j];    // 被删除元素之后的元素前移
    --L.length;                         // 表长减1
    return OK;
}

int main()
{
    /***************************************************/
}

优化版:

#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100

typedef int Status;
typedef int ElemType;

typedef struct
{
    ElemType *elem;
    int length;
}SqList; 

// 1、初始化
Status InitList(SqList &L)
{
    L.elem = new ElemType[MAXSIZE];
    if(!L.elem) exit(OVERFLOW);
    L.length = 0;
    return OK;
}

// 2、取值
Status GetElem(SqList L, int i, ElemType &e)
{
    if(i-1<0 || i>L.length) return ERROR;
    e = L.elem[i-1];
    return OK;
}

// 3、查找
int LocateElem(SqList L, ElemType e)
{
    for(int i=0; i < L.length; ++i)
        if(L.elem[i] == e)
            return i+1;
    return ERROR;
}

// 4、插入
Status ListInsert(SqList &L, int i, ElemType e)
{
    if(i-1<0 || 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;
}     

// 5、删除
Status ListDelete(SqList &L, int i)
{
    if(i-1<0 || i>L.length) return ERROR;
    for(int j=i-1; j < L.length-1; ++j)
        L.elem[j] = L.elem[j+1];
    --L.length;
    return OK;
}

int main()
{
    cout<<"************************************"<<endl;
    cout<<"*      1、初始化                   *"<<endl;
    cout<<"*      2、取值                     *"<<endl;
    cout<<"*      3、查找                     *"<<endl;
    cout<<"*      4、插入                     *"<<endl;
    cout<<"*      5、删除                     *"<<endl;
    cout<<"*      6、打印顺序表               *"<<endl;
    cout<<"*      7、退出                     *"<<endl;
    cout<<"************************************"<<endl;

    SqList L;
    ElemType e;
    int n,m,i,choice;
    while(1)
    {
        cout<<"请输入你的选择: ";
        cin>>choice;
        switch(choice)
        {
            case 1:
                if(InitList(L)) cout<<"初始化成功!"<<endl<<endl;
                else cout<<"初始化失败!"<<endl<<endl;
                continue;
            case 2:
                cout<<"请输入你要取的值的序号: ";
                cin>>n;
                if(GetElem(L,n,e)) cout<<"取值成功!"<<endl<<"得到的值为: "<<e<<endl<<endl;
                else cout<<"取值失败!你输入的序号有误,请重新选择。"<<endl<<endl;
                continue;
            case 3:
                cout<<"请输入要查找的数: ";
                cin>>e;
                if(LocateElem(L,e)) cout<<"数 "<<e<<" 在第 "<<LocateElem(L,e)<<" 位."<<endl<<endl;
                else cout<<"数 "<<e<<" 查找失败!请确认你输入的数字存在于表中. 请重新操作。"<<endl<<endl;
                continue;
            case 4:
                cout<<"请输入要插入的数的个数( <"<<MAXSIZE-L.length<<" ): ";
;               cin>>n;
                if(n > MAXSIZE-L.length) 
                {
                    cout<<"输入异常,请重新操作."<<endl<<endl;
                    continue;
                }
                for(i=0; i<n;++i)
                {
                    cout<<endl<<"请输入要插入的位置: ";
                    cin>>m;
                    cout<<"请输入要插入的数字: ";
                    cin>>e;
                    if(ListInsert(L,m,e)) cout<<"数 "<<e<<" 插入成功!"<<endl<<endl;
                    else { cout<<"数字 "<<e<<" 插入失败!请重新输入."<<endl<<endl; i++; }
                }
                continue;
            case 5:
                cout<<"请输入要删除的数的位置: ";
                cin>>m;
                if(ListDelete(L,m)) cout<<" 删除成功!"<<endl<<endl;
                else cout<<"请重新操作."<<endl<<endl;
                continue;
            case 6:
                for(i=0;i<L.length;++i)
                {
                    cout<<L.elem[i]<<" ";
                }
                cout<<endl;
                continue;
            case 7:
                cout<<"正在退出..."<<endl;
                for(i=0;i<99999999;++i){}
                cout<<"程序已经结束.欢迎下次再使用0.0"<<endl;
                break;
            default:
                for(i=0;i<99999999;++i){}
                cout<<"程序异常退出! "<<endl;
                break;
        }
        break;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值