实验报告3(使用单链表简单实现图书管理系统)

news/2025/10/10 22:28:40/文章来源:https://www.cnblogs.com/LCGJ/p/18405072

一、实验目的:

使用单链表实现案例2.3的图书管理系统,要求实现查找、插入、删除和计数功能。要求包含主函数,用c语言或者c++实现。

二、实验仪器或设备:

操作系统:Windows11

编程环境:Dev-cpp 5.11

三、算法总体设计

1.创建节点

2.头插法

3.查找

4. 打印链表

5.从文件读取书籍信息到链表

6. 计数

7.用户交互

四、实验步骤(包括主要步骤、命令分析等)

  1 #include <iostream>
  2 #include <fstream>
  3 #include <string>
  4 #include <iomanip>
  5 #include <limits>
  6 #include <vector> 
  7 using namespace std;
  8 typedef int Status;
  9 const Status ERROR = -1;
 10 const Status OVERFLOW = -2;
 11 const Status OK = 1;
 12 // 图书的信息结构体
 13 struct Book {
 14     string id; // ISBN
 15     string name; // 书名
 16     double price; // 定价
 17 };
 18 // 单链表节点结构体
 19 struct Node {
 20     Book data;
 21     Node* next;
 22 };
 23 // 创建新节点
 24 Node* createNode(const Book& book) {
 25     Node* newNode = new Node();
 26     newNode->data = book;
 27     newNode->next = NULL;
 28     return newNode;
 29 }
 30 // 插入节点到链表头部
 31 void insertNodeByHead(Node*& head, const Book& book) {
 32     Node* newNode = createNode(book);
 33     newNode->next = head;
 34     head = newNode;
 35 }
 36 void deleteNodeByName(Node*& head, const string& bookname) {
 37     if (head == NULL) {
 38         cout << "链表为空,无法删除节点。" << endl;
 39         return;
 40     }
 41     Node* posLeftNode = NULL;  // 用于存储当前节点的前一个节点
 42     Node* posNode = head;      // 当前节点 
 43     // 检查头节点是否是目标节点
 44     if (posNode->data.name == bookname) {
 45         Node* temp = posNode;
 46         head = posNode->next;  // 更新头指针
 47         delete temp;           // 删除头节点
 48         cout << "删除成功: " << bookname << endl;
 49         return;
 50     }
 51     // 遍历链表查找目标节点
 52     while (posNode != NULL && posNode->data.name != bookname) {
 53         posLeftNode = posNode;
 54         posNode = posNode->next;
 55     }
 56     if (posNode == NULL) {
 57         cout << "未找到书籍: " << bookname << endl;
 58     } else {
 59         posLeftNode->next = posNode->next;  // 从链表中移除目标节点
 60         delete posNode;  // 释放目标节点的内存
 61         cout << "删除成功: " << bookname << endl;
 62     }
 63 }
 64 // 根据书名进行查找 
 65 void findNodeByName(Node*& head, const string& bookname) {
 66     Node* posLeftNode = head;
 67     Node* posNode = head->next;
 68     if(posLeftNode->data.name == bookname)
 69     {
 70         cout << "成功找到书籍: " << bookname << endl;
 71         cout << "书籍的信息为: " << setw(10) <<posLeftNode->data.id << setw(30) <<posLeftNode->data.name << setw(15) << posLeftNode->data.price << endl;
 72     }   
 73     else{
 74     while (posNode != NULL && posNode->data.name != bookname) {
 75         posLeftNode = posNode;
 76         posNode = posNode->next;
 77     }
 78     if (posNode == NULL) {
 79         cout << "未找到书籍: " << bookname << endl;
 80         return;
 81     } else {
 82         cout << "成功找到书籍: " << bookname << endl;
 83         cout << "书籍的信息为: " << setw(10) <<posNode->data.id << setw(30) << posNode->data.name << setw(15) << posNode->data.price << endl;       
 84     }
 85     }    
 86 }
 87 // 打印链表
 88 void printList(Node* head) {
 89     if (head == NULL) {
 90         std::cout << "链表为空" << std::endl;
 91         return;
 92     }
 93    cout << std::setw(10) << "ID" << std::setw(30) << "书名" << std::setw(15) << "价格" << std::endl;
 94     Node* pMove = head; // 直接从 head 开始遍历,因为没有头节点
 95     while (pMove != NULL) {
 96        cout <<setw(10) << pMove->data.id <<setw(30) << pMove->data.name<<setw(15) << pMove->data.price <<endl;
 97         pMove = pMove->next;
 98     }
 99 }
