C++利用链表实现一个简单的图书信息管理系统,供大家参考,具体内容如下(1)图书信息包括ISBN号、书名、作者名、价格和数量等;(2)系统的主要功能包括:图书信息的创建。

#include 
using namespace std;
#define nullptr NULL

// 定义图书信息结构体
struct Book {
    string isbn;
    string title;
    string author;
    double price;
    int quantity;
    Book* next;
};

// 添加图书信息
void addBook(Book*& head, Book* newBook) {
    if (head == nullptr) {
        head = newBook;
    } else {
        Book* current = head;
        while (current->next != nullptr) {
            current = current->next;
        }
        current->next = newBook;
    }
}

// 输出图书信息
void displayBooks(Book* head) {
    if (head == nullptr) {
        cout << "图书信息为空" << endl;
    } else {
        Book* current = head;
        while (current != nullptr) {
            cout << "ISBN: " << current->isbn << endl;
            cout << "书名: " << current->title << endl;
            cout << "作者名: " << current->author << endl;
            cout << "价格: " << current->price << endl;
            cout << "数量: " << current->quantity << endl;
            cout << "=================" << endl;
            current = current->next;
        }
    }
}

// 查询图书信息
void searchBook(Book* head, string bookTitle) {
    if (head == nullptr) {
        cout << "图书信息为空" << endl;
    } else {
        Book* current = head;
        bool found = false;
        while (current != nullptr) {
            if (current->title == bookTitle) {
                cout << "ISBN: " << current->isbn << endl;
                cout << "书名: " << current->title << endl;
                cout << "作者名: " << current->author << endl;
                cout << "价格: " << current->price << endl;
                cout << "数量: " << current->quantity << endl;
                found = true;
            }
            current = current->next;
        }

        if (!found) {
            cout << "未找到该图书" << endl;
        }
    }
}

// 删除图书信息
void deleteBook(Book*& head, string bookTitle) {
    if (head == nullptr) {
        cout << "图书信息为空" << endl;
    } else {
        Book* current = head;
        Book* previous = nullptr;
        bool found = false;
        while (current != nullptr) {
            if (current->title == bookTitle) {
                if (previous == nullptr) {
                    head = current->next;
                } else {
                    previous->next = current->next;
                }
                delete current;
                found = true;
                cout << bookTitle << "已删除" << endl;
                break;
            }
            previous = current;
            current = current->next;
        }

        if (!found) {
            cout << "未找到该图书" << endl;
        }
    }
}

// 主函数
int main() {
    Book* head = nullptr;  // 头节点指针

    // 创建图书信息
    Book* book1 = new Book{"978-7-121-30918-5", "C++ Primer Plus", "Stephen Prata", 59.9, 10};
    Book* book2 = new Book{"978-7-111-56320-5", "C++ Primer", "Stanley B. Lippman", 69.9, 5};
    Book* book3 = new Book{"978-7-302-40865-1", "Effective Modern C++", "Scott Meyers", 89.9, 3};

    // 添加图书信息到链表
    addBook(head, book1);
    addBook(head, book2);
    addBook(head, book3);

    // 输出图书信息
    displayBooks(head);

    // 查询图书信息
    searchBook(head, "C++ Primer");

    // 删除图书信息
    deleteBook(head, "C++ Primer");

    // 再次输出图书信息
    displayBooks(head);

    // 释放内存
    Book* current = head;
    Book* next = nullptr;
    while (current != nullptr) {
        next = current->next;
        delete current;
        current = next;
    }

    return 0;
}

C++利用链表实现一个简单的图书信息管理系统,供大家参考,具体内容如下(1)图书信息包括ISBN号、书名、作者名、价格和数量等;(2)系统的主要功能包括:图书信息的创建。_第1张图片

你可能感兴趣的:(C++,c++,链表,开发语言)