线性表顺序存储C++实现

仅供参考 

#include <iostream>
using namespace std;

// 函数结果状态代码
#define TRUE    1
#define FALSE   0
#define OK      1
#define ERROR   0
#define INFEASIBLE -1
#define OVERFLOW   -2
#define MAXSIZE    100

// Status 是函数的类型 其值是函数结果状态代码
typedef int Status;
typedef char ElemType;

typedef struct {

    ElemType *elem;
    int length;
}Sqlist;

// 初始化操作,建立一个空的线性表L
Status InitList(Sqlist &L);

// 销毁已存在的线性表L
void DestroyList(Sqlist &L);

// 将线性表清空
void ClearList(Sqlist &L);

// 在线性表L中的第i个位置插入新元素e
Status ListInsert(Sqlist &L, int i, ElemType e);

// 删除线性表L中第i个位置元素,用e返回
Status ListDelete(Sqlist &L, int i, ElemType &e);

// 若线性表为空,返回true,否则false
int IsEmpty(Sqlist L);

// L中查找与给定值e相等的元素,若成功返回该元素在表中的序号,否则返回0;
int LocateElem(Sqlist L, ElemType e);

// 将线性表L中的第i个位置元素返回给e
Status GetElem(Sqlist L, int i, ElemType &e);

// 求线性表的长度
int GetLength(Sqlist L);

// 遍历线性表
void ListTraversals(Sqlist L);

///

int main() {

    // 创建一个线性表L
    int ret = 0;
    Sqlist L;
    ret = InitList(L);
    if (ret == OK) cout << "创建线性表L成功" << endl;

    // 插入线性表
    ret = ListInsert(L, 1, 11);
    if (ret == OK) cout << "插入线性表L成功" << endl;
    ret = ListInsert(L, 2, 22);
    ret = ListInsert(L, 3, 33);

    // 遍历线性表
    cout << "遍历线性表: ";
    for (int i = 0; i < L.length; ++i) {
        cout <<  (int)L.elem[i] << " ";
    }
    cout << endl;

    // 求线性表长度
    cout << "线性表的长度:" << GetLength(L) << endl;

    // 删除线性表L中第i个位置元素,用e返回
    ElemType e;
    ret = ListDelete(L, 1 , e);
    cout << "被删除的元素:" << (int)e << endl;
    cout << "被删除后线性表的长度:" << GetLength(L) << endl;

    // L中查找与给定值e相等的元素,若成功返回该元素在表中的序号,否则返回0;
    e = 33;
    ret = LocateElem(L, e);
    cout << "该元素 " << (int)e << " 在表中序号: " << ret << endl;

    // 将线性表L中的第i个位置元素返回给e
    ret = GetElem(L, 1, e);
    cout << "第一个位置元素:" << (int)e << endl;

    // 将线性表清空
    ClearList(L);
    cout << "清空线性表" << endl;
    // 若线性表为空,返回true,否则false
    ret = IsEmpty(L);
    if (ret == TRUE) cout << "线性表为空" << endl;
    else cout << "线性表为非空" << endl;

    // 销毁已存在的线性表L
    DestroyList(L);
    cout << "线性表已销毁" << endl;

    return 0;
}

//

// 构造一个空的顺序表
Status InitList(Sqlist &L){

    L.elem = new ElemType[MAXSIZE];  // 为顺序表分配空间
    if (!L.elem) exit(OVERFLOW);     // 存储分配失败
    L.length = 0;                   // 空表长度0
    return OK;
}

void DestroyList(Sqlist &L){

    if (L.elem) delete L.elem;  // 释放存储空间;
    L.elem = nullptr;
    L.length = 0;
}

// 将线性表清空
void ClearList(Sqlist &L){

    L.length = 0;          // 讲线性表的长度置0
}

// 求线性表的长度
int GetLength(Sqlist L){

    return L.length;
}

// 判断线性表是否为空
int IsEmpty(Sqlist L){

    if (L.length == 0) return TRUE;
    else return FALSE;
}

