In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how the single linked list in the C language to achieve the library management system, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!
The details are as follows
Compared with the management system realized by structure, the book management system realized by single linked list can open up new space at any time and increase the information of books.
Realization of single linked list
First of all, it must be the general operation of printing a single linked list, creating a header, creating a node, inserting a header, deleting a specific location, and printing a linked list.
Struct book {char name [20]; number of float price; int num; / / books}; / / 3 data container-linked list struct Node {struct book data; struct Node*next;}; void printflist (struct Node*headnode); struct Node*headnode = NULL;// create header struct Node*createlisthead () {/ / dynamic memory request struct Node*headnode = (struct Node*) malloc (sizeof (struct Node)) / / basic rules of variables: headnode- > next = NULL; return headnode;} / / create nodes before use to prepare / / change user data into structural variables struct Node* createnewnode (struct book data) {struct Node*newnode = (struct Node*) malloc (sizeof (struct Node)); newnode- > data = data; newnode- > next = NULL; return newnode } / / insert void insertbyhead (struct Node*headnode, struct book data) {struct Node* newnode = createnewnode (data) by header method; / / newnode- > next = headnode- > next; headnode- > next = newnode;} / / delete void deletenodebyname (struct Node*headnode, char * bookname) {struct Node*posleftnode = headnode; struct Node*posnode = headnode- > next at the specified location / / string comparison function while (posnode! = NULL & & strcmp (posnode- > data.name,bookname)) {posleftnode = posnode; posnode = posnode- > next;} / / discussion result if (posnode = = NULL) {printf ("data not found"); return;} else {posleftnode- > next = posnode- > next; free (posnode) Posnode = NULL;} printflist (headnode);} / find books struct Node*searchbyname (struct Node*headnode, char * bookname) {struct Node* posnode = headnode- > next; while (posnode! = NULL & & strcmp (posnode- > data.name, bookname)) {posnode = posnode- > next;} return posnode;} / / print the linked list-print void printflist (struct Node*headnode) {struct Node* pmove = headnode- > next from the second node Printf ("title\ t price\ t quantity\ n"); while (pmoveprices null) {printf ("% s\ t% .1f\ t% d\ n", pmove- > data.name,pmove- > data.price,pmove- > data.num); pmove = pmove- > next;} printf ("\ n");}
Bubble sort-- by price
The first for loop represents the number of iterations, and the second for loop makes two adjacent elements compare and exchange
1 in comparison conditions, only the Q pointer can be used.
2 need to create a temporary variable when exchanging
/ / Bubble sorting algorithm void bubblesortlist (struct Node*headnode) {for (struct Node*p = headnode- > next; p! = NULL; p = p-> next) {for (struct Node*q = headnode- > next; Q-> next! = NULL Q = Q-> next) {if (Q-> data.price > Q-> next- > data.price) {/ / Exchange struct book tempdata = Q-> data; Q-> data = Q-> next- > data; Q-> next- > data = tempdata;}} printflist (headnode);}
If we do not store the information, then every time we close the console after entering the information, the information cannot be retained, so we store the information in the form of files.
File write operation
1 traverse the information in the output file by creating a node pointer variable
2 you can keep the input information through fprintf
/ write operation void savefile (const char*filename, struct Node*headnode) {FILE*fp = fopen (filename, "w"); struct Node*pmove = headnode- > next; while (pmove! = NULL) {fprintf (fp, "% s\ t% .1f\ t% d\ n", pmove- > data.name, pmove- > data.price, pmove- > data.num); pmove = pmove- > next;} fclose (fp); fp = NULL;}
File read operation
1 when opening a file in the form of "r" fails, indicating that there is no such file, it can be opened in the form of "w +". When there is no file, a file will be created.
2 if the read data is inserted into the linked list by header method, the information can be printed again.
/ File read operation void readfile (const char * filename, struct Node*headnode) {FILE*fp = fopen (filename, "r"); if (fp = = NULL) {/ / create fp = fopen (filename, "w+") if the file does not exist;} struct book tempdata While (fscanf (fp, "% s\ t% f\ t% d\ n", tempdata.name, & tempdata.price, & tempdata.num)! = EOF) {insertbyhead (headnode, tempdata);} fclose (fp); fp = NULL;}
Remaining code
1 when looking for a book, use a temporary pointer to accept the pointer that finds the book, and then print the book information
/ / 1 interface void menu () {printf ("- -\ n"); printf ("\ t library management system\ n"); printf ("\ t0. Exit the system\ n "); printf ("\ T1. Register books\ n "); printf ("\ T2. Browse for books\ n "); printf ("\ T3. Borrow books\ n "); printf ("\ T4. Return the books\ n "); printf ("\ t5. Sort books\ n "); printf ("\ t6. Delete books\ n "); printf ("\ t7. Find books\ n "); printf ("--\ n "); printf (" Please enter 0x7\ n ");} / / 2 interact void keydown () {int input = 0; struct book tempbook; / / create temporary variables to store book information struct Node*result = NULL / / create a temporary pointer variable to scanf ("% d", & input); switch (input) {case 0: printf ("[exit]\ n"); printf ("quit successfully\ n"); system ("pause"); exit (0); / / close the entire program break Case 1: printf ("[registration]\ n"); printf ("enter information for books (name,price,num)"); scanf ("% s%f%d", tempbook.name, & tempbook.price, & tempbook.num); insertbyhead (headnode, tempbook); savefile ("book.txt", headnode); break; case 2: printf ("[browse]\ n") Printflist (headnode); break; case 3: printf ("[borrowing]\ n"); / / Books exist, number-1 printf ("Please enter books to borrow"); scanf ("% s", tempbook.name); result = searchbyname (headnode, tempbook.name) If (result = = NULL) {printf ("cannot borrow related books");} else {if (result- > data.num > 0) {result- > data.num--; printf ("borrowed successfully") } else printf ("No inventory");} break; case 4: printf ("[return]\ n"); / / returned by the secretary, quantity + 1 printf ("Please enter books to be returned"); scanf ("% s", tempbook.name); result = searchbyname (headnode, tempbook.name) If (result = = NULL) printf ("illegal source"); else {result- > data.num++; printf ("Book returned successfully!") ;} break; case 5: printf ("[sort]\ n"); bubblesortlist (headnode); savefile ("book.txt", headnode); break; case 6: printf ("[delete]\ n"); printf ("enter the title of the book to delete"); scanf ("% s", tempbook.name) Deletenodebyname (headnode, tempbook.name); savefile ("book.txt", headnode); break; case 7: printf ("[find]\ n"); printf ("Please enter the book you want to find"); scanf ("% s", tempbook.name); result = searchbyname (headnode, tempbook.name) If (result = = NULL) {printf ("relevant information not found!\ n");} else {printf ("title\ t price\ t quantity\ n"); printf ("% s\ t% .1f\ t% d\ t", result- > data.name, result- > data.price, result- > data.num);} break Default: printf ("wrong choice, please reselect: >"); break;}} int main () {headnode = createlisthead (); readfile ("book.txt", headnode); while (1) {menu (); keydown (); system ("pause"); system ("cls");} system ("pause"); return 0 } these are all the contents of the article "how to realize the Library Management system with single linked list in C language". 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!
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.