使用链式结构写图书管理系统
时间: 2023-05-20 14:02:36 浏览: 59
非常感谢您的提问。以下是使用链式结构写图书管理系统的示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Book {
char title[50];
char author[50];
int year;
struct Book* next;
} Book;
Book* create_book(char* title, char* author, int year) {
Book* book = (Book*)malloc(sizeof(Book));
strcpy(book->title, title);
strcpy(book->author, author);
book->year = year;
book->next = NULL;
return book;
}
void add_book(Book** head, Book* book) {
if (*head == NULL) {
*head = book;
} else {
Book* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = book;
}
}
void remove_book(Book** head, char* title) {
Book* current = *head;
Book* previous = NULL;
while (current != NULL) {
if (strcmp(current->title, title) == 0) {
if (previous == NULL) {
*head = current->next;
} else {
previous->next = current->next;
}
free(current);
return;
}
previous = current;
current = current->next;
}
}
void print_books(Book* head) {
Book* current = head;
while (current != NULL) {
printf("%s by %s (%d)\n", current->title, current->author, current->year);
current = current->next;
}
}
int main() {
Book* head = NULL;
add_book(&head, create_book("The Great Gatsby", "F. Scott Fitzgerald", 1925));
add_book(&head, create_book("To Kill a Mockingbird", "Harper Lee", 1960));
add_book(&head, create_book("1984", "George Orwell", 1949));
print_books(head);
remove_book(&head, "To Kill a Mockingbird");
print_books(head);
return 0;
}
```
希望这个示例代码能够帮助您实现图书管理系统。如果您有任何问题,请随时问我。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)