// 将线性表L中的第i个位置元素返回给e
Status GetElem(Sqlist L, int i, ElemType &e){

    if (i < 1 || i > L.length) return ERROR; // 判断是否合理
    e = L.elem[i-1];    // 第i-1的单元存储着第i个数据
    return OK;
}

// L中查找与给定值e相等的元素,若成功返回该元素在表中的序号,否则返回0;
int LocateElem(Sqlist L, ElemType e){

    for (int i = 0; i < L.length; ++i) {
        if (L.elem[i] == e) return i+1;
    }
    return 0;
}

// 在线性表L中的第i个位置插入新元素e
Status ListInsert(Sqlist &L, int i, ElemType e){

    if (i < 1 || i > L.length + 1) return ERROR;       // 判断i值是否合法
    if (L.length == MAXSIZE) return ERROR;             // 当前存储空间已满
    for (int j = L.length -1 ; j >= i -1 ; --j) {
        L.elem[j+1] = L.elem[j];                       // 插入位置及之后的元素后移
    }
    L.elem[i-1] = e;                                   // 将新元素e放入第i个位置
    L.length++;                                        // 表长+1
    return OK;
}

// 删除线性表L中第i个位置元素,用e返回
Status ListDelete(Sqlist &L, int i, ElemType &e){

    if (i < 1 || i > L.length) return ERROR;
    e = L.elem[i-1];
    for (int j = i; j <= L.length -1 ; ++j) {
        L.elem[j-1] = L.elem[j];
    }
    L.length--;
    return OK;
}

// 遍历线性表
void ListTraversals(Sqlist L){

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

 

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++线性表顺序存储结构可以用来实现图书管理系统,其基本思路是将所有的图书信息存储在一个数组中,每本书对应数组中的一个元素。每个元素包含图书的相关信息,如编号、名称、作者、出版社、价格等等。 具体实现过程中,需要定义一个结构体来存储每本书的信息,然后使用数组来存储所有的书籍。可以定义如下的结构体: ``` struct Book { int id; // 书籍编号 string name; // 书籍名称 string author; // 作者 string publisher; // 出版社 double price; // 价格 }; ``` 接下来,我们可以定义一个类来实现图书管理系统。这个类包括添加图书、删除图书、查找图书等基本功能。其中,添加图书操作可以使用数组的末尾添加,删除图书操作可以使用移动元素位置来实现,查找图书操作可以遍历整个数组进行查找。 下面是一个简单的示例代码: ``` #include <iostream> #include <string> using namespace std; const int MAX_SIZE = 100; // 数组最大长度 class BookManagementSystem { private: Book books[MAX_SIZE]; // 用于存储图书信息的数组 int size; // 数组中存储的图书数量 public: BookManagementSystem() { size = 0; } // 添加图书 void addBook(int id, string name, string author, string publisher, double price) { if (size >= MAX_SIZE) { cout << "图书库已满,无法添加新书!" << endl; return; } books[size].id = id; books[size].name = name; books[size].author = author; books[size].publisher = publisher; books[size].price = price; size++; cout << "添加成功!" << endl; } // 删除图书 void deleteBook(int id) { int pos = -1; // 找到要删除的元素位置 for (int i = 0; i < size; i++) { if (books[i].id == id) { pos = i; break; } } if (pos == -1) { cout << "未找到该编号的图书!" << endl; return; } for (int i = pos; i < size - 1; i++) { books[i] = books[i + 1]; } size--; cout << "删除成功!" << endl; } // 查找图书 void searchBook(int id) { for (int i = 0; i < size; i++) { if (books[i].id == id) { cout << "编号:" << books[i].id << endl; cout << "名称:" << books[i].name << endl; cout << "作者:" << books[i].author << endl; cout << "出版社:" << books[i].publisher << endl; cout << "价格:" << books[i].price << endl; return; } } cout << "未找到该编号的图书!" << endl; } }; int main() { BookManagementSystem bms; bms.addBook(1001, "C++程序设计", "张三", "清华大学出版社", 39.8); bms.addBook(1002, "Java程序设计", "李四", "人民邮电出版社", 45.6); bms.addBook(1003, "Python编程入门", "王五", "电子工业出版社", 29.9); bms.deleteBook(1002); bms.searchBook(1003); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值