1. 程式人生 > >1.線性表的順序儲存結構————順序表(包含C語言和C++版本的完整程式)

1.線性表的順序儲存結構————順序表(包含C語言和C++版本的完整程式)

1.順序表的定義

   將表中元素一個接一個的存入一組連續的儲存單元中,這種儲存結構是順序結構,採用順序儲存結構的線性表簡稱為“ 順序表”。
   順序表的儲存特點是:只要確定了起始位置,表中任一元素的地址都通過下列公式得到:

Loc(ai)=Loc(a1)+(i1)L,1in其中,L是元素佔用儲存單元的長度。

2.順序表的基本操作

(1)初始化:建立一個空的順序表;
(2)新建:新建一個順序表;
(3)合併順序表:將兩個順序表合併,並去掉重複元素;
(4)按元素查詢:查詢順序表中是否含有指定元素;
(5)按位置查詢:查詢指定位置的元素;
(6)求順序表的長度:計算順序表的元素個數;
(7)指定位置插入:在指定位置插入元素;
(8)刪除元素:刪除指定位置的元素;
(9)判空:判斷是否是空的順序表;
(10)清空順序表;
(11)顯示:顯示當前順序表的所有資料。

3.順序表的程式設計實現

  • 3.1 C++實現順序表

(1)順序表的標頭檔案 sequencelist.h

#ifndef SEQUENCELIST_H
#define SEQUENCELIST_H

#define MAXSIZE 20//最大儲存容量
typedef int ElemType;

class SqList
{
public:
    SqList();//
    SqList(ElemType elems[],int n);//有參構造器
    ~SqList();//
    bool CreatList();//新建一個順序表
    bool UnionList(SqList L1,SqList L2);
    int
LocateElem(ElemType e);//按元素查詢:成功則返回元素的序號(從1開始),失敗則返回0 int ListLength();//順序表的長度 bool GetElem(int i, ElemType& e);//查詢第i個位置的元素 bool ListInsert(int i,ElemType e);//在第i個位置插入元素 bool ListDelete(int i,ElemType& e);//刪除第i個位置的元素 bool ListEmpty();//判空 void clearList();//清空順序表 void display();//顯示當前的順序表
private: ElemType data[MAXSIZE];//下標從0開始,但是對線性表的操作中的下標從1開始:第1個元素其實就是下標為0的元素 int length; }; #endif // SEQUENCELIST_H

(2)順序表的原始檔 sequencelist.cpp

#include "sequencelist.h"
#include <iostream>
using namespace std;

SqList::SqList()//初始化
{
    length=0;//
}

SqList::SqList(ElemType elems[],int n)//有參構造器
{
    if(n>MAXSIZE)
    {
        cout<<"傳入的順序表長度超出最大範圍,只接收了前"<<MAXSIZE<<"個元素"<<endl;
        length=MAXSIZE;
    }
    else
        length=n;//

    for(int i=0;i<length;i++)
        data[i]=elems[i];
}

SqList::~SqList()
{

}

bool SqList::CreatList()
{

    cout<<"插入多少個元素(0-20)?"<<endl;
    cin>>length;
    if(length<0||length>MAXSIZE)
    {
        length=0;
        return false;
    }
    for(int i=1;i<=length;i++)
    {
//        cout<<"請輸入順序線性表的第"<<i<<"個元素:";
//        cin>>L->data[i-1];
        data[i-1]=i;
    }
    return true;
}

bool SqList::UnionList(SqList L1,SqList L2)
{
    int i,j;
    for(i=0;i<L1.length;i++)
    {
        data[i]=L1.data[i];
    }

    for(j=0;j<L2.length;j++)
        if(L1.LocateElem(L2.data[j])==0)
        {
            if(i>=MAXSIZE)
                return false;
            data[i]=L2.data[j];
            i++;
        }
    length=i;
    return true;
}

int  SqList::LocateElem(ElemType e)//成功則返回元素的序號(從1開始),失敗則返回0
{
    for(int i=0;i<length;i++)
        if(data[i]==e)
            return i+1;
    return 0;
}

int  SqList::ListLength()
{
    return length;
}

bool SqList::GetElem(int i, ElemType& e)
{
    if(length==0 || i<1|| i>length)
        return false;
    e=data[i-1];
    return true;
}

bool SqList::ListInsert(int i,ElemType e)
{
    if(length==MAXSIZE || i<1|| i>length+1)//線性表滿,或者i的範圍不在合理範圍內時返回錯誤
        return false;
    if(i<=length)//不在表尾
    {
        //插入位置的後續元素後移一位
        for(int k=length-1;k>=i-1;k--)
            data[k+1]=data[k];// 倒序挪動位置,避免覆蓋問題
    }
    data[i-1]=e;//插入元素
    length++;
    return true;
}

bool SqList::ListDelete(int i,ElemType& e)
{
    if(length==0 || i<1|| i>length)//線性表滿,或者i的範圍不在合理範圍內時返回錯誤
        return false;
    e=data[i-1];//取出元素
    if(i<=length)//不在表尾
    {
        //插入位置的後續元素前移一位
        for(int k=i-1;k<length-1;k++)
            data[k]=data[k+1];// 倒序挪動位置,避免覆蓋問題
    }
    length--;
    return true;
}

bool SqList::ListEmpty()
{
    if (length==0)
        return true;
    return false;
}

void SqList::clearList()
{
    length=0;
}

void SqList::display()
{
    for(int i=0;i<length;i++)
        cout<<data[i]<<"  ";
    cout<<endl;
}

(3)主函式 main.cpp

#include <iostream>
#include "sequencelist.h"
using namespace std;

int main()
{
    SqList list;
    int num;
    ElemType elem;
    bool flag;

    cout<<"            1.順序表的建立和顯示"<<endl;
    if(!list.CreatList())
        cout<<"順序表建立失敗!"<<endl;
    else
        cout<<"順序表建立成功!    "<<endl;
    //順序表的顯示
    list.display();
    cout<<endl<<endl;

    cout<<"            2.按元素查詢"<<endl;
    num=list.LocateElem(3);
    cout<<"3是順序表的第"<<num<<"個元素"<<endl<<endl<<endl;

    cout<<"            3.按位置查詢"<<endl;
    list.GetElem(4,elem);
    cout<<"順序表的第4個元素是:"<<elem<<endl<<endl<<endl;

    cout<<"            4.順序表的插入"<<endl;
    if(list.ListInsert(2,10))
        cout<<"插入成功!在第2個位置插入10後:    "<<endl;
    else
        cout<<"插入失敗!"<<endl;
    list.display();
    cout<<endl<<endl;

    cout<<"            5.刪除元素"<<endl;
    list.ListDelete(5,elem);
    cout<<"刪掉第5個元素:"<<elem<<endl;
    cout<<"該表的長度為:"<<list.ListLength()<<endl;
    list.display();
    cout<<endl<<endl;

    cout<<"            6.清空順序表"<<endl;
    cout<<"清空順序表前-----";
    if(!list.ListEmpty())
    {
        cout<<"當前順序表不是空表!"<<endl;
        list.clearList();
        cout<<"清空順序表後-----";
        if(list.ListEmpty())
            cout<<"當前順序表是空表!"<<endl;
    }
    cout<<endl<<endl;

    cout<<"            7.合併順序表"<<endl;
    ElemType elems1[8]={0,1,2,3,4,5,6,7};
    ElemType elems2[9]={5,6,7,8,9,10,11,1,12};

    SqList list1={elems1,8};
    SqList list2={elems2,9};
    SqList list3;
    cout<<"合併前的兩個表為:"<<endl;
    list1.display();
    list2.display();
    flag=list3.UnionList(list1,list2);
    if(!flag)
        cout<<"合併後,順序表的長度超過最大範圍"<<endl;
    cout<<"該表的長度為:    "<<list3.ListLength()<<endl;
    list3.display();
    return 0;
}
  • 3.2 C語言實現順序表


