Add, delete, modify, and query the most comprehensive C-language linked list

Source: Internet
Author: User

Add, delete, modify, and query the most comprehensive C-language linked list

1 // This is written in C language, but an error is reported because len (current node length) 2 // cannot be inserted or deleted) then change 3 // delete cannot be used because delete is an operator in C ++. 4 // finally, I wrote the program change again in C ++, use references to change the real len value by 5 # include <stdio. h> 6 # include <stdlib. h> 7 typedef int ElementType; 8 typedef struct node {9 ElementType data; 10 struct node * pNext; // pointer to the next Node 11} node, * pNode; // here NODE is equivalent to struct node, and PNODE is equivalent to struct Node * 12 13 pNode Create_List (int len); 14 pNode change (pNode pHead, int len ); 15 void ergodic (pNode pHead, int len); 16 pNode insert (pNode pHead, int * len); 17 int main () {18 pNode pHead = NULL; // create a header Node 19 int len; // used to store valid node words 20 printf ("Enter the number of nodes:"); 21 scanf ("% d ", & len); 22 pHead = Create_List (len); // create a single-chain table and assign the header pointer of the linked list to pHead 23 pNode p; // create a mobile pointer, point to the node to be accessed 24 25 26 ergodic (pHead, len); // print the data output 27 p = change (pHead, len ); // modify the node data 28 ergodic (pHead, len); // print the data output 29 p = insert (pHead, & len ); // insert a node 30 printf ("len: % d", len); 31 printf ("\ n inserted successfully \ n"); 32 ergodic (pHead, len); // print the data output 33 return 0; 34} 35 36 37 38 pNode Create_List (int len) // PNODE is used to return a pointer of the struct type 39 {40 // create a linked list 41 42 pNode pHead = (pNode) malloc (sizeof (Node )); // allocate a header node 43 that does not store valid data // malloc returns a node, where (struct type pointer) malloc (sizeof (struct name )) 44 pNode pTail = pHead; // define a tail pointer and initialize 45 pTail-> pNext = NULL; // leave the tail node pointer blank 46 47 int I; 48 int val; 49 for (I = 0; I <len; I ++) {50 printf ("Enter the value of node % d:", I + 1 ); 51 scanf ("% d", & val); 52 pNode pNew = (pNode) malloc (sizeof (Node )); 53 // allocate space to the next node 54 pNew-> data = val; // 1. first, assign the value to the next node 55 pTail-> pNext = pNew; // the pointer field of the new node pTail 56 pNew = NULL; // leave the pointer field of the node empty 57 pTail = pTail-> pNext; // point the tail pointer + 1 to the last node 58} 59 return pHead; // return the head pointer of a linked list 60} 61 // ++ +++ 62 void ergodic (pNode pHead, int len) {63 // print data output 64 pNode p; 65 p = pHead; // point the pointer to the header node 66 int j; 67 for (j = 0; j <len; j ++) {68 p = p-> pNext; 69 printf ("the value of node % d is % d \ n ", (j + 1), p-> data ); 70} 71} 72 73 // ++ 74 pNode change (pNode pHead, int len) {75 // modify the node data 76 pNode p; 77 p = pHead; 78 int value; 79 printf ("Modify node data \ n "); 80 int k; 81 for (k = 0; k <len; k ++) {82 p = p-> pNext; 83 printf ("Enter the data to be modified at node % d:", k + 1); 84 scanf ("% d", & value ); 85 p-> data = value; 86} 87 return pHead; 88} 89 // ++ 90 // insert node 91 pNode insert (pNode pHead, int * len) {92 pNode p; 93 p = pHead; 94 printf ("the input is inserted after the first node (not counted as the header node):"); 95 int m; 96 scanf ("% d", & m); 97 int I; 98 for (I = 0; I <m; I ++) {99 p = p-> pNext; 100} 101 pNode e = (pNode) malloc (sizeof (Node); 102 e-> pNext = NULL; 103 printf ("Enter the value of this Node :"); 104 int n; 105 scanf ("% d", & n); 106 e-> data = n; 107 e-> pNext = p-> pNext; 108 p-> pNext = e; 109 len ++; 110 return pHead; 111} 112 // ++ 113 // ++ 114 // delete node 115 pNode delete (pNode pHead, int len) {116 pNode p; 117 pNode q; 118 p = pHead; 119 printf ("Enter the nodes to delete (excluding the header nodes )"); 120 int k; 121 scanf ("% d", & k); 122 int I; 123 for (I = 0; I <K-1; I ++) {124 p = p-> pNext; 125} 126 q = p-> pNext; 127 p-> pNext = q-> pNext; 128 free (q ); 129 q = NULL; 130 return pHead; 131}

 

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.