C语言实现利用链表实现一个简单的图书信息管理系统
时间: 2023-12-15 22:07:12 浏览: 26
好的,这是一个简单的图书信息管理系统,使用链表实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct book {
char title[50];
char author[50];
int pages;
struct book *next;
};
void add_book(struct book **library, char title[], char author[], int pages) {
struct book *new_book = (struct book*) malloc(sizeof(struct book));
strcpy(new_book->title, title);
strcpy(new_book->author, author);
new_book->pages = pages;
new_book->next = NULL;
if (*library == NULL) {
*library = new_book;
} else {
struct book *current_book = *library;
while (current_book->next != NULL) {
current_book = current_book->next;
}
current_book->next = new_book;
}
}
void print_library(struct book *library) {
struct book *current_book = library;
while (current_book != NULL) {
printf("Title: %s\n", current_book->title);
printf("Author: %s\n", current_book->author);
printf("Pages: %d\n\n", current_book->pages);
current_book = current_book->next;
}
}
int main() {
struct book *library = NULL;
add_book(&library, "The Great Gatsby", "F. Scott Fitzgerald", 180);
add_book(&library, "To Kill a Mockingbird", "Harper Lee", 281);
add_book(&library, "1984", "George Orwell", 328);
print_library(library);
return 0;
}
```
这个程序定义了一个 `book` 结构体来表示每一本书,包括标题、作者和页数。然后定义了一个 `add_book` 函数来添加一本新书到图书馆链表中,以及一个 `print_library` 函数来打印整个图书馆。在 `main` 函数中,我们添加了三本书到图书馆,然后打印整个图书馆。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)