数据结构中线性表的基本操作,C++实现

数据对象在计算机中的存储表示称为数据的存储结构,也称为物理结构。把数据对象存储到计算机时,通常要求既要存储各数据元素的数据,又要存储数据元素之间的逻辑关系,数据元素在计算机内用一个结点来表示。数据元素在计算机中有两种基本的存储结构,分别是顺序存储结构和链式存储结构。
这里给出的是顺序表的实现。
首先要构造一个如下结构体类型:

struct Sqlist
{
    long *elem;    //存放结点
    int Length;    //表长度
    int listsize;  //最大长度
};

对顺序表进行初始化操作:

void Initlist(Sqlist &L)//初始化
{
    L.elem=new long[List_Init_Size];//动态分配内存
    if(!L.elem)    //如果动态分配内存失败,则进行下面操作
        exit("Overflow!");
    L.Length=0;
    L.listsize=List_Init_Size;
}

创建顺序表:

void Create(Sqlist &L)//创建顺序表
{
    int i,num;
    cout<<"请输入顺序表元素个数:";
    cin>>num;
    for(i=0;i<num;i++)
    {
        cin>>L.elem[i];
        L.Length++;
    }
    cout<<"创建顺序表成功!"<<endl;
}

求直接前驱:

int PriorElem(Sqlist L,long pur_e,long pre_e)//求前驱
{
    long *p,*q;    //申请两个新结点,用于下面操作
    int j=1;
    for(int i=1;i<L.Length;i++)  //遍历整个顺序表找到要求前驱的元素,为下面求前驱做准备
    {
        if(pur_e==L.elem[i])   
        {
            p=&L.elem[i];   //将该结点的地址赋给p,也可将该方法参数改为引用传递则可省去&
            break;
        }
        else
            j++;
    }
    if(j==L.Length)  //顺序表中无该元素
    {
        cout<<"你输入的元素不合法,请重新输入!"<<endl;
        cin>>pur_e;
        PriorElem(L,pur_e,pre_e);
    }
    else if(L.Length==0)  //顺序表为空
    {
        cout<<"线性表中无数据!"<<endl;
    }
    else
    {
        q=p-1;
        pre_e=*q;
        return pre_e;
    }
}

同理可求直接后继:

int NextElem(Sqlist L,long pur_e,long next_e)//求后继
{
    long *p,*q;
    int j=1;
    for(int i=0;i<L.Length-1;i++)
    {
        if(pur_e==L.elem[i])
        {
            p=&L.elem[i];
            break;
        }
        else
            j++;
    }
    if(j==L.Length)
    {
        cout<<"你输入的元素不合法,请重新输入!"<<endl;
        cin>>pur_e;
        NextElem(L,pur_e,next_e);
    }
    else if(L.Length==0)
    {
        cout<<"线性表中无数据!"<<endl;
    }
    else
    {
        q=p+1;
        next_e=*q;
        return next_e;
    }

}

插入:

int InsertElem(Sqlist&L,int i,long e)//插入
{
    if(i<1 || i>L.Length+1)
        return error;//位置不合法返回-1
    if(L.Length==L.listsize)
        return error;//超出表长返回-1
    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 right;//成功插入返回1
}

删除:

int DeleteElem(Sqlist &L,int i)//删除
{
    if(!L.elem)
        return error;//未分配好返回-1
    else if(i<1 || i>L.Length)
        return error;//位置不合法返回-1
    else
    {
        for(int j=i;j<=L.Length-1;j++)
            {
                L.elem[j-1]=L.elem[j];
            }
            --L.Length;
            return right;//成功删除返回1
    }
}

判断表是否为空

void EmptyList(Sqlist L)//判断表是否为空
{
    if(L.Length==0)
        cout<<"线性表为空"<<endl;
    else
        cout<<"线性表非空"<<endl;
}
int LengthList(Sqlist L)//求表长
{
    return L.Length;
}
void TraverList(Sqlist L)//遍历
{
    for(int i=0;i<L.Length;i++)
    {
        if(i==L.Length-1)
            cout<<L.elem[i]<<endl;
        else
            cout<<L.elem[i]<<",";
    }
}

遍历顺序表中元素:

void TraverList(Sqlist L)//遍历
{
    for(int i=0;i<L.Length;i++)
    {
        if(i==L.Length-1)
            cout<<L.elem[i]<<endl;
        else
            cout<<L.elem[i]<<",";
    }
}

还有一些其他的小操作这里不再赘述
下面是完整代码:

#include <iostream>
#include <stdio.h>
#define List_Init_Size 50
#define error -1
#define right 1
using namespace std;

