数据结构 顺序表的基本操作

参考:
数据结构入门----顺序表的基本操作
顺序表及其基本操作的实现
顺序表----12个基本操作实现
我的代码:

//实验二、顺序表的基本操作
#include<iostream>
#include<cstdlib>
using namespace std;

#define InitSize 100

typedef struct
{
    
    
    int *data;
    int MaxSize;
    int length;
} SeqList;

//1.初始化顺序表
int InitList( SeqList &L )
{
    
    
    L.data = (int *) malloc (sizeof (int) * InitSize);

    if( !L.data )
    {
    
    
        return 0;
    }
    L.length = 0;
    L.MaxSize = InitSize;
    return 1;
}

//2.顺序表的销毁
void DestroyList( SeqList &L )
{
    
    
    if( L.data )
    {
    
    
        free( L.data );
    }
    L.length = 0;
}

//3.清空顺序表
bool ClearList( SeqList &L )
{
    
    
    L.length = 0;
    cout << "线性表清空成功!" << endl;
    return true;
}

//4.顺序表的判空
bool Empty( SeqList L )
{
    
    
    if( L.length == 0 )
    {
    
    
        return true;
    }
    else
    {
    
    
        return false;
    }
}

//5.顺序表的长度
int Length( SeqList L )
{
    
    
    return L.length;
}

//6.顺序表的按位查找
bool GetElem( SeqList L, int i )
{
    
    
    //判断查找位置是否合法
    if ( i<1 || i > L.length )
    {
    
    
        cout << "查找位置不合法!" << endl;
        return false;
    }

    cout << "该位置上的元素是:" <<  L.data[i - 1] << endl;

    return false;
}

//7.顺序表的按值查找
bool LocateElem( SeqList L, int e )
{
    
    
    for( int i=0; i<L.length; i++ )
    {
    
    
        if( L.data[i] == e )
        {
    
    
            cout << "该元素的位置是:" <<  i+1 << endl;
            return true;
        }
    }
    cout << "输入元素不存在!" << endl;
    return false;
}

//8.返回前驱
int PriorElem( SeqList L, int cur_e )
{
    
    

    int *pt = L.data;
    if( cur_e == 1 )
    {
    
    
        cout << "该位置上的元素是" << L.data[cur_e-1] << ",它没有前驱!" << endl;
        return false;
    }
    else if ( cur_e > L.length )
    {
    
    
        cout << "该位置不合法!" << endl;
        return false;
    }
    else
    {
    
    
        int i=1;
        do
        {
    
    
            i++;
            pt++;

        }
        while( i == cur_e-1 );

        cout << "该位置上的元素是" << L.data[cur_e-1] << ",它的前驱是:" <<  *(--pt) << endl;
        return true;
    }
}

//9.返回后继
bool NextElem( SeqList L, int cur_e )
{
    
    


    int *pt = L.data;

    if( cur_e == L.length )
    {
    
    
        cout << "该位置上的元素是" << L.data[cur_e-1] << ",它没有后继!" << endl;
        return false;
    }
    else if ( cur_e > L.length )
    {
    
    
        cout << "该位置不合法!" << endl;
        return false;
    }
    else
    {
    
    
        int i=0;
        do
        {
    
    
            i++;
            pt++;

        }
        while( i == cur_e-1 );

        cout << "该位置上的元素是" << L.data[cur_e-1] << ",它的后继是:" <<  *(pt) << endl;
        return true;
    }
}


//10.插入操作
bool ListInsert( SeqList &L, int i, int e )
{
    
    
    //判断插入位置是否合法
    if ( i<1 || i > L.length+1 )
    {
    
    
        cout << "插入位置不合法!" << endl;
        return false;
    }

    //判断当前存储空间是否已满
    if( L.length == L.MaxSize )
    {
    
    
        cout << "当前存储空间已满!无法插入!" << endl;
        return false;
    }

    //将该插入位置之后的元素向后移动一位
    for( int j=L.length; j>=i; j-- )
    {
    
    
        L.data[j] = L.data[j - 1];
    }
    //插入新元素
    L.data[i - 1] = e;
    L.length++;
    return true;
}

//11.顺序表删除元素
bool ListDelete( SeqList &L, int i )
{
    
    
    //判断位置是否合法
    if( i<1 || i>L.length )
    {
    
    
        cout << "删除位置不合法!" << endl;
        return false;
    }

    int j;
    for( j = i-1; j<L.length; j++ )
    {
    
    
        L.data[j] = L.data[j+1];
    }
    L.length--;
    i--;
    cout << "删除元素成功!" << endl;
    return true;

}

