线性表的顺序存储结构(c++实现)

线性表:零个 或 多个数据的有限序列
第一个元素没有前驱,最后一个元素没有后继
线性表的顺序存储结构,指的是用一段地址连续的存储单元依次存储线性表的数据元素
线性表的顺去存储是通过数组来实现的

#pragma once
#include <iostream>
using namespace std;
#define length 50

#include<fstream>

class Arr
{
   
public:
	Arr();//构造函数
	~Arr();//析构函数
	bool add_arr(int val);//添加元素
	bool delete_arr(int val, int pos);//在pos位置上删除一个val值元素
	bool insert_arr(int val, int pos);在pos位置上插入一个val值元素
	bool is_empty();//判断数组是否为空
	bool is_full();//判断数组是否满
	void sort_arr();//数组排序
	void show_arr();//显示数组元素
	void inversion_arr();//倒置数组

	int *Base;//存储数组第一个元素地址

	
	int count;//线性表有效元素的个数,线性表的长度
	


};

具体函数实现如下

在这里插入代码片#include<iostream>
#include"线性表中顺序存储结构.h"
using namespace std;
Arr::Arr()//构造函数
{
   
	count = 0
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值