天天看点

数据结构笔记之顺序表

顺序表

1.定义:用一组地址连续的存储单元依次存储线性表的数据元素的表。

数据结构笔记之顺序表

由上图可以看出元素地址满足以下关系:

数据结构笔记之顺序表

2.类型定义

以图书数据举例,其类型定义如下:

#define MAXSIZE 100 //最大长度
typedef struct      
{
    char no[];    //图书ISBN
    char name[];  //图书名字
    float price;    //图书价格
}Book;              //图书结构类型
typedef struct
{ 
    Book *elem;     //基地址
    int length;     //图书个数
}SqList;            //图书表结构类型
           

3.顺序表操作

3.1初始化

【步骤】

①为L分配一个预定义大小的数组空间,elem指向这段空间的基地址。

②将表的长度置为0。

Status InitList(SqList &L)
{
    L.elem = new ElemType[MAXSIZE];
    if(!L.elem) exit(OVERFLOW);
    L.length = ;
    return OK;
}
           

3.2取值

【步骤】

①判断i值合不合理。

②将第i个元素即L.elem[i-1]赋给e。

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

3.3查找

【步骤】

①从第一个元素开始,依次比较,若相等返回i+1,若比较到最后也没有找到则查找失败,返回0。

Status LocateElem(SqList L,ElemType e)
{
    for(i=;i<L.length;i++)
        if(L.elem[i] == e)    return i+;
    return ;
}
           

查找平均时间复杂度O(n)

3.4插入

数据结构笔记之顺序表

【步骤】

①判断插入位置是否合法。

②判断存储空间是否已满。

③从后往前从第n至i依次往移动,空出第i各位置。

④将元素插入第i个位置。

Status ListInsert(SqList &L,int i;ELemType e)
{
    if((i<) || (i>L.length+))    return ERROR;
    if(L.length == MAXSIZE)    return ERROR;
    for(j=L.length-;j>=i-;j--)
        L.elem[j+] = L.elem[j];
    L.elem[i-] = e;
    ++L.length;
    return OK;
}
           

平均时间复杂度O(n)

3.5删除

数据结构笔记之顺序表

【步骤】

①判断i是否合法。

②将第i+1个至n依次前移。

③表长减1。

Status ListDelete(SqList &L, int i)
{
    if(i< || i>L.length)    retrun ERROR;
    for(j=i;j<Length-;j++)
        L.elem[j-]=L.elem[j];
    --L.length;
    return OK;
}
           

平均时间复杂度O(n)

3.6销毁线性表L

void DestroyList(SqList &L)
{
  if (L.elem) delete[]L.elem;    //释放存储空间
}
           

3.7清空线性表

void ClearList(SqList &L) 
{
   L.length=;                //将线性表的长度置为0
}
           

4.完整代码

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型

#define MAXSIZE 100         //顺序表可能达到的最大长度
struct Book {
    string id;//ISBN
    string name;//书名
    double price;//定价
};
typedef struct {
    Book *elem; //存储空间的基地址
    int length; //当前长度
} SqList;

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

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

int LocateElem_Sq(SqList L, double e) { //算法2.3 顺序表的查找
    //顺序表的查找
    for (int i = ; i < L.length; i++)
        if (L.elem[i].price == e)
            return i + ;//查找成功,返回序号i+1
    return ;//查找失败,返回0
}

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

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

int main() {
    SqList L;
    int i = , temp, a, c, choose;
    double price;
    Book e;
    string head_1, head_2, head_3;
    cout << "1. 建立\n";
    cout << "2. 输入\n";
    cout << "3. 取值\n";
    cout << "4. 查找\n";
    cout << "5. 插入\n";
    cout << "6. 删除\n";
    cout << "7. 输出\n";
    cout << "0. 退出\n\n";

    choose = -;
    while (choose != ) {
        cout << "请选择:";
        cin >> choose;
        switch (choose) {
        case ://创建顺序表
            if (InitList_Sq(L))
                cout << "成功建立顺序表\n\n";
            else
                cout << "顺序表建立失败\n\n";
            break;
        case : {//顺序表信息输入
            i = ;
            L.elem = new Book[MAXSIZE];
            if (!L.elem)
                exit(OVERFLOW);
            L.length = ;
            fstream file;
            file.open("book.txt");
            if (!file) {
                cout << "错误!未找到文件!" << endl;
                exit(ERROR);
            }
            file >> head_1 >> head_2 >> head_3;
            while (!file.eof()) {
                file >> L.elem[i].id >> L.elem[i].name >> L.elem[i].price;
                i++;
            }
            cout << "输入 book.txt 信息完毕\n\n";
            L.length = i;
            file.close();
        }
            break;
        case ://顺序表的取值
            cout << "请输入一个位置用来取值:\n";
            cin >> i;
            temp = GetElem(L, i, e);
            if (temp != ) {
                cout << "查找成功\n";
                cout << "第" << i << "本图书的信息是:\n";
                cout << left << setw() << e.id << "\t" << left << setw()
                        << e.name << "\t" << left << setw() << e.price << endl
                        << endl;
            } else
                cout << "查找失败!位置超出范围\n\n";
            break;
        case : //顺序表的查找
            cout << "请输入所要查找价格:";
            cin >> price;
            temp = LocateElem_Sq(L, price);
            if (temp != ) {
                cout << "查找成功\n";
                cout << "该价格对应的书名为:" << L.elem[temp - ].name << endl << endl;
            } else
                cout << "查找失败!没有这个价格对应的书籍\n\n";
            break;
        case : //顺序表的插入
            cout << "请输入插入的位置和书本信息,包括:编号 书名 价格(用空格隔开):";
            cin >> a;
            cin >> e.id >> e.name >> e.price; //输入a和b,a代表插入的位置,b代表插入的数值(书本信息)
            if (ListInsert_Sq(L, a, e))
                cout << "插入成功.\n\n";
            else
                cout << "插入失败.\n\n";
            break;
        case : //顺序表的删除
            cout << "请输入所要删除的书籍的位置:";
            cin >> c;
            if (ListDelete_Sq(L, c))
                cout << "删除成功.\n\n";
            else
                cout << "删除失败.\n\n";
            break;
        case : //顺序表的输出
            cout << "当前图书系统信息(顺序表)读出:\n";
            for (i = ; i < L.length; i++)
                cout << left << setw() << L.elem[i].id << "\t" << left
                        << setw() << L.elem[i].name << "\t" << left
                        << setw() << L.elem[i].price << endl;
            cout << endl;
            break;
        }
    }
    return ;
}
           

5.顺序表的优缺点

数据结构笔记之顺序表

继续阅读