//12.顺序表的打印
void PrintList( SeqList L )
{
    
    
    if( Empty(L) )
    {
    
    
        cout << "顺序表为空!" << endl;
        return ;
    }

    for( int i=0; i<L.length; i++ )
    {
    
    
        cout << L.data[i] << ' ';
    }
    cout << endl;
}

//格式化输出菜单
void xianshi( int i, string text )
{
    
    
    if( i < 10 )
    {
    
    
        cout << i << "----" << text << endl;
    }
    if( i >= 10 )
    {
    
    
        cout << i << "---" << text << endl;
    }
}

int main()
{
    
    
    int n, i=1, place_i;
    SeqList a[1000];

    xianshi(1,"初始化一个线性表");
    xianshi(2,"销毁线性表");
    xianshi(3,"清空线性表");
    xianshi(4,"判断线性表是否为空");
    xianshi(5,"求取线性表长度");
    xianshi(6,"获取线性表中指定位置的元素");
    xianshi(7,"获取线性表元素的位置");
    xianshi(8,"求前驱");
    xianshi(9,"求后继");
    xianshi(10,"在线性表指定位置插入元素");
    xianshi(11,"删除线性表指定位置的元素");
    xianshi(12,"显示线性表");
    cout<<"    退出输入一个负数"<<endl;
    cout<<"请输入操作代码:"<<endl;

    //定义一个波尔函数,用来控制未初始化一个线性表无法执行操作
    bool continuedo=false;

    do
    {
    
    
        cin >> n;
        if( n < 0 )
        {
    
    
            return 0;
        }
        else
        {
    
    
            switch( n )
            {
    
    
            case 1 :
            {
    
    
                if( InitList( a[i] ) )
                {
    
    
                    cout<<"初始化线性表成功!线性表序号为:"<<i<<endl;
                    place_i = i;
                    i++;
                    continuedo = true;
                }
                else
                {
    
    
                    cout << "线性表初始化失败!" << endl;
                }
                break;
            }
            case 2 :
            {
    
    
                if( continuedo == true )
                {
    
    
                    int m=0;
                    cout << "请输入要销毁的线性表的序号:" << endl;
                    cin >> m;
                    if( m > place_i )
                    {
    
    
                        cout << "要销毁的线性表不存在!" << endl;
                    }
                    else
                    {
    
    
                        DestroyList( a[m] );
                        cout << "线性表" << m << "销毁成功!" << endl;
                    }
                }
                else
                {
    
    
                    cout << "线性表未初始化,无法销毁!" << endl;
                }
                break;
            }
            case 3 :
            {
    
    
                if( continuedo == true )
                {
    
    
                    int m=0;

                    cout << "请输入要清空的线性表序号:" << endl;
                    cin >> m;

                    if( m > place_i )
                    {
    
    
                        cout << "线性表不存在!" << endl;
                    }
                    else
                    {
    
    
                        ClearList( a[m] );
                    }
                }
                else
                {
    
    
                    cout << "线性表未初始化,无法清空!" << endl;
                }
                break;
            }
            case 4 :
            {
    
    
                if( continuedo == true )
                {
    
    
                    int m=0;

                    cout << "请输入要判空的线性表序号:" << endl;
                    cin >> m;

                    if( m > place_i )
                    {
    
    
                        cout << "线性表不存在!" << endl;
                    }
                    else
                    {
    
    
                        if( Empty( a[m] ))
                        {
    
    
                            cout << "线性表为空!" << endl;
                        }
                        else
                        {
    
    
                            cout << "线性表非空!" << endl;
                        }
                    }
                }
                else
                {
    
    
                    cout << "线性表未初始化,无法判空!" << endl;
                }
                break;
            }
            case 5 :
            {
    
    
                if(continuedo==true)
                {
    
    
                    cout<<"请输入要测量的线性表的序号:"<<endl;
                    int m=0;
                    cin>>m;
                    if(m>place_i)
                    {
    
    
                        cout<<"线性表不存在!"<<endl;
                    }
                    else
                    {
    
    
                        cout<<Length(a[m])<<endl;
                    }
                }
                else
                {
    
    
                    cout<<"线性表未初始化,无法测量长度!"<<endl;
                }
                break;
            }
            case 6 :
            {
    
    
                if( continuedo == true )
                {
    
    
                    cout << "请输入要查找的线性表的序号:" << endl;
                    int m=0;
                    cin >> m;

                    if( m> place_i )
                    {
    
    
                        cout << "线性表不存在!" << endl;
                    }
                    else
                    {
    
    
                        int q=0;
                        cout << "请输入要查找的元素位置:" << endl;
                        cin >> q;
                        GetElem(a[m], q);
                    }
                }
                else
                {
    
    
                    cout<<"线性表未初始化,无法查找!"<<endl;
                }
                break;
            }

            case 7 :
            {
    
    
                if( continuedo == true )
                {
    
    
                    cout << "请输入要查找的线性表的序号:" << endl;
                    int m=0;
                    cin >> m;

                    if( m> place_i )
                    {
    
    
                        cout << "线性表不存在!" << endl;
                    }
                    else
                    {
    
    
                        int q=0;
                        cout << "请输入要查找的元素:" << endl;
                        cin >> q;
                        LocateElem(a[m], q);
                    }
                }
                else
                {
    
    
                    cout<<"线性表未初始化,无法查找!"<<endl;
                }
                break;
            }
            case 8 :
            {
    
    
                if( continuedo == true )
                {
    
    
                    cout << "请输入要查找的线性表的序号:" << endl;
                    int m=0;
                    cin >> m;

                    if( m> place_i )
                    {
    
    
                        cout << "线性表不存在!" << endl;
                    }
                    else
                    {
    
    
                        int q=0;
                        cout << "请输入要查找的位置:" << endl;
                        cin >> q;
                        PriorElem(a[m], q);
                    }
                }
                else
                {
    
    
                    cout<<"线性表未初始化,无法查找!"<<endl;
                }
                break;
            }
            case 9 :
            {
    
    
                if( continuedo == true )
                {
    
    
                    cout << "请输入要查找的线性表的序号:" << endl;
                    int m=0;
                    cin >> m;

                    if( m> place_i )
                    {
    
    
                        cout << "线性表不存在!" << endl;
                    }
                    else
                    {
    
    
                        int q=0;
                        cout << "请输入要查找的位置:" << endl;
                        cin >> q;
                        NextElem(a[m], q);
                    }
                }
                else
                {
    
    
                    cout<<"线性表未初始化,无法查找!"<<endl;
                }
                break;
            }
            case 10 :
            {
    
    
                if( continuedo == true )
                {
    
    
                    cout << "请输入要插入的线性表的序号:" << endl;
                    int m=0;
                    cin >> m;

                    if( m> place_i )
                    {
    
    
                        cout << "线性表不存在!" << endl;
                    }
                    else
                    {
    
    
                        int q=0;
                        cout << "请输入要插入的位置:" << endl;
                        cin >> q;
                        int p=0;
                        cout << "请输入要插入的元素:" << endl;
                        cin >> p;
                        ListInsert(a[m], q, p);
                    }
                }
                else
                {
    
    
                    cout<<"线性表未初始化,无法插入!"<<endl;
                }
                break;
            }
            case 11 :
            {
    
    
                if( continuedo == true )
                {
    
    
                    cout << "请输入要删除的线性表的序号:" << endl;
                    int m=0;
                    cin >> m;

                    if( m> place_i )
                    {
    
    
                        cout << "线性表不存在!" << endl;
                    }
                    else
                    {
    
    
                        int q=0;
                        cout << "请输入要删除的位置:" << endl;
                        cin >> q;

                        ListDelete(a[m], q);
                    }
                }
                else
                {
    
    
                    cout<<"线性表未初始化,无法删除!"<<endl;
                }
                break;
            }
            case 12 :
            {
    
    
                if( continuedo == true )
                {
    
    
                    cout << "请输入要打印的线性表的序号:" << endl;
                    int m=0;
                    cin >> m;

                    if( m> place_i )
                    {
    
    
                        cout << "线性表不存在!" << endl;
                    }
                    else
                    {
    
    
                        PrintList(a[m]);
                    }
                }
                else
                {
    
    
                    cout<<"线性表未初始化,无法打印!"<<endl;
                }
                break;
            }
            default:
                cout << "操作码输入有误!" << endl;
                break;
            }
        }
    }
    while( 1 );

    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_46161051/article/details/115270628