图书管理系统数据结构链表c
时间: 2023-07-21 10:52:36 浏览: 83
C语言图书管理系统 链表实现 功能齐全(书籍信息以及用户信息保存在附带的txt文件中)
5星 · 资源好评率100%
图书管理系统可以使用链表来存储书籍信息,每本书可以用一个结构体来表示,包括书名、作者、出版社、ISBN、价格等属性。链表的节点可以表示每本书,包括指向前一个节点和后一个节点的指针,以及存储书籍信息的结构体。
下面是一个简单的图书管理系统链表的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 书籍结构体
typedef struct Book {
char title[50]; // 书名
char author[20]; // 作者
char publisher[30]; // 出版社
char ISBN[20]; // ISBN
float price; // 价格
struct Book* next; // 指向下一个节点的指针
struct Book* prev; // 指向前一个节点的指针
} Book;
// 创建新的书籍节点
Book* create_book_node(char* title, char* author, char* publisher, char* ISBN, float price) {
Book* book = (Book*)malloc(sizeof(Book));
strcpy(book->title, title);
strcpy(book->author, author);
strcpy(book->publisher, publisher);
strcpy(book->ISBN, ISBN);
book->price = price;
book->next = NULL;
book->prev = NULL;
return book;
}
// 在链表末尾添加新的书籍
void add_book(Book** head, char* title, char* author, char* publisher, char* ISBN, float price) {
Book* book = create_book_node(title, author, publisher, ISBN, price);
if (*head == NULL) {
*head = book;
} else {
Book* curr = *head;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = book;
book->prev = curr;
}
}
// 根据书名查找书籍
Book* find_book_by_title(Book* head, char* title) {
Book* curr = head;
while (curr != NULL) {
if (strcmp(curr->title, title) == 0) {
return curr;
}
curr = curr->next;
}
return NULL;
}
// 更新书籍信息
void update_book(Book* book, char* title, char* author, char* publisher, char* ISBN, float price) {
strcpy(book->title, title);
strcpy(book->author, author);
strcpy(book->publisher, publisher);
strcpy(book->ISBN, ISBN);
book->price = price;
}
// 删除书籍
void delete_book(Book** head, Book* book) {
if (*head == book) {
*head = book->next;
} else {
book->prev->next = book->next;
}
if (book->next != NULL) {
book->next->prev = book->prev;
}
free(book);
}
// 打印所有书籍
void print_books(Book* head) {
Book* curr = head;
while (curr != NULL) {
printf("Title: %s\n", curr->title);
printf("Author: %s\n", curr->author);
printf("Publisher: %s\n", curr->publisher);
printf("ISBN: %s\n", curr->ISBN);
printf("Price: %.2f\n\n", curr->price);
curr = curr->next;
}
}
// 测试
int main() {
Book* head = NULL;
add_book(&head, "The Great Gatsby", "F. Scott Fitzgerald", "Charles Scribner's Sons", "9780743273565", 9.99);
add_book(&head, "To Kill a Mockingbird", "Harper Lee", "J. B. Lippincott & Co.", "9780060935467", 7.19);
add_book(&head, "1984", "George Orwell", "Secker and Warburg", "9780451524935", 6.99);
printf("All books:\n");
print_books(head);
Book* book = find_book_by_title(head, "To Kill a Mockingbird");
if (book != NULL) {
printf("Update book:\n");
update_book(book, "To Kill a Mockingbird (Updated)", "Harper Lee", "J. B. Lippincott & Co.", "9780060935467", 8.99);
print_books(head);
printf("Delete book:\n");
delete_book(&head, book);
print_books(head);
} else {
printf("Book not found\n");
}
return 0;
}
```
这个示例实现了以下功能:
- 创建书籍节点的函数 `create_book_node`
- 在链表末尾添加新的书籍的函数 `add_book`
- 根据书名查找书籍的函数 `find_book_by_title`
- 更新书籍信息的函数 `update_book`
- 删除书籍的函数 `delete_book`
- 打印所有书籍的函数 `print_books`
你可以根据自己的需求修改和扩展这个示例。
阅读全文