数据结构——顺序表

近4个小时写的数据结构,用c++实现的顺序表,包括一些基础操作,创建,插入,删除,清空,查找 等;谨以此文来记录我的学习,以便日后改进,加油!

AC代码:

#include<iostream>
#define Maxsize 100
#define ElemType int
#define Status int
#define OK 1
using namespace std;
// 顺序表结构;
typedef struct
{
    ElemType *elem;
    int length;
}SqList;

// *******************************基本操作函数*********************************//
// 顺序表初始化函数,构造一个空的顺序表;
Status InitList(SqList &L)
{
    L.elem=new ElemType[Maxsize];
    if(!L.elem)
        cout<<"空间不足,无法创建!!!"<<endl;
    L.length=0;
    return OK;
}
// 创建顺序表函数,初始化前n个数据;
bool CreateList(SqList &L, int n)
{
    if(n<0 || n>Maxsize)
        return false;
    for(int i=0; i<n; i++)
    {
        cin>>L.elem[i];
        L.length++;
    }
    return true;
}
// 插入函数 位置 i 插入数据 i 及之后元素后移 ,1=<i<=length+1;
bool InsertList(SqList &L, int i, ElemType e)
{
    if(i<1 || i>L.length+1)  // 判断位置是否有效;
    {
        cout<<"非法位置!!!"<<endl;
        return false;
    }
    if(L.length>=Maxsize)  // 判断存储空间是否已满;
    {
        cout<<"此顺序表位置已满,无法插入!"<<endl;
        return false;
    }
    for(int j=L.length; j>=i; j--)  //  位置 i 及之后元素后移;
    {
        L.elem[j]=L.elem[j-1];
    }
    L.elem[i-1]=e;
    ++L.length;
    return true;
}
//  删除函数 删除位置 i 的元素,i 之后的元素依次前移;
bool DeleteList(SqList &L, int i)
{
    if(i<1 || i>L.length)
    {
        cout<<"非法位置!!!"<<endl;
        return false;
    }
    for(int j=i; j<=L.length-1; j++)  //  位置 i 之后的元素依次前移;
    {
        L.elem[j-1]=L.elem[j];
    }
    --L.length;
    return true;
}
//  查找函数 按位置从小到大查找第一个值等于 e 的元素,并返回其位置;
int LocateElem(SqList L, ElemType e)
{
    for(int i=0; i<L.length; i++)
    {
        if(L.elem[i]==e)
        {
            return i+1;
        }
    }
    return 0;
}
//  清空顺序表;
void ClearList(SqList &L)
{
    L.length=0;
}

//*****************************功能函数******************************************//
// 输出顺序表功能函数  按位置从小到大输出顺序表所有元素;
void PrintList(SqList L)
{
    cout<<"当前顺序表所有元素为: ";
    for(int i=0; i<L.length; i++)
    {
        cout<<L.elem[i]<<" ";
    }
    cout<<endl;
}
// 创建顺序表功能函数;
void Create(SqList &L)
{
    int n;
    bool flag;
    L.length=0;
    cout<<"请输入你想要创建顺序表的长度(>1):";
    cin>>n;
    cout<<"请输入"<<n<<"个元素(空格隔开):";
    flag=CreateList(L, n);
    if(flag)
    {
        cout<<"创建成功!";
        PrintList(L);
    }
    else
        cout<<"输入长度非法!!!"<<endl;
}
//  插入功能函数  调用InsertList完成插入,调用PrintList显示结果;
void Insert(SqList &L)
{
    int place;
    ElemType e;
    bool flag;
    cout<<"请输入要插入的位置(从1开始)及元素:";
    cin>>place>>e;
    flag=InsertList(L, place, e);
    if(flag)
    {
        cout<<"插入成功!"<<endl;
        PrintList(L);
    }
}
//  删除功能函数  调用DeleteList完成删除,调用PrintList显示结果;
void Delete(SqList &L)
{
    int place;
    bool flag;
    cout<<"请输入要删除的位置(从1开始):";
    cin>>place;
    flag=DeleteList(L, place);
    if(flag)
    {
        cout<<"删除成功!"<<endl;
        PrintList(L);
    }
}
//  查找功能函数  调用LocateElem查找函数;
void Search(SqList L)
{
    ElemType e;
    int flag;
    cout<<"请输入要查找的值:";
    cin>>e;
    flag=LocateElem(L, e);
    if(flag)
    {
        cout<<"该元素位置为:"<<flag<<endl;
    }
    else
        cout<<"未找到该元素!"<<endl;
}
//  菜单
void menu()
{
    cout<<"********1.创建                    2.插入********"<<endl;
    cout<<"********3.删除                    4.查找********"<<endl;
    cout<<"********5.倒置                    6.分奇偶排序**"<<endl;
    cout<<"********7.输出                    8.清空********"<<endl;
    cout<<"********9.退出                          ********"<<endl;
}

int main()
{
    SqList L;
    int choice;
    InitList(L);
    while(1)
    {
        menu();
        cout<<"请输入菜单序号:";
        cin>>choice;
        if(9==choice)
            break;
        switch(choice)
        {
            case 1: Create(L);  break;
            case 2: Insert(L);   break;
            case 3: Delete(L);  break;
            case 4: Search(L); break;
       //     case 5: Reverse(L); break;
       //     case 6: SplitSort(L); break;
            case 7: PrintList(L); break;
            case 8:ClearList(L); break;
            default: cout<<"输入错误!!!"<<endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Gofor.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值