// List1.cpp: defines the entry point for the console application. /// * Basic list1.cpp ------------------------------------------------------- Author: Software Engineering @ hit 1093710210 Alex time: 2010.9.10 ------------------------------------- * // a small exercise left by Dean Huang hujie in the data structure basics and algorithms class... I haven't written it for several days, and it doesn't feel too difficult. I didn't expect it to make myself perfect, I have to practice more. // I have learned a lot from the process of writing a linked list .. Do not trust networks .. It feels messy .. Prepare a white paper and a pen .. I have drawn myself to understand ==# include "stdafx. H "# include" stdio. H "# include" stdlib. H "struct list {int data; struct list * Next;}; void menu (); struct list * Create (struct list * H ); // create and insert the ascending linked list void display (struct list * H); // output the linked list information struct list * delete_all (struct list * H ); // Delete the entire chain table struct list * Delete (struct list * H, int xdata); // Delete the node struct list * search (struct list * H, int xdata) in the chain table ); // find the location of a node. In this example, the address struct list * reve is returned. RS (struct list * H); // implement the reverse int main (INT argc, char * argv []) of the linked list {int choice, I = 0; struct list * position, * mylist = NULL; int xdata; menu (); While (1) {printf ("Enter the operation:"); scanf ("% d", & choice ); switch (choice) {Case 1: printf ("Please input the node:/N"); mylist = create (mylist); break; Case 2: printf ("Please input the node you want to delete:/N"); scanf ("% d", & xdata); mylist = Delete (mylist, xdata); Break; Case 3: printf ("Please input the node you want to locate:/N"); scanf ("% d", & xdata); position = search (mylist, xdata); printf ("the node position is % d", position); break; Case 4: Display (mylist); break; Case 5: mylist = revers (mylist ); printf ("the links reversed is:/N"); display (mylist); break; Case 6: mylist = delete_all (mylist); printf ("link deleted! /N "); break; default: printf (" wrong! ") ;}} Return 0;} void menu () {printf (" one-way ascending linked list operation/N "); printf (" ---------------------------------------/N "); printf ("1-> Create an ascending linked list or insert a node in the ascending linked list/N"); printf ("2-> delete a node/n" in the linked list "); printf ("3-> return address pointer of a node/N"); printf ("4-> Output Linear Linked List/N "); printf ("5-> implement unidirectional linked list reverse/N"); printf ("6-> Delete the entire linear table/N");}/* function: input Function Parameters of the linked list in ascending order: struct pointer return value: struct pointer */struct list * Create (struct list * h) {struct list * newpr = NULL; struct list * TEM P = H; struct list * flag = NULL; int data; newpr = (struct list *) malloc (sizeof (struct list); If (newpr = NULL) // used to check whether the dynamic space is successfully applied {printf ("memory error"); exit (0) ;}scanf ("% d", & data ); newpr-> DATA = data; newpr-> next = NULL; If (H = NULL) H = newpr; else {While (temp-> next! = NULL & temp-> data <= data) // used by the instructor for class <, I think it should still be used <= to solve the problem of invalid duplicate input with the same data {flag = temp; // flag is used to record the appropriate position before the insertion point temp = temp-> next;} If (temp-> DATA> data) // It is used to check whether it is between two values, it can be seen whether the end of the linked list {If (temp = h) // indicates that data is inserted before the header to generate a new header {newpr-> next = h; H = newpr;} else {temp = flag; // Move the current position forward to newpr-> next = temp-> next; temp-> next = newpr ;}} else // insert {temp-> next = newpr ;}} return H ;}/ * function: output line Sexual table function parameters: struct pointer return value: void */void display (struct list * h) {struct list * P = H; int counter = 1; printf ("position: data/N "); While (P! = NULL) {printf ("% d", counter); printf (": % d/N", p-> data); counter ++; P = p-> next;}/* function: delete a node. function parameter: struct pointer. Delete the int value of the node data: struct pointer */struct list * Delete (struct list * H, int xdata) {struct list * temp = H; struct list * flag = H; If (H = NULL) {printf ("no link! "); Return (h);} while (temp-> data! = Xdata & temp-> next! = NULL) {flag = temp; temp = temp-> next;} If (xdata = temp-> data) // check whether the corresponding node {If (temp = h) has been found at the end. // The first node is {H = temp-> next ;} else {flag-> next = temp-> next;} Free (temp);} else printf ("no node! /N "); Return h;}/* function: return the address function parameter of a node: struct pointer. You need to find the int value of the node data: struct pointer */struct list * search (struct list * H, int xdata) {struct list * temp = H; If (H = NULL) {printf ("no link! "); Return (h);} while (temp-> data! = Xdata & temp-> next! = NULL) {temp = temp-> next;} If (xdata = temp-> data) // check whether the return temp node is found at the end; else printf ("no location! /N "); Return temp;}/* function: reverse function parameter of one-way linked list: struct pointer return value: struct pointer */struct list * revers (struct list * H) {struct list * back, * P, * newhead = NULL; P = H; while (P! = NULL) {back = p-> next; // record the next position of the current node to prevent the loss of p-> next = newhead; // The current node starts to point to the forward node. At this time, newhead initialization must be null newhead = P; // The newhead pointer is followed by P to move backward until P points to null, at this time, newhead is the first node in the reverse order p = back; // find the next node} return newhead;} // feeling: I read one from the Internet, it is clear that when the pointer P is incorrect, the location of the next node is not recorded, causing the chain table to be disconnected. // its approach is back = P; // P-> next = newhead; // newhead = P; // P = back-> next; in this case, back records the next node. In fact, the next of the P point has been changed to null, invalidate the last statement struct list * delete_all (Struct list * h) {struct list * temp = NULL; while (H! = NULL) {temp = H; H = temp-> next; free (temp);} return h ;}