In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how C++ uses linked lists to achieve a book information management system. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
C++ uses linked lists to implement a simple book information management system, the specific contents are as follows
(1) Book information includes ISBN number, title, author's name, price and quantity, etc.
(2) the main functions of the system include: creating book information, outputting book information, querying book information, adding book information and deleting book information.
# include # include # include / / create the structure and its member typedef struct Node {int num;// number char name [20]; / / title char author [20]; / / author int isexsit;// quantity float price;// Price struct Node * next;// pointer Domain} Slocket / structure defined as Sstroke / function definition void choose (); void menu (); / menu function S * create () / / create linked list function void print (S *); / / output linked list function void pop_sort (S *); / / sort void insert (S *); / / insert node function void del (S *); / / delete node function void search2 (S *); / / title lookup node function void search3 (S *); / / author lookup node function void search4 (S *); / / numbered lookup void mod (S *) / / modify the book information / / the main function int main () {choose ();} void choose () {S * head; int n, a = 1 is used to control the selected operation type, a controls the loop, terminates the while (a > 0) {menu () with-1; / / displays the menu printf ("choose the function you want to use:"); scanf ("% d", & n) / / Select the operation switch (n) / / each Operand number corresponds to the menu number, and determine the operation type {case 1 create / create head = create () by n; break; case 2 head hand / output printf ("Book Information is (sorted by price)\ n") Pop_sort (head); printf ("No.\ t title\ t author\ t quantity\ t Price\ n"); print (head); break; case 3 head / insert insert (head); printf ("after insertion\ n") Printf ("No.\ t title\ t author\ t quantity\ t Price\ n"); print (head); break; case 4head / delete del (head); printf ("after deletion\ n") Printf ("No.\ t title\ t author\ t quantity\ t Price\ n"); print (head); break; case 5 head / title search search2 (head); break; case 6 head hand / author search search3 (head) Break; case 7 head / ID find search4 (head); break; case 8 head / modify mod (head); pop_sort (head); printf ("Book Information is\ n") Printf ("serial number\ t title\ t author\ t quantity\ t price\ n"); print (head); break; default: a =-1 domestic break / out-of-loop condition The menu module directly displays void menu () {printf ("\ n\ n"); printf ("\ t\ t Welcome to the Library Management system\ n"); printf ("\ t\ t |-SCORE- |\ n"); printf ("\ t\ t |\ T1. Create a book |\ n "); printf ("\ t\ t |\ T2. Display book information |\ n "); printf ("\ t\ t |\ T3. Add book information |\ n "); printf ("\ t\ t |\ t4. Delete book |\ n "); printf ("\ t\ t |\ T5. Search by book name |\ n "); printf ("\ t\ t |\ T6. Search by author name |\ n "); printf ("\ t\ t |\ t7. Search by book number |\ n "); printf ("\ t\ t |\ t8. Modify book information |\ n "); printf ("\ t\ t |\ t9. Exit the program |\ n "); printf ("\ t\ t |-- |\ n "); printf ("\ t\ t\ tchoice (1-9):\ n ");} / / create linked list module S * create () {S * head, * p, * Q head / define pointer int I Head = (S *) malloc (sizeof (S)); / / header node opening space head- > next = NULL;// short node pointer field Q = head;//q pointer record head node address p = head- > next;//p pointer record head node pointer domain address printf ("Please enter the book number, book name, author, book quantity, price, and finally enter 0 end\ n") Int num; scanf ("% d", & num); while (num! = 0) / / enter the book number as zero to stop the loop {p = (S *) malloc (sizeof (S)); / / p pointer open space / / enter members p-> num = num Scanf ("% s% s% d% f", p-> name, p-> author, & p-> isexsit, & p-> price); p-> next = NULL;// the pointer field of the p node Q-> next = pincushion p node connection Q = p / / Q pointer moved back to printf ("Please enter book number, book name, author, book quantity, price, and finally enter 0 end\ n"); scanf ("% d", & num);} return head;// returns the starting address of the linked list} / / insert node module (multiple inserts) void insert (S * head) {int I, num, flag = 1 / / the flag implementation determines whether the pointer reaches the last node S * p, * Q, * r; / / defines the pointer to facilitate the insertion operation printf ("Please enter the information of a book:\ n"); printf ("Please enter the book number and enter 0 ending\ n"); scanf ("% d", & num) While (num! = 0) / / the input number is not a 00:00 cycle and ends with zero, and multiple insertions of {r = (S *) malloc (sizeof (S)) can be achieved; / / Open up space for r r-> next = NULL;// pointer field r-> num = num; printf ("Please enter book name, author, number of books, book price\ n") Scanf ("% s% s% d% f", r-> name, r-> author, & r-> isexsit, & r-> price); Q = head;//q pointer recording header node address p = head- > next;//p pointer recording header node pointer domain address while (Q-> next! = NULL & p-> price
< r->Price) / / Loop condition: when Q-> next is not empty, and insert {p = p-> next;//p pointer backward Q = Q-> next by price / / if (Q-> next = = NULL) / / this judgment prevents Q-> next from being empty, and when executing the loop, there is a wild pointer that causes the program to make an error {p = NULL;// to prevent wild pointer p Q-> next = r bank / connection node r-> next = NULL / / empty the r pointer field flag = 0umbram / arrive at the last node change flag break;}} if (flag) / / determine whether the last node is reached or not, and perform the operation {r-> next = p; Q-> next = r if it is true / / implement inserting r nodes into the linked list} printf ("Please enter the book number, enter the end of 0\ n"); scanf ("% d", & num);} / delete node module void del (S * head) {S * p, * QSpac / define pointer int b / for entering the number to find and delete p = head / / the address of the recording header node Q = head- > the address of the pointer domain of the next;//q recording header node printf ("Please enter the book number you want to delete:"); / / enter the number scanf ("% d", & b) Execute loop {if (Q-> num = = b) / / when while (Q! = NULL) / / determine whether the input number / / is true {p-> next = Q-> next;// disconnects Q node free (Q); / / release Q node neicun Q = NULL / / set null Q pointer to prevent wild pointer} else {/ / when p = p-> next;//p pointer is judged to be false, Q = Q-> next / / Q pointer moves back}} if (p = = NULL) / / output input error printf ("input error\ n") when the last node has not been found the number to be deleted;} / / title lookup module void search2 (S * head) {S * head / define pointer char name1 [20] / / define name1 for entering the title of the search book printf ("Please enter the title of the book you want to search:"); / / enter the title of the search book scanf ("% s", name1); p = head- > next While (p! = NULL) {if (strcmp (p-> name, name1) = = 0) / / output information printf ("book information\ n") when determining whether a book {/ / is true; printf ("No.\ t title\ t author\ t number\ t Price\ n") Printf ("% d\ t% s\ t% s\ t% d\ t% .2f\ n", p-> num, p-> name, p-> author, p-> isexsit, p-> price); break;} else / / is false p = p-> next / / move the pointer back} if (p = = NULL) / / output input error printf ("input error\ n") when the last node has not found the desired number;} / / author search module void search3 (S * head) {S * pash / define pointer char name2 [20]; / / define the name2 to be used to input the search book printf ("enter the author you want to query:") / / input to find the author scanf ("% s", name2); p = head- > next; while (p! = NULL) {if (strcmp (p-> author, name2) = = 0) / / output information printf ("Book Information\ n") to determine whether a book is found {/ / is true Printf ("serial number\ t title\ t author\ t quantity\ t price\ n"); printf ("% d\ t% s\ t% s\ t% d\ t% .2f\ n", p-> num, p-> name, p-> author, p-> isexsit, p-> price); break;} else / / if false p = p-> next / / move the pointer back} if (p = = NULL) / / output input error printf ("input error\ n") when the last node has not found the desired number;} / / number lookup void search4 (S * head) {S * pash / define pointer int num1;// definition num1 is used to enter the search book printf ("Please enter the book number you want to search:") / / enter the lookup number scanf ("% d", & num1); p = head- > next; while (p! = NULL) {if (p-> num = = num1) / / output information printf ("Book Information\ n") when determining whether the book {/ / is true. Printf ("serial number\ t title\ t author\ t quantity\ t price\ n"); printf ("% d\ t% s\ t% s\ t% d\ t% .2f\ n", p-> num, p-> name, p-> author, p-> isexsit, p-> price); break;} else / / if false p = p-> next / / move the pointer back} if (p = = NULL) / / output ERROR INPUT printf ("input error\ n") when the last node has not found the desired number;} / / modify the information module void mod (S * head) {S * pash / define pointer int num1, num2, isexsit1;// definition num1 is used to input and find book modification information, num2 is used to modify char name1 [20], author1 [20] Float price1; printf ("Please enter the book number you want to modify:"); / / enter the book number scanf ("% d", & num1); p = head- > next While (p! = NULL) {if (p-> num = = num1) / / determine whether the book {printf is found ("Please enter the book number, title, author, quantity, price\ n") / / if true, re-enter the book information scanf ("% d% s% s% d% f", & num2, name1, author1, & isexsit1, & price1); p-> num = num2; strcpy (p-> name, name1); strcpy (p-> author, author1); p-> isexsit = isexsit1; p-> price = price1; break When} else / / is false, p = p-> next;// pointer moves back} if (p = = NULL) / / output input error printf ("input error\ n") when the last node has not yet found the desired number } void pop_sort (S * head) / / Bubble sort of linked list {/ / the pointer value of head- > next is not modified, but the value S * pre, * p, * tail, * temp; tail = NULL; pre = head of pointer content is changed. While ((head- > next- > next)! = tail) / / (head- > next)! = tail is also applicable. Compare {p = head- > next; pre = head; while (p-> next! = tail) {if ((p-> price) > (p-> next- > price)) {pre- > next = p-> next / / switch node method temp = p-> next- > next; p-> next- > next = p; p-> next = temp; p = pre- > next; / / p fallback one node} p = p-> next; / / p forward one node pre = pre- > next } tail = p;}} / output linked list module void print (S * head) {int i; S * p = head- > next; while (p) / execute {printf ("% d\ t% s\ t% s\ t% d\ t% .2f\ n", p-> num, p-> name, p-> author, p-> isexsit, p-> price) when p is not empty Printf ("\ n"); p = p-> next;// pointer moves back}} this is the end of sharing about how C++ uses linked lists to achieve the library information management system. I hope the above content can be of some help and can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.