基于链表的图书信息管理系统c语言代码,至少包括ISBN,书名与定价,实现插入,删除,修改,查找和计数功能
时间: 2024-05-12 21:17:40 浏览: 87
C语言链表实现图书管理系统
好的,我可以回答这个问题。以下是基于链表的图书信息管理系统C语言代码的示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct book {
char isbn[20];
char title[50];
float price;
struct book* next;
} Book;
Book* head = NULL;
void insertBook(char isbn[], char title[], float price) {
Book* new_book = (Book*) malloc(sizeof(Book));
strcpy(new_book->isbn, isbn);
strcpy(new_book->title, title);
new_book->price = price;
new_book->next = head;
head = new_book;
}
void deleteBook(char isbn[]) {
Book* current_book = head;
Book* previous_book = NULL;
while (current_book != NULL) {
if (strcmp(current_book->isbn, isbn) == 0) {
if (previous_book == NULL) {
head = current_book->next;
} else {
previous_book->next = current_book->next;
}
free(current_book);
return;
}
previous_book = current_book;
current_book = current_book->next;
}
}
void modifyBook(char isbn[], char title[], float price) {
Book* current_book = head;
while (current_book != NULL) {
if (strcmp(current_book->isbn, isbn) == 0) {
strcpy(current_book->title, title);
current_book->price = price;
return;
}
current_book = current_book->next;
}
}
void searchBook(char isbn[]) {
Book* current_book = head;
while (current_book != NULL) {
if (strcmp(current_book->isbn, isbn) == 0) {
printf("ISBN: %s\n", current_book->isbn);
printf("Title: %s\n", current_book->title);
printf("Price: %.2f\n", current_book->price);
return;
}
current_book = current_book->next;
}
printf("Book not found.\n");
}
void countBooks() {
int count = 0;
Book* current_book = head;
while (current_book != NULL) {
count++;
current_book = current_book->next;
}
printf("Number of books: %d\n", count);
}
int main() {
insertBook("9780131103627", "The C Programming Language", 52.99);
insertBook("9780132353849", "Effective C++", 35.99);
insertBook("9780201634604", "Design Patterns", 56.99);
searchBook("9780201634604"); // should print information about Design Patterns
deleteBook("9780131103627");
modifyBook("9780132353849", "Effective C++ (3rd edition)", 42.99);
countBooks(); // should print "Number of books: 2"
return 0;
}
```
希望对你有所帮助!如果你有任何其他问题,请继续提问。
阅读全文