C language review-pointer linked list and file operations, pointer

Source: Internet
Author: User

C language review-pointer linked list and file operations, pointer

I was just in my sophomore year, preparing to start learning C ++. I practiced the C language I learned in my freshman year, just in time for the task assigned by the teacher. I wrote a C LanguageSales Management SystemAs much as possible to use what you have learned, that is, the structure, pointer, file operations, and data structure. This time, the linked list is added.

Two functions Load_LinkList () and Save_LinkList () are used to combine the linked list and file operations. In addition to the print function, all other functions operate the linked list in the memory, which makes writing more organized, when creating a linked list, instead of using an intermediate variable in the book to guide and insert it to the front of the node, you can directly apply for memory at the next end of the linked list for ease of understanding and operation.

 

/* The first is file inclusion. In this case, a regular header file is not written as ifndef */# include <stdio. h> # include <string. h> # include <windows. h> # include <stdlib. h> # include <conio. h>
/* The next step is the struct * // * Each product corresponds to a node, which is connected by a linked list and written to a file in a unified manner, or read */typedef struct commodity {int data from the file; // The number of data statistics at the header node, and the rest are the product number char name [20]; // The name double price; // The price int count; // The number double sum; // total, the sum of the header nodes is the total struct commodity * next;} * LinkList, LNode;
/* Define global variables for ease of use * // * linked list header pointer */LinkList H = NULL;/* FILE pointer */FILE * fp = NULL;

 

/* Function declaration * // **************** display function **************** /// welcome page void welcome (); // display the menu void menu ();
// Print the table header void printf_header ();
// Display the information of a single node void printf_linklist_info (LinkList pTemp); // delay function void delay (); /***************************** // read from the file to the linked list void Load_LinkList (LinkList H ); // Save the linked list to the file void Save_LinkList (LinkList H ); ********************** /// create the header node void Creat_LinkList (); // Add the node to the end of the linked list LinkList Add_LinkList (LinkList H); // enter the node data void Scanf_LinkList (LinkList pTemp ); // find the required node's precursor LinkList Find_LinkList_Pos (LinkList H, int index); // find the address of the required node LinkList Find_LinkList_Val (LinkList H, char * name ); // Delete the specified node void Del_LinkList (LinkList H, char * name); // modify the node content void Modify_LinkList (LinkList H, int index, int data ); // output void Printf_LinkList (LinkList H) sequentially; // release the memory void Free_LinkList (LinkList H );

 

/* Main function, nothing to say, except for the convenience of using the getch function */int main () {LinkList pTemp = NULL; char name [20] = {0}; system ("color A"); // welcome (); Creat_LinkList (); Load_LinkList (H); while (1) {system ("cls"); menu (); switch (getch () {case '1': pTemp = Add_LinkList (H); Scanf_LinkList (pTemp); getch (); break; case '2': printf_header (); Printf_LinkList (H); getch (); break; case '3': printf ("\ n input name :"); scanf ("% s", name); pTe Mp = Find_LinkList_Val (H, name); printf_linklist_info (pTemp); getch (); break; case '4': printf ("\ n please first enter the name for search :"); scanf ("% s", name); pTemp = Find_LinkList_Val (H, name); Scanf_LinkList (pTemp); getch (); break; case '5 ': printf ("\ n please enter the name first:"); scanf ("% s", name); Del_LinkList (H, name); getch (); break; case '6': Save_LinkList (H); printf ("\ n successfully saved % d messages! \ N ", H-> data); getch (); break; case '0': printf (" \ n welcome to use it next time! \ N "); exit (0); default: printf (" incorrect input! "); Getch () ;}} return 0 ;}

//// // The following shows the function implementation content.

/* Welcome Page. In fact, do not use the */void welcome () {int I; for (I = 1; I <= 100; I ++) {printf ("******************** welcome to use this system ************* ************"); printf ("\ n"); printf ("loading"); printf ("... \ n "); printf (" % 3d % \ n ", I ); printf ("************************************* *********************"); system ("cls");} return ;}

 

/* Menu function */void menu () {system ("cls"); printf ("welcome to the system \ n"); printf ("\ n "); printf ("----------------------------------------------------------------- \ n"); printf ("| 1 add product | \ n"); printf ("| 2 display product | \ n "); printf ("| 3 find product | \ n"); printf ("| 4 modify product | \ n"); printf ("| 5 delete product | \ n "); printf ("| 6 save changes | \ n"); printf ("| 0 exit system | \ n"); printf ("--------------------------------------------------------------- \ n ") ; Printf ("prompt: Save before exiting! "); Printf (" \ nchoose (0-8 ):");}

 

/* Print all product information in a table */void printf_header () {system ("cls"); printf ("--------------------------------------------------- \ n "); printf ("| no | Name | price | quantity | Total | \ n"); printf ("| ---------- | --------------- | ---------------- | ------------ | \ n ");}

 

/* Display Single Node information */void printf_linklist_info (LinkList pTemp) {if (pTemp = NULL) {return;} printf_header (); pTemp-> sum = pTemp-> price * pTemp-> count; printf ("| % 10d | % 15s | % lf | % 20d | % lf | \ n ", pTemp-> data, pTemp-> name, pTemp-> price, pTemp-> count, pTemp-> sum ); printf ("| ---------- | --------------- | ---------------- | ------------ | \ n"); return ;}

 

/* The latency function. After writing it, I found that I always used the getch wait button */void delay () {long int I, j; for (I = 500000; I> 0; I --) {for (j = 0; j <= 2000; j ++ );}}

 

/* Create a header node */void Creat_LinkList () {H = (LinkList) malloc (sizeof (LNode); if (H) {H-> next = NULL; h-> data = 0;} return ;}

 

/* Add a node. Here, the memory is directly applied for by the next node of the last node */LinkList Add_LinkList (LinkList H) {LinkList q = H; while (q-> next! = NULL) q = q-> next; q-> next = (LinkList) malloc (sizeof (LNode); q-> next-> sum = 0; q-> next = NULL; H-> data ++; return q-> next ;}

 

/* Enter the node information when adding the node, or enter */void Scanf_LinkList (LinkList pTemp) {if (pTemp = NULL) {return ;} printf ("\ n input number:"); scanf ("% d", & pTemp-> data); printf ("input name :"); scanf ("% s", pTemp-> name); printf ("input price:"); scanf ("% lf", & pTemp-> price ); printf ("input quantity:"); scanf ("% d", & pTemp-> count); pTemp-> sum = pTemp-> price * pTemp-> count ;}

 

/* Read from the file and load it to the linked list. Like the Save function, it is the two most critical functions */void Load_LinkList (LinkList H) {LinkList p = NULL, pTemp = NULL; pTemp = (LinkList) malloc (sizeof (LNode); pTemp-> next = NULL; fp = fopen ("D:/a.txt", "rb "); while (1) {/* here an intermediate node is used for temporary storage. fread reads the node once to determine whether to add the node. It will be wrong to add the node directly with p, when a file is empty, there will be an extra node for storing the garbage value, and fread must have a piece of memory to read */if (fread (pTemp, sizeof (LNode), 1, fp ))! = 0) {p = Add_LinkList (H); p-> data = pTemp-> data; strcpy (p-> name, pTemp-> name ); p-> price = pTemp-> price; p-> count = pTemp-> count; H-> data ++;} else break;} free (pTemp ); fclose (fp); return ;}

 

/* Save the linked list to the file */void Save_LinkList (LinkList H) {LinkList p = H-> next; if (p = NULL) {/* clear it here, if there is data stored in the linked list, fwrite cannot be used after the call is deleted, but in this case, clear the file with wb */fp = fopen ("D:/a.txt ", "wb"); H-> data = 0; fclose (fp); getch (); return;} fp = fopen ("D:/a.txt", "wb "); while (p! = NULL) {fwrite (p, sizeof (LNode), 1, fp); p = p-> next;} fclose (fp); return ;}

 

///// // The subsequent function is to operate the linked list only in the memory.

/* Through location search, the address of the node's front node is returned. The plan is to find the node. Later, I didn't want to use the menu, so I didn't move it here */LinkList Find_LinkList_Pos (LinkList H, int index) {LinkList p = H; index --; while (index --) {p = p-> next;} return p ;}

 

/* Search by string matching and return the Node Address */LinkList Find_LinkList_Val (LinkList H, char * name) {LinkList p = H; while (strcmp (p-> name, name )! = 0 & p-> next! = NULL) {p = p-> next;} if (p-> next = NULL) {printf ("This item is not available! "); Return NULL; getch ();} return p ;}

 

/* Delete a node. pre is the precursor node and p is the node to be searched */void Del_LinkList (LinkList H, char * name) {int flag = 0; linkList p = H-> next, pre = H; while (p! = NULL) {if (strcmp (p-> name, name) = 0) {flag = 1; break;} pre = p; p = p-> next ;} if (flag = 0) {printf ("This item is not available! \ N "); return;} p = pre-> next; pre-> next = p-> next; free (p); return ;}

 

/* You can also modify the node information. Like Find_LinkList_Pos (), you have never used */void Modify_LinkList (LinkList H, int index, int data) {LinkList p = Find_LinkList_Pos (H, index + 1); if (p) p-> data = data; return ;}

 

/* Ordered output. The table is not too neat. It should be like % xd. I don't want to change it later */void Printf_LinkList (LinkList H) {LinkList p = NULL; p = H-> next; if (p = NULL) {printf ("no products currently! "); Getch (); return;} while (p! = NULL) {p-> sum = p-> price * p-> count; printf ("| % 10d | % 15s | % lf | % 15d | % lf | \ n", p-> data, p-> name, p-> price, p-> count, p-> sum); printf ("bytes \ n"); p = p-> next;} printf ("% lf \ n ", h-> sum); return ;}

 

/* The memory is always to be released */void Free_LinkList (LinkList H) {LinkList pre = NULL, p = H-> next; while (pre! = NULL) {pre = p-> next; free (p); p = p-> next;} H-> next = NULL; return ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.