C-language single-chain table operation, C-language single-chain

Source: Internet
Author: User

C-language single-chain table operation, C-language single-chain

I am studying the C language in depth, and now I am using the data structure section. I recently learned a single-chain table. The integrated code is as follows. I hope to discuss it with you. This is the first blog post in my blog, and I will add new learning achievements later to share with you.

Use the C language to complete single-chain table related operations.

 

1/*** @ filename danlianbiao. c 3 * @ author haohaibo 4 * @ data preview /4/11 5 * @ brief single-chain table operation, including insert, delete, modify, and invert 6 **/7 # include <stdio. h> 8 # include <stdlib. h> 9 # include <strings. h> 10 # define N 32 11 typedef int datatype; 12 int checkresult [N]; // It is used to store the search result. The search result is an integer. 13 typedef struct node {14 15 datatype data; 16 struct node * Next; 17 18} linklist_t; 19 20/** 21 * @ brief get the number of nodes 22 */23 int linklist_count (linklist_t * header) 24 {25 int count = 0; 26 while (header-> Next! = NULL) 27 {28 header = header-> Next; 29 count ++; 30} 31 return count; 32} 33/** 34 * @ brief check whether the linked list is empty 35 */36 int linklist_null (linklist_t * header) 37 {38 if (NULL = header) 39 return 1; 40 else 41 return 0; 42} 43 44/*** 45 * @ brief create a chain table head node 46 */47 linklist_t * linklist_creat () 48 {49 linklist_t * h; 50 h = (linklist_t *) malloc (sizeof (linklist_t); 51 if (h = NULL) 52 {53 printf (_ FUNCTION _); 5 4 printf ("line % d" ,__ LINE _); 55 puts ("error! "); 56 return (linklist_t *)-1; 57} 58 bzero (h, sizeof (struct node); 59 h-> Next = NULL; 60 return h; 61} 62 63/** 64 * @ brief Insert a new node from the head of the linked list 65 */66 int linklist_insert_header (linklist_t * header, datatype value) 67 {68 linklist_t * insertnode; 69 insertnode = (linklist_t *) malloc (sizeof (linklist_t); 70 if (insertnode = NULL) 71 {72 printf (_ FUNCTION __); 73 printf ("line % d" ,__ LINE _); 74 puts ("error! "); 75 return-1; 76} 77 bzero (insertnode, sizeof (struct node); 78 insertnode-> data = value; 79 insertnode-> Next = header-> Next; 80 header-> Next = insertnode; 81 return 0; 82} 83 84/** 85 * @ brief Insert a new node 86 */87 int linklist_insert_tail (linklist_t * header, datatype value) 88 {89 linklist_t * insertnode; 90 insertnode = (linklist_t *) malloc (sizeof (linklist_t); 91 if (insertnode = NULL) 92 {93 print F (_ FUNCTION _); 94 printf ("line % d" ,__ LINE _); 95 puts ("error! "); 96 return-1; 97} 98 bzero (insertnode, sizeof (struct node); 99 insertnode-> data = value; 100 while (header-> Next! = NULL) 101 header = header-> Next; 102 header-> Next = insertnode; 103 insertnode-> Next = NULL; 104 return 0; 105} 106 107/** 108 * @ brief Insert a new node 109 */110 int linklist_insert_pos (linklist_t * header, int pos, datatype value) based on the location) 111 {112 113 if (pos> linklist_count (header) 114 {115 printf (_ FUNCTION _); 116 printf ("line % d" ,__ LINE __); 117 puts ("error! "); 118 return-1; 119} 120 linklist_t * insertnode; 121 insertnode = (linklist_t *) malloc (sizeof (linklist_t); 122 while (pos --) 123 header = header-> Next; 124 insertnode-> data = value; 125 insertnode-> Next = header-> Next; 126 header-> Next = insertnode; 127 return 0; 128} 129 130/** 131 * @ brief Insert a new node 132 */133 int linklist_insert_shunxu (linklist_t * header, datatype value) Based on the Data Sequence) 134 {135 while (header-> Next! = NULL & value <= header-> Next-> data) 136 header = header-> Next; 137 linklist_insert_header (header, value); 138 return 0; 139} 140/** 141 * @ brief delete node 142 */143 datatype linklist_del_header (linklist_t * header) 144 {145 linklist_t * p = header-> Next; 146 datatype value = p-> data; 147 header-> Next = header-> Next; 148 free (p); 149 p = NULL; 150 return value; 151 152} 153 154/** 155 * @ brief delete a node from the end of the linked list 156 */157 datatyp E linklist_del_tail (linklist_t * header) 158 {159 int I = 0; 160 while (header-> Next! = NULL) 161 header = header-> Next; 162 linklist_t * p = header-> Next; 163 datatype value = p-> data; 164 header-> Next = NULL; 165 free (p); 166 p = NULL; 167 return value; 168 169} 170 171/** 172 * @ brief modify the data 173 */174 void linklist_rev_data (linklist_t * header, datatype dataold, datatype datanew) in the node based on the data) 175 {176 177 while (header-> Next! = NULL) 178 {179 if (dataold = header-> data) 180 header-> data = datanew; 181 header = header-> Next; 182} 183} 184 185/** 186 * @ brief searches for the node location 187 */188 int * linklist_check_pos (linklist_t * header, datatype data) based on the data) 189 {190 int I = 0, j = 0; 191 while (header-> Next! = NULL) 192 {193 if (data = header-> data) 194 checkresult [I ++] = j; 195 header = header-> Next; 196 j ++; 197} 198 return checkresult; 199} 200 201/** 202 * @ brief searches for data in the node based on the reciprocal position 203 */204 datatype linklist_check_pos_t (linklist_t * header, int pos) 205 {206 int count = linklist_count (header)-pos; 207 if (count <0) 208 {209 printf (_ FUNCTION __); 210 printf ("line % d" ,__ LINE _); 211 puts ("error! "); 212 return-1; 213} 214 count ++; 215 while (count --) 216 header = header-> Next; 217 return (datatype) header-> data; 218} 219 220/** 221 * @ brief linked list to 222 */223 int linklist_reversal (linklist_t * header) 224 {225 linklist_t * p; 226 linklist_t * temp; 227 p = header-& gt; Next; 228 while (p-& gt; Next! = NULL) 229 {230 temp = p-> Next; 231 p-> Next = temp-> Next; 232 temp-> Next = header-> Next; 233 header-> Next = temp; 234} 235 return 0; 236} 237 238 239/** 240 * @ brief print the data in the linked list 241 */242 void linklist_show (linklist_t * p) 243 {244 while (p-> Next! = NULL) 245 {246 p = p-> Next; 247 printf ("p2-> data = % d \ n", p-> data ); 248} 249} 250 int main (void) 251 {252 int I = 0, a = 0; 253 linklist_t * p = linklist_creat (); 254 linklist_insert_tail (p, 100 ); 255 linklist_insert_tail (p, 200); 256 linklist_insert_tail (p, 300); 257 linklist_insert_tail (p, 200); 258 linklist_insert_tail (p, 300); 259 linklist_insert_tail (p, 200 ); 260 linklist_insert_tail (p, 300); 261 linklist_insert_tail (p, 400); 262 for (I = 0; I <N; I ++) 263 {264 a = linklist_check_pos (p, 300) [I]; 265 if (a) 266 printf ("% d", a); 267} 268 putchar (10); 269}

 

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.