C语言数据结构图书管理系统
时间: 2024-01-12 09:04:02 浏览: 105
C语言数据结构图书管理系统是一个基于结构体的动态链表的程序,分为管理员操作和会员操作两个界面。管理员操作界面可以进行图书的收录和删除等操作,会员操作界面可以进行注册、借书和还书等操作。
以下是一个简单的C语言数据结构图书管理系统的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义图书结构体
typedef struct Book {
char title[100];
char author[100];
int year;
struct Book* next;
} Book;
// 全局变量,指向图书链表的头节点
Book* head = NULL;
// 函数声明
void addBook();void deleteBook();
void displayBooks();
void freeMemory();
int main() {
int choice;
while (1) {
printf("图书管理系统\n");
printf("1. 添加图书\n"); printf("2. 删除图书\n");
printf("3. 显示图书\n");
printf("4. 退出\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
displayBooks();
break;
case 4:
freeMemory();
exit(0);
default:
printf("无效的选择!\n");
}
}
return 0;
}
// 添加图书
void addBook() {
Book* newBook = (Book*)malloc(sizeof(Book));
printf("请输入图书标题:");
scanf("%s", newBook->title);
printf("请输入图书作者:");
scanf("%s", newBook->author);
printf("请输入图书出版年份:");
scanf("%d", &(newBook->year));
newBook->next = head;
head = newBook;
printf("图书添加成功!\n");
}
// 删除图书
void deleteBook() {
if (head == NULL) {
printf("图书馆为空!\n");
return;
}
char title[100];
printf("请输入要删除的图书标题:");
scanf("%s", 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);
printf("图书删除成功!\n");
return;
}
previous = current;
current = current->next;
}
printf("未找到要删除的图书!\n");
}
// 显示图书
void displayBooks() {
if (head == NULL) {
printf("图书馆为空!\n");
return;
}
Book* current = head;
while (current != NULL) {
printf("标题:%s\n", current->title);
printf("作者:%s\n", current->author);
printf("出版年份:%d\n", current->year);
printf("--\n");
current = current->next;
}
}
// 释放内存
void freeMemory() {
Book* current = head;
Book* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
}
```
阅读全文