100 // 从文件读取书籍信息到链表
101 Status readBooksFromFile(Node*& head, const string& filename) {
102     ifstream infile(filename.c_str());
103     if (!infile.is_open()) {
104         cerr << "文件打开失败 " << filename << endl;
105         return ERROR;
106     }
107     string id, name;
108     double price;
109     infile.ignore(numeric_limits<streamsize>::max(), '\n'); // 跳过标题行
110     while (infile >> id >> name >> price) {
111         Book book = {id, name, price};
112         insertNodeByHead(head, book);
113     }
114     infile.close();
115     return OK;
116 }
117 //计数
118 int countBooks(Node*& head) {
119     int count = 0;
120     Node* posNode = head->next; // 从头节点的下一个节点开始计数
121     while (posNode != NULL) {
122         count++;
123         posNode = posNode->next;
124     }
125     return count; 
126 }
127 void clearList(Node*& head) {
128     Node* temp;
129     while (head != NULL) {
130         temp = head;
131         head = head->next;
132         delete temp;
133     }
134 }
135 // 主函数
136 int main() {
137     Node* head = new Node(); // 创建头节点(这里作为哑节点,不存储实际数据)
138     head->next = NULL;
139     string filename = "book.txt";
140     Status status = readBooksFromFile(head, filename);
141     if (status == OK) {
142         printList(head);
143     } else {
144         cerr << "读取书籍信息失败。" << endl;
145          delete head; // 如果读取失败,释放头节点
146         return 1;
147     }
148     int userkey;
149     string bookname;
150     Book tempBook;
151     while (true) {
152         cout << "\n欢迎来到图书管理系统\n";
153         cout << "请输入项目前编号执行相关的操作\n";
154         cout << "[0]退出\n";
155         cout << "[1]查找\n";
156         cout << "[2]插入\n";
157         cout << "[3]删除\n";
158         cout<< "[4]计数\n";
159         cout << "请选择: ";
160         cin >> userkey;
161         switch (userkey) {
162             case 0:
163                 cout << "退出成功\n";
164               clearList(head); // 释放整个链表
165                 delete head; // 释放头节点
166                 return 0;
167             case 1:
168                 cout << "【查找】\n";
169                   cout << "请输入要查找的书籍名字: ";
170                 cin >> bookname;
171                 findNodeByName(head, bookname);
172                 
173                 break;
174             case 2:
175                 cout << "【插入】\n";
176                 cout << "请输入ISBN、书名和价格(用空格分隔): ";
177                 cin >> tempBook.id >> tempBook.name >> tempBook.price;
178                 insertNodeByHead(head, tempBook);
179                 printList(head);
180                 break;
181             case 3:
182                 cout << "【删除】\n";
183                 cout << "请输入要删除的书籍名字: ";
184                 cin >> bookname;
185                 deleteNodeByName(head, bookname);
186                 printList(head);
187                 break;
188                 case 4:
189                 cout<<"【计数】\n";
190                 cout<<"书籍总本数为:"<<countBooks(head); 
191         }
192     }
193     return 0;
194 }

1.首页

 

2.查找功能

 

3.插入功能

 

4.删除功能

 

5.计数功能

 

6.退出

 

结语:

通过本次实验,我深入理解了链表的基本操作,包括节点的创建、遍历、访问和删除等。同时,我也学会了如何在链表中根据特定条件查找并删除节点,这对于我掌握数据结构的基本概念和操作方法具有重要意义...

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/934448.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【黑马python】2.Python 字符串

参考链接黑马-2.Python 字符串 08-字符串的三种定义方式tbd

FineReport自定义登录系统技术 - 详解

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

实验报告2(简单实现图书馆管理系统)

一、实验目的:、 实现书上图书馆管理系统 (1) 主函数 (2) 修改:根据指定的ISBN,修改图书的价格 (3) 排序:将图书按照价格由低到高进行排序。 (4) 计数:统计文件中的图书数量 要求:用c语…

实验报告1(switch语句,二维数组)

一、实验目的: 熟练使用switch语句 熟练使用二维数组 二、实验仪器或设备: 操作系统:Windows11 编程环境:Dev-cpp 5.11 三、算法总体设计 (1)项目一:运输公司对用户计算运费 用到的算法的目的:计算并输出基于给…

【实现自己的 kafka!】kafka 的关键概念

