【數據結構】雙向循環鏈表實現簡易圖書管理系統的增刪改查

圖書管理系統


使用雙向循環鏈表實現一個簡單的圖書管理系統,圖書管理系統有如下功能:
1.添加書籍
2.刪除書籍
3.修改書籍信息
4.查詢書籍信息
5.借書
6.還書

#include <stdio.h>
#include <stdlib.h>
#include <string.h>// 書籍結構體
struct book {char name[20];      // 書名char authorname[20]; // 作者名float price;        // 價格int num;            // 總數量int borrowed;       // 已借出數量
};// 雙向循環鏈表節點
struct node {struct book data;struct node *prev;struct node *next;
};// 全局變量:鏈表頭指針
struct node *head = NULL;// 函數聲明
void add_book();
void delete_book();
void modify_book();
void search_book();
void borrow_book();
void return_book();
void display_all();
void free_list();
struct node* find_book(char *name);int main() {int choice;while (1) {printf("\n圖書管理系統\n");printf("1. 添加書籍\n");printf("2. 刪除書籍\n");printf("3. 修改書籍信息\n");printf("4. 查詢書籍信息\n");printf("5. 借書\n");printf("6. 還書\n");printf("7. 顯示所有書籍\n");printf("0. 退出\n");printf("請選擇操作: ");scanf("%d", &choice);switch (choice) {case 1: add_book(); break;case 2: delete_book(); break;case 3: modify_book(); break;case 4: search_book(); break;case 5: borrow_book(); break;case 6: return_book(); break;case 7: display_all(); break;case 0: free_list(); return 0;default: printf("無效選擇!\n");}}return 0;
}// 添加書籍
void add_book() {struct node *new_node = (struct node*)malloc(sizeof(struct node));if (!new_node) {printf("內存分配失敗!\n");return;}printf("請輸入書名: ");scanf("%s", new_node->data.name);printf("請輸入作者名: ");scanf("%s", new_node->data.authorname);printf("請輸入價格: ");scanf("%f", &new_node->data.price);printf("請輸入總數量: ");scanf("%d", &new_node->data.num);new_node->data.borrowed = 0;if (!head) {head = new_node;head->next = head;head->prev = head;} else {new_node->next = head;new_node->prev = head->prev;head->prev->next = new_node;head->prev = new_node;}printf("書籍添加成功!\n");
}// 刪除書籍
void delete_book() {if (!head) {printf("沒有書籍可刪除!\n");return;}char name[20];printf("請輸入要刪除的書名: ");scanf("%s", name);struct node *current = head;do {if (strcmp(current->data.name, name) == 0) {if (current->data.borrowed > 0) {printf("該書還有借出未還,不能刪除!\n");return;}if (current == head) {if (head->next == head) {head = NULL;} else {head = head->next;}}current->prev->next = current->next;current->next->prev = current->prev;free(current);printf("書籍刪除成功!\n");return;}current = current->next;} while (current != head);printf("未找到該書!\n");
}// 修改書籍信息
void modify_book() {if (!head) {printf("沒有書籍可修改!\n");return;}char name[20];printf("請輸入要修改的書名: ");scanf("%s", name);struct node *book = find_book(name);if (!book) {printf("未找到該書!\n");return;}printf("當前信息:\n");printf("書名: %s\n", book->data.name);printf("作者: %s\n", book->data.authorname);printf("價格: %.2f\n", book->data.price);printf("總數量: %d\n", book->data.num);printf("已借出: %d\n", book->data.borrowed);printf("\n請輸入新的作者名: ");scanf("%s", book->data.authorname);printf("請輸入新的價格: ");scanf("%f", &book->data.price);printf("請輸入新的總數量: ");scanf("%d", &book->data.num);printf("書籍信息修改成功!\n");
}// 查詢書籍信息
void search_book() {if (!head) {printf("沒有書籍可查詢!\n");return;}char name[20];printf("請輸入要查詢的書名: ");scanf("%s", name);struct node *book = find_book(name);if (!book) {printf("未找到該書!\n");return;}printf("\n書籍信息:\n");printf("書名: %s\n", book->data.name);printf("作者: %s\n", book->data.authorname);printf("價格: %.2f\n", book->data.price);printf("總數量: %d\n", book->data.num);printf("可借數量: %d\n", book->data.num - book->data.borrowed);
}// 借書
void borrow_book() {if (!head) {printf("沒有書籍可借!\n");return;}char name[20];printf("請輸入要借的書名: ");scanf("%s", name);struct node *book = find_book(name);if (!book) {printf("未找到該書!\n");return;}int available = book->data.num - book->data.borrowed;printf("當前可借數量: %d\n", available);if (available <= 0) {printf("該書已全部借出!\n");return;}int quantity;printf("請輸入要借的數量: ");scanf("%d", &quantity);if (quantity <= 0) {printf("借書數量必須大于0!\n");return;}if (quantity > available) {printf("借書數量超過可借數量!\n");return;}book->data.borrowed += quantity;printf("借書成功! 當前已借出: %d\n", book->data.borrowed);
}// 還書
void return_book() {if (!head) {printf("沒有書籍可還!\n");return;}char name[20];printf("請輸入要還的書名: ");scanf("%s", name);struct node *book = find_book(name);if (!book) {printf("未找到該書!\n");return;}if (book->data.borrowed <= 0) {printf("該書沒有借出記錄!\n");return;}printf("當前已借出數量: %d\n", book->data.borrowed);int quantity;printf("請輸入要還的數量: ");scanf("%d", &quantity);if (quantity <= 0) {printf("還書數量必須大于0!\n");return;}if (quantity > book->data.borrowed) {printf("還書數量超過已借出數量!\n");return;}book->data.borrowed -= quantity;printf("還書成功! 當前已借出: %d\n", book->data.borrowed);
}// 顯示所有書籍
void display_all() {if (!head) {printf("沒有書籍可顯示!\n");return;}struct node *current = head;printf("\n所有書籍信息:\n");do {printf("\n書名: %s\n", current->data.name);printf("作者: %s\n", current->data.authorname);printf("價格: %.2f\n", current->data.price);printf("總數量: %d\n", current->data.num);printf("可借數量: %d\n", current->data.num - current->data.borrowed);current = current->next;} while (current != head);
}// 釋放鏈表內存
void free_list() {if (!head) return;struct node *current = head;struct node *temp;do {temp = current;current = current->next;free(temp);} while (current != head);
}// 查找書籍
struct node* find_book(char *name) {if (!head) return NULL;struct node *current = head;do {if (strcmp(current->data.name, name) == 0) {return current;}current = current->next;} while (current != head);return NULL;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/66646.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/66646.shtml
英文地址,請注明出處:http://en.pswp.cn/web/66646.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

強化學習入門--基本概念

強化學習基本概念 grid-world example 這個指的是一個小機器人&#xff08;agent&#xff09;在一個網格區域&#xff08;存在邊界&#xff09;&#xff0c;網格中存在需要躲避的格子和目標格子&#xff0c;我們的目的就是找到到達目標格子的最短路徑 state 表示智能體相對…

STMCubeMX配置STM32F103ZET6

1 配置時鐘 配置RCC。 配置 SYS。將Timebase Source配置為TIM1, SysTick留給FreeRTOS用。 注意: 由于第一次配置的時候忘記配置這個步驟,導致工程第一次燒錄成功后,后面一直無法燒錄,報以下錯誤: keil no target connect Error: Flash Download failed - Target DLL h…

OFD 套版生成原理與 C# 實現詳解

1. 引言 OFD&#xff08;Open Fixed-layout Document&#xff09;是一種基于 XML 的開放版式文檔格式&#xff0c;主要用于電子文檔的存儲和交換。與 PDF 類似&#xff0c;OFD 是一種固定版式文檔格式&#xff0c;能夠確保文檔在不同設備和平臺上顯示的一致性。OFD 格式廣泛應…

Leetcode:2239

1&#xff0c;題目 2&#xff0c;思路 循環遍歷滿足條件就記錄&#xff0c;最后返回結果值 3&#xff0c;代碼 public class Leetcode2239 {public static void main(String[] args) {System.out.println(new Solution2239().findClosestNumber(new int[]{-4, -2, 1, 4, 8})…

C語言之斗地主游戲

&#x1f31f; 嗨&#xff0c;我是LucianaiB&#xff01; &#x1f30d; 總有人間一兩風&#xff0c;填我十萬八千夢。 &#x1f680; 路漫漫其修遠兮&#xff0c;吾將上下而求索。 ? C語言之斗地主游戲 目錄 程序概述程序設計 Card類CardGroup類Player類LastCards類Land…

python編程-OpenCV(圖像讀寫-圖像處理-圖像濾波-角點檢測-邊緣檢測)圖像變換

形態變換 圖像處理中的形態學操作是處理圖像結構的有效方法。以下是一些常見的形態學操作的介紹及其在 OpenCV 中的實現示例。 1. 腐蝕&#xff08;Erosion&#xff09; 腐蝕操作通過消除圖像邊界來減少圖像中的白色區域&#xff08;前景&#xff09;&#xff0c;使物體的邊…

【Prometheus】PromQL進階用法

?? 歡迎大家來到景天科技苑?? &#x1f388;&#x1f388; 養成好習慣&#xff0c;先贊后看哦~&#x1f388;&#x1f388; &#x1f3c6; 作者簡介&#xff1a;景天科技苑 &#x1f3c6;《頭銜》&#xff1a;大廠架構師&#xff0c;華為云開發者社區專家博主&#xff0c;…

SiamCAR(2019CVPR):用于視覺跟蹤的Siamese全卷積分類和回歸網絡

原文標題:SiamCAR: Siamese Fully Convolutional Classification and Regression for Visual Tracking 中文標題:SiamCAR:用于視覺跟蹤的Siamese全卷積分類和回歸 代碼地址: https://github.com/ohhhyeahhh/SiamCAR Abstract 通過將視覺跟蹤任務分解為兩個子問題,…

計算機網絡介質訪問控制全攻略:從信道劃分到協議詳解!!!

一、信道劃分介質訪問控制 介質訪問控制&#xff1a;多個節點共享同一個“總線型”廣播信道時&#xff0c;可能發生“信號沖突” 應該怎么控制各節點對傳輸介質的訪問&#xff0c;才能減少沖突&#xff0c;甚至避免沖突? 時分復用(TDM) 時分復用&#xff1a;將時間分為等長的“…

Prometheus部署及linux、mysql、monog、redis、RocketMQ、java_jvm監控配置

Prometheus部署及linux、mysql、monog、redis、RocketMQ、java_jvm監控配置 1.Prometheus部署1.2.Prometheus修改默認端口 2.grafana可視化頁面部署3.alertmanager部署4.監控配置4.1.主機監控node-exporter4.2.監控mysql數據庫mysqld_exporter4.3.監控mongod數據庫mongodb_expo…

基于tldextract提取URL里的子域名、主域名、頂級域

TLD是TopLevel Domain的縮寫。?tldextract? 是一個用于從URL中提取子域、主域名和頂級域&#xff08;TLD&#xff09;的Python庫。它利用公共后綴列表&#xff08;Public Suffix List&#xff09;來確保即使是復雜或不常見的URL結構也能被正確解析。tldextract能夠處理包括IC…

常見Arthas命令與實踐

Arthas 官網&#xff1a;https://arthas.aliyun.com/doc/&#xff0c;官方文檔對 Arthas 的每個命令都做出了介紹和解釋&#xff0c;并且還有在線教程&#xff0c;方便學習和熟悉命令。 Arthas Idea 的 IDEA 插件。 這是一款能快速生成 Arthas命令的插件&#xff0c;可快速生成…

Mellanox ConnectX 系列網卡的雙驅動架構:以太網與 InfiniBand 的協同設計

在現代數據中心和高性能計算(HPC)環境中,網絡硬件的性能和功能至關重要。Mellanox ConnectX 系列網卡以其卓越的性能和多功能性而聞名,支持從傳統的以太網到高性能的 InfiniBand 網絡協議。這種多功能性使得 Mellanox 網卡能夠滿足不同應用場景的需求,從常規的數據中心網絡…

win32匯編環境,對多行編輯框添加或刪除文本

;運行效果 ;win32匯編環境,對多行編輯框添加或刪除文本 ;主要要先設置文本的開始點與結束點&#xff0c;然后把一段文本頂替上去。沒有添加文本或刪除文本的概念&#xff0c;只有頂替。如果開始點與結束點都是前面文本的長度值&#xff0c;則成了從后面添加文本的效果。如果結束…

CSDN年度回顧:技術征途上的堅實步伐

嘿&#xff0c;時光過得可真快呀&#xff0c;就像那匹跑得飛快的白馬&#xff0c;嗖的一下&#xff0c;2024 年的日歷就這么悄無聲息地翻到了最后一頁。這會兒我回頭看看在 CSDN 上度過的這一年&#xff0c;心里那叫一個感慨萬千&#xff0c;滿滿的都是喜悅&#xff0c;就像心里…

泛型子類使用Builder提示:both methods have same erasure, yet neither hides the other

父類 Data Builder AllArgsConstructor NoArgsConstructor public class ParentClass {public String name; } 子類 AllArgsConstructor NoArgsConstructor Data SuperBuilder public class ChildClass<T> extends ParentClass {private T value; } 提示錯誤 builde…

Springboot集成Elasticsearch8.0(ES)版本,采用JAVA Client方式進行連接和實現CRUD操作

本文章介紹了 springboot t集成Elasticsearch8.0(ES)版本,如何通過 AVA Client方式進行連接和實現CRUD操作 在ES7.15版本之后,ES官方將高級客戶端 RestHighLevelClient標記為棄用狀態。同時推出了全新的 Java API客戶端 Elasticsearch Java API Client,該客戶端也將在 Ela…

人臉識別打卡系統--基于QT(附源碼)

逃離舒適區 項目源代碼放在我的倉庫中&#xff0c;有需要自取 項目地址 https://gitcode.com/hujiahangdewa/Face_recognition.git 文章目錄 一、項目結構分析二、服務器的搭建三、客戶端的搭建四、人臉識別庫的申請五、基于人臉識別庫的識別判斷六、QT人臉識別----調用百度ai…

人工智能在數字化轉型中的角色:從數據分析到智能決策

引言 在數字化轉型浪潮中&#xff0c;人工智能&#xff08;AI&#xff09;正迅速崛起&#xff0c;成為推動企業創新和變革的關鍵力量。面對日益復雜的市場環境和激烈的行業競爭&#xff0c;企業亟需借助技術手段提高運營效率、優化決策過程&#xff0c;并增強市場競爭力。而AI…

react install

react 安裝 React 是一個用于構建用戶界面的 JavaScript 庫。以下是安裝 React 的步驟&#xff1a; 使用 Create React App Create React App 是一個官方支持的命令行工具&#xff0c;用于快速搭建 React 應用。 安裝 Node.js 和 npm 確保你的計算機上安裝了 Node.js 和 npm…