使用来链表实现图书信息的录入,排序,插入,查找,删除,修改,统计,浏览
时间: 2024-06-13 14:08:27 浏览: 81
链表功能有建立,排序,插入,删除,输出
以下是使用链表实现图书信息管理的示例代码:
```C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义图书信息结构体
typedef struct Book {
char name[50]; // 书名
char author[20]; // 作者
char publisher[30]; // 出版社
int year; // 出版年份
float price; // 价格
struct Book *next; // 指向下一本书的指针
} Book;
// 定义链表头指针
Book *head = NULL;
// 函数声明
void addBook();
void sortBooks();
void insertBook();
void searchBook();
void deleteBook();
void modifyBook();
void countBooks();
void browseBooks();
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("8. 浏览图书信息\n");
printf("0. 退出系统\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
addBook();
break;
case 2:
sortBooks();
break;
case 3:
insertBook();
break;
case 4:
searchBook();
break;
case 5:
deleteBook();
break;
case 6:
modifyBook();
break;
case 7:
countBooks();
break;
case 8:
browseBooks();
break;
case 0:
printf("感谢使用本系统!\n");
exit(0);
default:
printf("输入有误,请重新输入!\n");
break;
}
}
return 0;
}
// 录入图书信息
void addBook() {
Book *p = (Book *)malloc(sizeof(Book));
printf("请输入书名:");
scanf("%s", p->name);
printf("请输入作者:");
scanf("%s", p->author);
printf("请输入出版社:");
scanf("%s", p->publisher);
printf("请输入出版年份:");
scanf("%d", &p->year);
printf("请输入价格:");
scanf("%f", &p->price);
p->next = NULL;
if (head == NULL) {
head = p;
} else {
Book *q = head;
while (q->next != NULL) {
q = q->next;
}
q->next = p;
}
printf("录入成功!\n");
}
// 排序图书信息
void sortBooks() {
if (head == NULL) {
printf("链表为空!\n");
return;
}
Book *p, *q, *tail = NULL;
while (head != tail) {
p = head;
q = head->next;
while (q != tail) {
if (strcmp(p->name, q->name) > 0) {
char temp[50];
strcpy(temp, p->name);
strcpy(p->name, q->name);
strcpy(q->name, temp);
strcpy(temp, p->author);
strcpy(p->author, q->author);
strcpy(q->author, temp);
strcpy(temp, p->publisher);
strcpy(p->publisher, q->publisher);
strcpy(q->publisher, temp);
int year = p->year;
p->year = q->year;
q->year = year;
float price = p->price;
p->price = q->price;
q->price = price;
}
p = p->next;
q = q->next;
}
tail = p;
}
printf("排序成功!\n");
}
// 插入图书信息
void insertBook() {
if (head == NULL) {
printf("链表为空!\n");
return;
}
Book *p = (Book *)malloc(sizeof(Book));
printf("请输入书名:");
scanf("%s", p->name);
printf("请输入作者:");
scanf("%s", p->author);
printf("请输入出版社:");
scanf("%s", p->publisher);
printf("请输入出版年份:");
scanf("%d", &p->year);
printf("请输入价格:");
scanf("%f", &p->price);
p->next = NULL;
Book *q = head;
while (q != NULL) {
if (strcmp(p->name, q->name) < 0) {
if (q == head) {
p->next = head;
head = p;
} else {
p->next = q;
Book *prev = head;
while (prev->next != q) {
prev = prev->next;
}
prev->next = p;
}
printf("插入成功!\n");
return;
}
q = q->next;
}
q->next = p;
printf("插入成功!\n");
}
// 查找图书信息
void searchBook() {
if (head == NULL) {
printf("链表为空!\n");
return;
}
char name[50];
printf("请输入要查找的书名:");
scanf("%s", name);
Book *p = head;
while (p != NULL) {
if (strcmp(name, p->name) == 0) {
printf("书名:%s\n", p->name);
printf("作者:%s\n", p->author);
printf("出版社:%s\n", p->publisher);
printf("出版年份:%d\n", p->year);
printf("价格:%.2f\n", p->price);
return;
}
p = p->next;
}
printf("未找到该书籍!\n");
}
// 删除图书信息
void deleteBook() {
if (head == NULL) {
printf("链表为空!\n");
return;
}
char name[50];
printf("请输入要删除的书名:");
scanf("%s", name);
Book *p = head, *prev = NULL;
while (p != NULL) {
if (strcmp(name, p->name) == 0) {
if (p == head) {
head = p->next;
} else {
prev->next = p->next;
}
free(p);
printf("删除成功!\n");
return;
}
prev = p;
p = p->next;
}
printf("未找到该书籍!\n");
}
// 修改图书信息
void modifyBook() {
if (head == NULL) {
printf("链表为空!\n");
return;
}
char name[50];
printf("请输入要修改的书名:");
scanf("%s", name);
Book *p = head;
while (p != NULL) {
if (strcmp(name, p->name) == 0) {
printf("请输入新的书名:");
scanf("%s", p->name);
printf("请输入新的作者:");
scanf("%s", p->author);
printf("请输入新的出版社:");
scanf("%s", p->publisher);
printf("请输入新的出版年份:");
scanf("%d", &p->year);
printf("请输入新的价格:");
scanf("%f", &p->price);
printf("修改成功!\n");
return;
}
p = p->next;
}
printf("未找到该书籍!\n");
}
// 统计图书信息
void countBooks() {
if (head == NULL) {
printf("链表为空!\n");
return;
}
int count = 0;
float total = 0;
Book *p = head;
while (p != NULL) {
count++;
total += p->price;
p = p->next;
}
printf("共有%d本书,总价值为%.2f元。\n", count, total);
}
// 浏览图书信息
void browseBooks() {
if (head == NULL) {
printf("链表为空!\n");
return;
}
Book *p = head;
while (p != NULL) {
printf("书名:%s\n", p->name);
printf("作者:%s\n", p->author);
printf("出版社:%s\n", p->publisher);
printf("出版年份:%d\n", p->year);
printf("价格:%.2f\n", p->price);
p = p->next;
}
}
```
阅读全文