struct Sqlist
{
    long *elem;
    int Length;
    int listsize;
};
void exit(string s)
{
    cout<<s<<endl;
}
void Initlist(Sqlist &L)//初始化
{
    L.elem=new long[List_Init_Size];
    if(!L.elem)
        exit("Overflow!");
    L.Length=0;
    L.listsize=List_Init_Size;
}
void Create(Sqlist &L)//创建顺序表
{
    int i,num;
    cout<<"请输入顺序表元素个数:";
    cin>>num;
    for(i=0;i<num;i++)
    {
        cin>>L.elem[i];
        L.Length++;
    }
    cout<<"创建顺序表成功!"<<endl;
}
void ClearList(Sqlist &L)//清空顺序表
{
    L.Length=0;
    cout<<"已清空线性表"<<endl;
}
void EmptyList(Sqlist L)//判断表是否为空
{
    if(L.Length==0)
        cout<<"线性表为空"<<endl;
    else
        cout<<"线性表非空"<<endl;
}
int LengthList(Sqlist L)//求表长
{
    return L.Length;
}
void TraverList(Sqlist L)//遍历
{
    for(int i=0;i<L.Length;i++)
    {
        if(i==L.Length-1)
            cout<<L.elem[i]<<endl;
        else
            cout<<L.elem[i]<<",";
    }
}
void GetElem(Sqlist L,int i)//取元素
{
    if(i<1 || i>L.Length)
    {
         cout<<"位置不合法,请重新输入!"<<endl;
         cin>>i;
         GetElem(L,i);
    }
    else
        cout<<L.elem[i-1]<<endl;
}
int LocateElem(Sqlist L,long e)//定位
{
    int j=1;
    for(int i=0;i<L.Length;i++)
    {
        if(e==L.elem[i])
        {
            return i+1;
            break;//返回从开始遍历到的第一个该元素位置
        }
        else
            j++;
    }
    if(j==L.Length)
        return error;//不存在则返回-1
}
int PriorElem(Sqlist L,long pur_e,long pre_e)//求前驱
{
    long *p,*q;
    int j=1;
    for(int i=1;i<L.Length;i++)
    {
        if(pur_e==L.elem[i])
        {
            p=&L.elem[i];
            break;
        }
        else
            j++;
    }
    if(j==L.Length)
    {
        cout<<"你输入的元素不合法,请重新输入!"<<endl;
        cin>>pur_e;
        PriorElem(L,pur_e,pre_e);
    }
    else if(L.Length==0)
    {
        cout<<"线性表中无数据!"<<endl;
    }
    else
    {
        q=p-1;
        pre_e=*q;
        return pre_e;
    }
}
int NextElem(Sqlist L,long pur_e,long next_e)//求后继
{
    long *p,*q;
    int j=1;
    for(int i=0;i<L.Length-1;i++)
    {
        if(pur_e==L.elem[i])
        {
            p=&L.elem[i];
            break;
        }
        else
            j++;
    }
    if(j==L.Length)
    {
        cout<<"你输入的元素不合法,请重新输入!"<<endl;
        cin>>pur_e;
        NextElem(L,pur_e,next_e);
    }
    else if(L.Length==0)
    {
        cout<<"线性表中无数据!"<<endl;
    }
    else
    {
        q=p+1;
        next_e=*q;
        return next_e;
    }
}
int InsertElem(Sqlist&L,int i,long e)//插入
{
    if(i<1 || i>L.Length+1)
        return error;//位置不合法返回-1
    if(L.Length==L.listsize)
        return error;//超出表长返回-1
    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 right;//成功插入返回1
}
int DeleteElem(Sqlist &L,int i)//删除
{
    if(!L.elem)
        return error;//未分配好返回-1
    else if(i<1 || i>L.Length)
        return error;//位置不合法返回-1
    else
    {
        for(int j=i;j<=L.Length-1;j++)
            {
                L.elem[j-1]=L.elem[j];
            }
            --L.Length;
            return right;//成功删除返回1
    }
}
int main()
{
    Sqlist L;
    Initlist(L);//初始化线性表
    int i,n;
    bool b=true;
    long e1,e2;
    while(b)
    {
        cout<<"      0、创建线性表       "<<endl;
        cout<<"      1、清空线性表       "<<endl;
        cout<<"      2、判断线性表是否为空       "<<endl;
        cout<<"      3、求线性表长度       "<<endl;
        cout<<"      4、获取线性表指定位置元素      "<<endl;
        cout<<"      5、求前驱      "<<endl;
        cout<<"      6、求后继       "<<endl;
        cout<<"      7、在线性表指定位置插入元素      "<<endl;
        cout<<"      8、删除线性表指定位置元素       "<<endl;
        cout<<"      9、显示线性表       "<<endl;
        cout<<"      10、定位线性表中某元素位置       "<<endl;
        cout<<"          退出,输入非上述数字!       "<<endl;
        cout<<"请输入操作代码:";
        cin>>n;
        switch(n)
        {
            case 0:Create(L);break;
            case 1:ClearList(L);break;
            case 2:EmptyList(L);break;
            case 3:cout<<"线性表长度为:"<<LengthList(L)<<endl;break;
            case 4:cout<<"请输入要获取元素的位置:";
                   cin>>i;
                   GetElem(L,i);break;
            case 5:cout<<"请输入要求直接前驱的元素:";
                   cin>>e1;
                   cout<<"其前驱为:"<<PriorElem(L,e1,e2)<<endl;break;
            case 6:cout<<"请输入要求直接后继的元素:";
                   cin>>e1;
                   cout<<"其后继为:"<<NextElem(L,e1,e2)<<endl;break;
            case 7:cout<<"请输入要插入元素的位置和插入元素:";
                   cin>>i>>e1;
                   cout<<InsertElem(L,i,e1)<<endl;break;
            case 8:cout<<"请输入要删除元素的位置:";
                   cin>>i;
                   cout<<DeleteElem(L,i)<<endl;break;
            case 9:cout<<"线性表中的元素为:"<<endl;
                   TraverList(L);break;
            case 10:cout<<"请输入要查找位置的元素:";
                    cin>>e1;
                    cout<<"其位置为:"<<LocateElem(L,e1)<<endl;break;
            default :b=false;delete []L.elem;break;//用以退出循环
        }
    }
    return 0;
}

下面是程序运行初始界面截图:
在这里插入图片描述如有错误欢迎指正!

  • 10
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值