| xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1 | #include "linklist.h" | 
|  | 2 |  | 
|  | 3 |  | 
|  | 4 | linklist create_linklist(void) | 
|  | 5 | { | 
|  | 6 | linklist L; | 
|  | 7 | if((L=(linklist)malloc(sizeof(listnode))) == NULL) | 
|  | 8 | { | 
|  | 9 | printf("malloc no memmory!\n"); | 
|  | 10 | return NULL; | 
|  | 11 | } | 
|  | 12 | L->data.fd = 0; | 
|  | 13 | bzero(L->data.ipv4_addr,sizeof(L->data.ipv4_addr)); | 
|  | 14 | L->data.port = 0; | 
|  | 15 |  | 
|  | 16 | L->next = NULL; | 
|  | 17 | return L; | 
|  | 18 | } | 
|  | 19 | linklist create_n_linklist(void) | 
|  | 20 | { | 
|  | 21 | linklist L,H,r; | 
|  | 22 | datatype x; | 
|  | 23 | if((L=(linklist)malloc(sizeof(listnode))) == NULL) | 
|  | 24 | { | 
|  | 25 | printf("malloc no memmory!\n"); | 
|  | 26 | return NULL; | 
|  | 27 | } | 
|  | 28 | L->data.fd = 0; | 
|  | 29 | bzero(L->data.ipv4_addr,sizeof(L->data.ipv4_addr)); | 
|  | 30 | L->data.port = 0; | 
|  | 31 | L->next = NULL; | 
|  | 32 | r = L; | 
|  | 33 | while(1) | 
|  | 34 | { | 
|  | 35 | printf("Please input a NUM_NL80211_WOWLAN_TRIGber(-1 exit :"); | 
|  | 36 | while(scanf("%d",&x.fd) != 1) | 
|  | 37 | { | 
|  | 38 | printf("Please INPUT_PROP_CNTut a number(-1 exit :"); | 
|  | 39 | getchar(); | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | if(x.fd == -1)break; | 
|  | 43 |  | 
|  | 44 | if((H=(linklist)malloc(sizeof(listnode))) == NULL) | 
|  | 45 | { | 
|  | 46 | printf("malloc nexto memmory!\n"); | 
|  | 47 | return L; | 
|  | 48 | } | 
|  | 49 | H->data = x; | 
|  | 50 | H->next = NULL; | 
|  | 51 | r->next = H; | 
|  | 52 | r=H; | 
|  | 53 | } | 
|  | 54 | return L; | 
|  | 55 |  | 
|  | 56 | } | 
|  | 57 | int delete_pos_linklist(linklist L,int pos) | 
|  | 58 | { | 
|  | 59 | linklist r,p; | 
|  | 60 | int i=-1; | 
|  | 61 | if(pos < 0 ||pos >=get_length_linklist(L)) | 
|  | 62 | { | 
|  | 63 | printf("input pos is invalid!\n"); | 
|  | 64 | return -1; | 
|  | 65 | } | 
|  | 66 | r = L; | 
|  | 67 | while(i < pos-1) | 
|  | 68 | { | 
|  | 69 | i++; | 
|  | 70 | r = r->next; | 
|  | 71 | } | 
|  | 72 | if(r == NULL || r->next==NULL) | 
|  | 73 | { | 
|  | 74 | printf("argc is iavalid!\n"); | 
|  | 75 | return -1; | 
|  | 76 | } | 
|  | 77 | else | 
|  | 78 | { | 
|  | 79 | p = r->next; | 
|  | 80 | r->next = p->next; | 
|  | 81 | p->next = NULL; | 
|  | 82 | free(p); | 
|  | 83 | return 0; | 
|  | 84 | } | 
|  | 85 | } | 
|  | 86 | int delete_locate_linklist(linklist L,datatype x) | 
|  | 87 | { | 
|  | 88 | linklist r,p; | 
|  | 89 | if(L->next == NULL) | 
|  | 90 | { | 
|  | 91 | printf("list is NULL!\n"); | 
|  | 92 | return -1; | 
|  | 93 | } | 
|  | 94 | r = L; | 
|  | 95 | while(r->next->data.fd != x.fd) | 
|  | 96 | { | 
|  | 97 | if(r->next == NULL) | 
|  | 98 | { | 
|  | 99 | printf("value is not in list!\n"); | 
|  | 100 | return -1; | 
|  | 101 | } | 
|  | 102 | r = r->next; | 
|  | 103 | } | 
|  | 104 | p = r->next; | 
|  | 105 | r->next = p->next; | 
|  | 106 | p->next = NULL; | 
|  | 107 | free(p); | 
|  | 108 |  | 
|  | 109 | return 0; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | void clear_linklist(linklist L) | 
|  | 113 | { | 
|  | 114 | while(get_length_linklist(L)) | 
|  | 115 | { | 
|  | 116 | delete_pos_linklist(L,get_length_linklist(L)-1); | 
|  | 117 | } | 
|  | 118 | free(L); | 
|  | 119 | L->next = NULL; | 
|  | 120 | L = NULL; | 
|  | 121 | } | 
|  | 122 | int get_length_linklist(linklist L) | 
|  | 123 | { | 
|  | 124 | int i=0; | 
|  | 125 | linklist r; | 
|  | 126 | r = L; | 
|  | 127 | while(r->next) | 
|  | 128 | { | 
|  | 129 | r = r->next; | 
|  | 130 | i++; | 
|  | 131 | } | 
|  | 132 | return i++; | 
|  | 133 | } | 
|  | 134 | linklist get_list_pos_linklist(linklist L,int pos) | 
|  | 135 | { | 
|  | 136 | int i=0; | 
|  | 137 | linklist r; | 
|  | 138 | if(L->next == NULL) | 
|  | 139 | { | 
|  | 140 | printf("list is NULL!\n"); | 
|  | 141 | return NULL; | 
|  | 142 | } | 
|  | 143 | if(pos<0 || pos>=get_length_linklist(L)) | 
|  | 144 | { | 
|  | 145 | printf("input is invalid!\n"); | 
|  | 146 | return NULL; | 
|  | 147 | } | 
|  | 148 | r = L->next; | 
|  | 149 | while(i<pos) | 
|  | 150 | { | 
|  | 151 | r = r->next; | 
|  | 152 | i++; | 
|  | 153 | } | 
|  | 154 | return r; | 
|  | 155 | } | 
|  | 156 | linklist get_list_locate_linklist(linklist L,datatype x) | 
|  | 157 | { | 
|  | 158 | linklist r; | 
|  | 159 | if(L->next == NULL) | 
|  | 160 | { | 
|  | 161 | printf("list is NULL!\n"); | 
|  | 162 | return NULL; | 
|  | 163 | } | 
|  | 164 | r = L->next; | 
|  | 165 | while(r->data.fd != x.fd) | 
|  | 166 | { | 
|  | 167 | if(r->next == NULL) | 
|  | 168 | { | 
|  | 169 | printf("value is not in list!\n"); | 
|  | 170 | return NULL; | 
|  | 171 | } | 
|  | 172 | r = r->next; | 
|  | 173 | } | 
|  | 174 | return r; | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | int insert_head_linklist(linklist L,datatype x) | 
|  | 178 | { | 
|  | 179 | linklist H; | 
|  | 180 | if((H=(linklist)malloc(sizeof(listnode))) == NULL) | 
|  | 181 | { | 
|  | 182 | printf("malloc no memmory\n"); | 
|  | 183 | return -1; | 
|  | 184 | } | 
|  | 185 | H->data = x; | 
|  | 186 | H->next = L->next; | 
|  | 187 | L->next = H; | 
|  | 188 | return 0; | 
|  | 189 | } | 
|  | 190 | int insert_n_head_linklist(linklist L) | 
|  | 191 | { | 
|  | 192 | datatype x; | 
|  | 193 |  | 
|  | 194 | while(1) | 
|  | 195 | { | 
|  | 196 | printf("Please input a number(-1 exit:"); | 
|  | 197 | while(scanf("%d",&x.fd) != 1) | 
|  | 198 | { | 
|  | 199 | printf("Please input a number:(-1 exit"); | 
|  | 200 | getchar(); | 
|  | 201 | } | 
|  | 202 | if(x.fd == -1)break; | 
|  | 203 | insert_head_linklist(L,x); | 
|  | 204 | } | 
|  | 205 | return 0; | 
|  | 206 | } | 
|  | 207 | int insert_end_linklist(linklist L,datatype x) | 
|  | 208 | { | 
|  | 209 | linklist r,H; | 
|  | 210 | if((H = (linklist)malloc(sizeof(listnode))) == NULL) | 
|  | 211 | { | 
|  | 212 | printf("malloc no memmory!"); | 
|  | 213 | return -1; | 
|  | 214 | } | 
|  | 215 | r = L; | 
|  | 216 | while(r->next) | 
|  | 217 | { | 
|  | 218 | r = r->next; | 
|  | 219 | } | 
|  | 220 | H->next = NULL; | 
|  | 221 | H->data = x; | 
|  | 222 | r->next = H; | 
|  | 223 | return 0; | 
|  | 224 | } | 
|  | 225 | int insert_n_end_linklist(linklist L) | 
|  | 226 | { | 
|  | 227 | datatype x; | 
|  | 228 |  | 
|  | 229 | while(1) | 
|  | 230 | { | 
|  | 231 | printf("Please input a number(-1 exit:"); | 
|  | 232 | while(scanf("%d",&x.fd) != 1) | 
|  | 233 | { | 
|  | 234 | printf("Please input a number:(-1 exit"); | 
|  | 235 | getchar(); | 
|  | 236 | } | 
|  | 237 | if(x.fd == -1)break; | 
|  | 238 | insert_end_linklist(L,x); | 
|  | 239 | } | 
|  | 240 | return 0; | 
|  | 241 |  | 
|  | 242 | } | 
|  | 243 | int insert_pos_linklist(linklist L,datatype x,int pos) | 
|  | 244 | { | 
|  | 245 | linklist K,r; | 
|  | 246 | if(pos == 0) | 
|  | 247 | { | 
|  | 248 | r = L; | 
|  | 249 | } | 
|  | 250 | else | 
|  | 251 | { | 
|  | 252 | r = get_list_pos_linklist(L,pos-1); | 
|  | 253 | } | 
|  | 254 | if(r == NULL) | 
|  | 255 | { | 
|  | 256 | printf("argc is invalidateid!\n"); | 
|  | 257 | return -1; | 
|  | 258 | } | 
|  | 259 | else | 
|  | 260 | { | 
|  | 261 | if((K = (linklist)malloc(sizeof(listnode))) == NULL) | 
|  | 262 | { | 
|  | 263 | printf("malloc no memmory!"); | 
|  | 264 | return -1; | 
|  | 265 | } | 
|  | 266 | K->data = x; | 
|  | 267 | K->next = r->next; | 
|  | 268 | r->next = K; | 
|  | 269 | } | 
|  | 270 | return 0; | 
|  | 271 | } | 
|  | 272 | int insert_order_linklist(linklist L,datatype x) | 
|  | 273 | { | 
|  | 274 | linklist r,H; | 
|  | 275 | if((H = (linklist)malloc(sizeof(listnode))) == NULL) | 
|  | 276 | { | 
|  | 277 | printf("malloc nexto memmory!\n"); | 
|  | 278 | return -1; | 
|  | 279 | } | 
|  | 280 | H->data = x; | 
|  | 281 |  | 
|  | 282 | r = L; | 
|  | 283 | while(r->next && r->next->data.fd < x.fd) | 
|  | 284 | { | 
|  | 285 | r = r->next; | 
|  | 286 | } | 
|  | 287 | H->next = r->next; | 
|  | 288 | r->next = H; | 
|  | 289 | return 0; | 
|  | 290 | } | 
|  | 291 | void reverse_linklist(linklist L) | 
|  | 292 | { | 
|  | 293 | linklist r,p; | 
|  | 294 |  | 
|  | 295 | if(L->next == NULL) | 
|  | 296 | { | 
|  | 297 | printf("list is NULL!\n"); | 
|  | 298 | return ; | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | r = L->next; | 
|  | 302 | L->next = NULL; | 
|  | 303 | while(r) | 
|  | 304 | { | 
|  | 305 | p = r; | 
|  | 306 | r = r->next; | 
|  | 307 |  | 
|  | 308 | p->next = L->next; | 
|  | 309 | L->next = p; | 
|  | 310 | } | 
|  | 311 | return ; | 
|  | 312 | } | 
|  | 313 | void sort_linklist(linklist L) | 
|  | 314 | { | 
|  | 315 | linklist r,p,q; | 
|  | 316 | if(L == NULL) | 
|  | 317 | { | 
|  | 318 | printf("list is NULL!\n"); | 
|  | 319 | return ; | 
|  | 320 | } | 
|  | 321 | r = L->next; | 
|  | 322 | L->next = NULL; | 
|  | 323 | while(r) | 
|  | 324 | { | 
|  | 325 | p = r; | 
|  | 326 | r = r->next; | 
|  | 327 |  | 
|  | 328 | q = L; | 
|  | 329 | while(q->next && q->next->data.fd < p->data.fd) | 
|  | 330 | { | 
|  | 331 | q = q->next; | 
|  | 332 | } | 
|  | 333 | p->next = q->next; | 
|  | 334 | q->next = p; | 
|  | 335 | } | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | void show_linklist(linklist L) | 
|  | 339 | { | 
|  | 340 | printf("list is:\n"); | 
|  | 341 | if(L->next == NULL) | 
|  | 342 | { | 
|  | 343 | printf("\tlist is NULL!list\n"); | 
|  | 344 | return ; | 
|  | 345 | } | 
|  | 346 | while(L->next) | 
|  | 347 | { | 
|  | 348 | printf("\t%d %s %d\t",L->next->data.fd,L->next->data.ipv4_addr,L->next->data.port); | 
|  | 349 | L = L->next; | 
|  | 350 | puts(""); | 
|  | 351 | } | 
|  | 352 | //puts(""); | 
|  | 353 | return ; | 
|  | 354 | } |