    (1)順序表的標頭檔案 sequencelist.h
#ifndef SEQUENCELIST_H
#define SEQUENCELIST_H

#define MAXSIZE 20//最大儲存容量
typedef int ElemType;
typedef struct
{
    ElemType data[MAXSIZE];//下標從0開始,但是對線性表的操作中的下標從1開始:第1個元素其實就是下標為0的元素
    int length;
}SqList;

typedef enum Bool
{
    FALSE,TRUE//列舉預設值從0開始,依次加1
}Bool;

Bool CreatList(SqList* L);//)新建一個順序表
Bool UnionList(SqList* L1,SqList* L2,SqList* L);
int LocateElem(SqList L,ElemType e);//成功則返回元素的序號(從1開始),失敗則返回0
int ListLength(SqList L);//順序表的長度
Bool GetElem(SqList L, int i, ElemType* e);//查詢第i個位置的元素
Bool ListInsert(SqList* L,int i,ElemType e);//在第i個位置插入元素
Bool ListDelete(SqList* L,int i,ElemType* e);//刪除第i個位置的元素
Bool ListEmpty(SqList L);//判空
void clearList(SqList* L);//清空順序表
void display(SqList L);//顯示當前的順序表
#endif // SEQUENCELIST_H

(2)順序表的原始檔 sequencelist.c

#include "sequencelist.h"
#include <stdio.h>

Bool GetElem(SqList L, int i, ElemType* e)
{
    if(L.length==0 || i<1|| i>L.length)
        return FALSE;
    *e=L.data[i-1];
    return TRUE;
}

Bool ListInsert(SqList* L,int i,ElemType e)
{
    int k;
    if(L->length==MAXSIZE || i<1|| i>L->length+1)//線性表滿,或者i的範圍不在合理範圍內時返回錯誤
        return FALSE;
    if(i<=L->length)//不在表尾
    {
        //插入位置的後續元素後移一位
        for(k=L->length-1;k>=i-1;k--)
            L->data[k+1]=L->data[k];// 倒序挪動位置,避免覆蓋問題
    }
    L->data[i-1]=e;//插入元素
    L->length++;
    return TRUE;
}

Bool ListDelete(SqList* L,int i,ElemType* e)
{
    int k;
    if(L->length==0 || i<1|| i>L->length)//線性表滿,或者i的範圍不在合理範圍內時返回錯誤
        return FALSE;
    *e=L->data[i-1];//取出元素
    if(i<=L->length)//不在表尾
    {
        //插入位置的後續元素前移一位
        for(k=i-1;k<L->length-1;k++)
            L->data[k]=L->data[k+1];// 倒序挪動位置,避免覆蓋問題
    }
    L->length--;
    return TRUE;
}

Bool CreatList(SqList* L)
{
    int i;
    printf("插入多少個元素(0-20)?\n");
    scanf("%d",&(L->length));
    if(L->length<0||L->length>MAXSIZE)
        return FALSE;
    for(i=1;i<=L->length;i++)
    {
//        cout<<"請輸入順序線性表的第"<<i<<"個元素:";
//        cin>>L->data[i-1];
        L->data[i-1]=i;
    }
    return TRUE;
}
Bool ListEmpty(SqList L)
{
    if (L.length==0)
        return TRUE;
    return FALSE;
}

void clearList(SqList* L)
{
    L->length=0;
}
int  LocateElem(SqList L,ElemType e)//成功則返回元素的序號(從1開始),失敗則返回0
{
    int i=0;
    for(i=0;i<L.length;i++)
        if(L.data[i]==e)
            return i+1;
    return 0;
}
int  ListLength(SqList L)
{
    return L.length;
}

void display(SqList L)
{
    int i;
    for(i=0;i<L.length;i++)
        printf("%d  ",L.data[i]);
    printf("\n");
}


//不呼叫底層程式,直接程式設計實現順序表合併,這種方法較為複雜
Bool UnionList1(SqList* L1,SqList* L2,SqList* L)
{
    int i,j,k;
    L->length=0;
    for(i=0;i<L1->length;i++)
    {
        L->data[i]=L1->data[i];
    }

    for(j=0;j<L2->length;j++)
    {
        for(k=0;k<L1->length;k++)
        {
           if(L2->data[j]==L1->data[k])
               break;
        }

        if(k==L1->length)
        {
            if(i>=MAXSIZE)
                return FALSE;
            L->data[i]=L2->data[j];
            i++;
        }
    }
    L->length=i;
    return TRUE;
}

//呼叫底層程式來查詢元素,減輕了工作量
Bool UnionList(SqList* L1,SqList* L2,SqList* L)
{
    int i,j;
    L->length=0;
    for(i=0;i<L1->length;i++)
    {
        L->data[i]=L1->data[i];
    }

    for(j=0;j<L2->length;j++)
        if(LocateElem(*L1,L2->data[j])==0)
        {
            if(i>=MAXSIZE)
                return FALSE;
            L->data[i]=L2->data[j];
            i++;
        }

    L->length=i;
    return TRUE;
}

(3)主函式 main.c

#include <stdio.h>
#include "sequencelist.h"

int main()
{
    SqList list;
    int num;
    ElemType elem;
    Bool flag;

    printf("            1.順序表的建立和顯示\n");
    if(!CreatList(&list))
        printf("順序表建立失敗!\n");
    else
        printf("順序表建立成功!\n    ");
    //順序表的顯示
    display(list);
    printf("\n\n");

    printf("            2.按元素查詢\n");
    num=LocateElem(list,3);
    printf("3是順序表的第%d個元素",num);
    printf("\n\n\n");

    printf("            3.按位置查詢\n");
    GetElem(list,4,&elem);
    printf("順序表的第4個元素是%d",elem);
    printf("\n\n\n");

    printf("            4.順序表的插入\n");
    ListInsert(&list,2,10);
    printf("在第2個位置插入10後:\n    ");
    display(list);
    printf("\n\n");

    printf("            5.刪除元素\n");
    ListDelete(&list,5,&elem);
    printf("刪掉第5個元素:%d\n",elem);
    printf("該表的長度為:%d\n    ",ListLength(list));
    display(list);
    printf("\n\n");

    printf("            6.清空順序表\n");
    printf("清空順序表前-----");
    if(!ListEmpty(list))
    {
        printf("當前順序表不是空表!\n");
        clearList(&list);
        printf("清空順序表後-----");
        if(ListEmpty(list))
            printf("當前順序表是空表!\n");
    }
    printf("\n\n");

    printf("            7.合併順序表\n");
    SqList list1={{0,1,2,3,4,5,6,7},8},list2={{5,6,7,8,9,10,11,1,12},9},list3;
    flag=UnionList(&list1,&list2,&list3);
    if(!flag)
        printf("合併後,順序表的長度超過最大範圍");
    printf("該表的長度為:%d\n    ",ListLength(list3));
    display(list3);
    printf("\n\n");
    return 0;
}
  • 3.3 測試結果


    這裡寫圖片描述