How to use linked list in C++ to realize Book Management system
This article mainly shows you "how C++ uses linked lists to achieve a library management system". The content is simple and clear. I hope it can help you solve your doubts. Next, let the editor lead you to study and learn this article "how C++ uses linked lists to achieve a library management system".
The details are as follows
First, the program realizes the function
1. Input books: input books into the library management system
two。 Browse books: view all the books in the library management system
3. Borrow books: books can be borrowed when they exist, inventory-1, but books cannot be borrowed if they are not in stock.
4. Return the book: stock + 1, if the book is not a book in the library, it cannot be entered
5. Delete a book: delete the book from the library management system based on the title
6. Find books: find books by title and display basic information about books
7. Sort books: sort books by price (descending)
II. Requirements
Written using functions, pointers, and linked lists.
III. Program function diagram
IV. Concrete function
Program code # include#include#include struct bookinfo {char name [20]; / title char author [10]; / / author char date [20]; / / publication date float price; / / Price int num; / / quantity}; struct Node {struct bookinfo data; struct Node* next;}; / * Global linked list * / struct Node* list = NULL / * create header * / struct Node* createhead () {/ * dynamic memory request * / struct Node* headNode = (struct Node*) malloc (sizeof (struct Node)); headNode- > next = NULL; return headNode;} / * create node * / struct Node* createNode (struct bookinfo data) {struct Node* newNode = (struct Node*) malloc (sizeof (struct Node)); newNode- > data = data; newNode- > next = NULL; return newNode;} void printList () Void display_menu (); void savebookfile (); void insertbook (); void readbookfile (); void deletebook (); struct Node* searchbook (); void sortbook (); void selectkey (); / * print linked list * / void printList (struct Node* headNode) {struct Node* Bmove = headNode- > next; printf ("title\ t author\ t publication date\ t Price\ t inventory\ n") While (Bmove! = NULL) {printf ("% s\ t% .1F\ t% d\ n", Bmove- > data.name,Bmove- > data.author,Bmove- > data.date,Bmove- > data.price,Bmove- > data.num); Bmove = Bmove- > next;}} / * menu login interface * / void display_menu () {char str (); FILE * fp; char * txt Fp = fopen ("menu.txt", "r"); txt = fgets (str,100,fp); while (txt! = NULL) {printf ("% s", str); txt = fgets (str,100,fp);} fclose (fp);} / * Save information in file * / void savebookfile (const char* filename,struct Node* headNode) {FILE* fp = fopen (filename, "w"); struct Node* Bmove = headNode- > next While (Bmove! = NULL) {fprintf (fp, "% s\ t% .1f\ t% d\ n", Bmove- > data.name,Bmove- > data.author,Bmove- > data.date,Bmove- > data.price,Bmove- > data.num); Bmove = Bmove- > next;} fclose (fp);} / * enter books * / void insertbook (struct Node* headNode,struct bookinfo data) {struct Node* newNode = createNode (data) NewNode- > next = headNode- > next; headNode- > next = newNode;} / * read file * / void readbookfile (const char* filename, struct Node* headNode) {FILE* fp = fopen (filename, "r"); if (fp = = NULL) {fp = fopen (filename, "w +");} struct bookinfo tempinfo While (fscanf (fp, "% s\ t% .1F\ t% d\ n", tempinfo.name,&tempinfo.author,&tempinfo.date,&tempinfo.price,&tempinfo.num)! = EOF) {insertbook (list,tempinfo);} fclose (fp);} / * Delete books * / void deletebook (struct Node* headNode,char * bookname) {struct Node* leftNode = headNode; struct Node* rightNode = headNode- > next While (rightNode! = NULL & & strcmp (rightNode- > data.name,bookname)) {leftNode = rightNode; rightNode = leftNode- > next;} if (leftNode = = NULL) {return;} else {printf ("Book deleted successfully! \ n "); leftNode- > next = rightNode- > next; free (rightNode); rightNode = NULL;}} / * find books * / struct Node* searchbook (struct Node* headNode, char* bookname) {struct Node* rightNode = headNode- > next; while (rightNode! = NULL & & strcmp (rightNode- > data.name, bookname)) {rightNode = rightNode- > next;} return rightNode } / * sort books * / void sortbook (struct Node* headNode) {for (struct Node* I = headNode- > next; I! = NULL; I = I-> next) {for (struct Node* j = headNode- > next;j- > next! = NULL;j = j-> next) {/ * sort books (in descending order by price) * / if (j-> data.price)
< j->Next- > data.price) {/ * Exchange value * / struct bookinfo tempdata = j-> data; j-> data = j-> next- > data; j-> next- > data = tempdata;} / * View results after sorting * / printList (headNode) } / * Interactive interface * / void selectkey () {int userkey = 0; struct bookinfo tempbook; / / generate a temporary variable to store the book information struct Node* searchname = NULL; / / generate a temporary variable to store the title of the book to be found struct Node* borrowbook = NULL; / / generate a temporary variable to store the title of the book to be borrowed struct Node* returnbook = NULL / / generate a temporary variable to store the title of the book to be returned scanf ("% d", & userkey); switch (userkey) {case 1: printf ("[enter book]\ n"); printf ("enter information about the book (name,author,date,price,num):") Scanf ("% s%s%s%f%d", tempbook.name,&tempbook.author,&tempbook.date,&tempbook.price,&tempbook.num); insertbook (list,tempbook); / * Save book information to a booksinfo text file * / savebookfile ("bookinfo.txt", list); break; case 2: printf ("[browse books]\ n"); printList (list) Break; case 3: printf ("[borrowing books]\ n"); / * books can be borrowed, stock-1, books cannot be borrowed if they are not in stock * / printf ("Please enter the title of the book to borrow:"); scanf ("% s", tempbook.name); borrowbook = searchbook (list,tempbook.name) If (borrowbook = = NULL) {printf ("the book does not exist and cannot be borrowed!" Else {if (borrowbook- > data.num > 0) {borrowbook- > data.num--; printf ("loan successful! \ n "); printList (list);} else {printf (" the current book stock is insufficient, the loan failed! " \ n ");}} break; case 4: printf (" [return books]\ n "); / / inventory + 1 printf (" Please enter the title of the book to be returned: "); scanf ("% s ", tempbook.name); returnbook = searchbook (list,tempbook.name) If (returnbook = = NULL) {printf ("this book is not a book in the library! \ n ");} else {returnbook- > data.num++; printf (" Book returned successfully! \ n "); printList (list);} break; case 5: printf (" [delete the book]\ n "); printf (" Please enter the title of the book to delete: "); scanf ("% s ", tempbook.name); deletebook (list,tempbook.name); / * Delete the book by title * / printList (list); break Case 6: printf ("[find Book]\ n"); printf ("Please enter the title of the book you want to query:"); scanf ("% s", tempbook.name); searchname = searchbook (list,tempbook.name); if (searchname = = NULL) {printf ("the book does not exist, please purchase! Else {/ * output information about the book * / printf ("title\ t author\ t publication date\ t Price\ t inventory\ n") Printf ("% s\ t% .1F\ t% d\ n", searchname- > data.name,searchname- > data.author,searchname- > data.date,searchname- > data.price,searchname- > data.num);} break; case 7: printf ("[sort books]\ n"); / * sort by price (descending order) * / sortbook (list); break Case 8: printf ("[exit the system]\ n"); printf ("exit successfully\ n"); system ("pause"); exit (0); / * close the entire program * / break; default: printf ("[error]\ n"); break } / * main interface * / int main () {list = createhead (); readbookfile ("bookinfo.txt", list); while (1) {display_menu (); selectkey (); system ("pause"); system ("cls");} system ("pause"); return 0;} VI.
1. Enter books
two。 Browse for books
3. Borrow books
When the book exists, the loan is successful, inventory-1:
When the book does not exist, it cannot be borrowed:
4. Return books
When the book does not exist in the library management system, the return fails:
5. Find books
When the book does not exist, the search fails:
6. Sort books
Re-enter the book:
Sort (descending):
7. Delete a book
These are all the contents of the article "how C++ uses linked lists to achieve a library management system". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!