kafka 的诞生 现在是在 2000 年代后期,你的名字叫做 Jay Kreps,你就职于 LinkedIn 公司。 LinkedIn 作为社交网络平台,用户规模和数据量现在快速增长,同时内部存在多种数据传递和处理需求,比如用户行为跟踪、日志…

12. 对话框

一、对话框对话框窗口是一个用来完成简单任务或者和用户进行临时交互的顶层窗口,通常用于输入信息、确认信息或者提示信息。Qt Quick 提供了一系列的标准对话框,如 FileDialog、ColorDialog、MessageDialog、FontDia…

2024ICPC区域赛香港站

define时间:#define int long long #define ind long double #define yes cout << "Yes" #define no cout << "No" #define pii pair<long long, long long> #define all(x) (…

AI产品经理要了解的算法有哪些?

中世纪拉丁语“algorismus”指的是用印度数字进行四个基本数学运算——加法,减法,乘法和除法的程序和捷径。后来,术语“算法”被人们用作表示任何逐步的逻辑过程,并成为计算逻辑的核心。 算法的历史可以分为三个阶…

一位印度小哥逆袭成为谷歌数据科学家的心路历程 - 教程

一位印度小哥逆袭成为谷歌数据科学家的心路历程 - 教程pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas&q…

基于selenium的网页自动搜索

第一节 通过简单的百度网页打开学习selenium库的基本功能。1 from selenium import webdriver2 from selenium.webdriver.chrome.service import Service3 from selenium.webdriver.chrome.options import Options4 fr…

MacOS Nginx

查看是否安装:brew info nginx 安装:brew install nginx 卸载:brew uninstall nginx 查看版本:nginx -v 安装目录:/opt/homebrew/Cellar/nginx/1.29.0 (27 files, 2.5MB) 根目录:Docroot is: /opt/homebrew/var/…

缓存的击穿、雪崩、穿透在你项目中的场景是什么

在我们的 OJ 平台中,为了保护数据库、提升响应速度,我设计了一套缓存防护体系: 缓存穿透: 针对恶意请求或不存在的题目 ID,我们用布隆过滤器提前过滤掉无效请求,误判率控制在 0.13% 以下,保护数据库不被大量无效…

[WC2021] 表达式求值

给定一个式子,包含 >,<,? 或者 \([0,m)\) 中的一个数字。其中每个数字代表一个数。 > 代表返回两边的最大值,< 代表返回两边的最小值,? 表示你要在上文的两个符号中选择一个符号替换它。 假设有 \(…

Set集合

无索引 Hashset主注意: LinkedHashset: 存取有顺序其余和hashset一样

JAVA - LinkedList 与 ArrayList 区别和 LinkedList 的四大接口解析

什么是 LinkedListLinkedList 就像一个火车车厢队列。每个“车厢”里装着一个数据(元素),而且每个车厢都知道:自己前面是哪节车厢(previous),自己后面是哪节车厢(next),所以它是一种 “链式结构”。 不像 Ar…

苍穹外卖第三天(Swagger、@RequestParam和@RequestBody的使用场景、@PostMapping和@RequestMapping的区别、对象属性拷贝、@Insert注解)

一、Swagger Swagger是一个用于生成、描述、文档化可视化API的工具(框架)。直接使用Swagger会比较繁琐,所以我们用到了Knife4j框架,它对Swagger进行了封装,简化了相应的操作。 1、Knife4j的使用方式: (1)导入K…

Git 多账号管理

# 新建空白文件夹 mkdir <YOUR PROJECT> # 初始化仓库 git init # 配置当前仓库账号 git config user.name "<YOUR NAME>" git config user.email "<YOUR EMAIL>" # 给当前账号…

完整教程:一文读懂费用分析:定义、分类与成本费用区别

完整教程:一文读懂费用分析:定义、分类与成本费用区别2025-10-10 21:48 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; …

Hyper Server 2019安装I226-V网卡驱动

背景:Hyper-V Server 2019 安装完提示找不到活动的网络适配器 网卡型号:Intel I226-V 以下内容以Hyper-V Server 2019和Intel I226-V网卡为例,其他系统版本(NT6以上)和同系列网卡,操作大同小异,可参考进行。 由…

P10201 永恒

rt好题。 对于一次询问 \((x_1,y_1)\) 到 \((x_2,y_2)\),显然若两点不在同一个联通块中则无解。考虑在同一个联通块中的答案。 我们对整张图进行黑白染色。则有结论:若黑色/白色格点存在不同的数,则一定有解。 证明…