1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 /*************************************************************************************/ 5 /* 第一版博主 原文地址 http://www.cnblogs.com/renyuan/archive/2013/05/21/3091506.html */ 6 /* 第二版博主 原文地址 http://www.cnblogs.com/wireless-dragon/p/5170565.html */ 7 /* 2.创建线性表,此函数输入不为正时终止读取数据*/ 8 /* 3.打印链表,链表的遍历 */ 9 /* 4.查询链表结点数并返回长度 */ 10 /* 5.检查单链表是否为空 */ 11 /* 6.将线性表进行冒泡排序 */ 12 /* 7.查找单链表中第n个结点中的元素 */ 13 /* 8.从单链表中查找具有给定值number的第一个元素,返回该结点的地址 */ 14 /* 9.把单链表中第n个结点的值修改为number的值 */ 15 /* 10.向单链表的表头插入一个元素 */ 16 /* 11.向单链表的末尾添加一个元素 */ 17 /* 12.向单链表中第n个结点位置插入元素为x的结点 */ 18 /* 13.向有序单链表中插入元素x结点,使得插入后仍然有序 */ 19 /* 14.从单链表中删除表头结点 */ 20 /* 15.从单链表中删除表尾结点 */ 21 /* 16.从单链表中删除第n个结点 */ 22 /* 17.从单链表中删除值为x的第一个结点 */ 23 /* 18.交换2个元素的位置 */ 24 /* 19.删除列表 */ 25 /*************************************************************************************/ 26 typedef int elemType; 27 typedef struct NODE 28 { 29 elemType element; 30 struct NODE *next; 31 } Node; 32 /* 2.创建线性表,此函数输入不为正时终止读取数据*/ 33 void creatList(Node **pHead) 34 { 35 printf("Please enter the list: "); 36 Node *p1, *p2; 37 p1 = p2 = (Node *)malloc(sizeof(Node)); 38 if (p1 == NULL || p2 == NULL) 39 exit(0); 40 memset(p1, 0, sizeof(Node)); 41 scanf("%d", &p1->element); 42 p1->next = NULL; 43 while(p1->element > 0) 44 { 45 if (*pHead == NULL) 46 (*pHead) = p1; 47 else 48 p2->next = p1; 49 p2 = p1; 50 p1 = (Node *)malloc(sizeof(Node)); 51 if (p1 == NULL) 52 exit(0); 53 memset(p1, 0, sizeof(Node)); 54 scanf("%d", &p1->element); 55 p1->next = NULL; 56 } 57 } 58 /* 3.打印链表,链表的遍历 */ 59 void printList(Node *pHead) 60 { 61 if (NULL == pHead) 62 printf("The list is empty "); 63 else 64 while(NULL != pHead) 65 { 66 printf("%d ", pHead->element); 67 pHead = pHead->next; 68 } 69 printf(" "); 70 } 71 /* 4.查询链表结点数并返回长度 */ 72 int sizeList(Node *pHead) 73 { 74 int size = 0; 75 while(pHead != NULL) 76 { 77 size ++; 78 pHead = pHead->next; 79 } 80 return size; 81 } 82 /* 5. 检查单链表是否为空 */ 83 void isEmptyList(Node *pHead) 84 { 85 if (pHead == NULL) 86 { 87 printf("The list is empty "); 88 exit(0); 89 } 90 } 91 /* 7.查找单链表中第n个结点中的元素 */ 92 void getElement(Node *pHead, int num) 93 { 94 for (int i = 1; i < num; ++i) 95 pHead = pHead->next; 96 printf("The value of the %dth element is:%d ", num, pHead->element); 97 } 98 /* 8.从单链表中查找具有给定值number的第一个元素,返回该结点的地址 */ 99 int getElemAddr(Node *pHead, int number) 100 { 101 int i = 1; 102 while(pHead != NULL) 103 { 104 if (pHead->element == number) 105 return i; 106 i++; 107 pHead = pHead->next; 108 } 109 return 0; 110 } 111 /* 9.把单链表中第n个结点的值修改为number的值 */ 112 void modifyElem(Node **pList, int addr, int number) 113 { 114 Node *pHead; //在此处如果直接更改pList指向的话,主函数中调用printList就会从addr处开始打印 115 int i = 1; 116 pHead = *pList; 117 while(pHead != NULL) 118 { 119 if (i == addr) 120 break; 121 pHead = pHead->next; 122 i++; 123 } 124 pHead->element = number; 125 } 126 /* 10.向单链表的表头插入一个元素 */ 127 void insertHeadList(Node **pHead) 128 { 129 Node *p1; 130 p1 = (Node *)malloc(sizeof(Node)); 131 if (p1 == NULL) 132 exit(0); 133 memset(p1, 0, sizeof(Node)); 134 printf("Please enter a number to be inserted:"); 135 scanf("%d", &p1->element); 136 p1->next = (*pHead);// 此时pHead指向的是第一个结点(有数据域的),所以新的结点要插入到头结点前 137 (*pHead) = p1; // pHead指向第一个结点 138 } 139 /* 11.向单链表的末尾添加一个元素 */ 140 void insertLastList(Node **pHead, int n) 141 { 142 Node *p1, *p2; 143 p2 = (*pHead); 144 int i; 145 for (i = 1; i < n; ++i) 146 p2 = p2->next; 147 p1 = (Node *)malloc(sizeof(Node)); 148 if (p1 == NULL) 149 exit(0); 150 memset(p1, 0, sizeof(Node)); 151 printf("Please enter a number to be inserted:"); 152 scanf("%d", &p1->element); 153 p1->next = NULL; 154 p2->next = p1; 155 } 156 /* 12.向单链表中第n个结点位置插入元素为x的结点 */ 157 void isAddPos(Node **pHead, int length) 158 { 159 Node *p1, *p2; 160 int position, i; 161 printf("Please enter the insert position:"); 162 scanf("%d", &position); 163 if (position > length || position <= 0) 164 { 165 printf("Input error, the program ends "); 166 exit(0); 167 } 168 p1 = (Node *)malloc(sizeof(Node)); 169 p2 = (*pHead); 170 if (p1 == NULL) 171 exit(0); 172 memset(p1, 0, sizeof(Node)); 173 printf("Please enter a number to be inserted:"); 174 scanf("%d", &p1->element); 175 for (i = 1; i < position - 1; ++i) 176 p2 = p2->next; 177 p1->next = p2->next; 178 p2->next = p1; 179 } 180 /* 6.将线性表进行冒泡排序 */ 181 void Arrange(Node **pHead, int length) 182 { 183 Node *p1; 184 p1 = (*pHead); 185 int i, j, temp; 186 for (i = length; i > 0; --i) 187 { 188 for(j = i - 1; j > 0; --j) 189 { 190 if ((p1->element) > (p1->next->element)) 191 { 192 temp = p1->element; 193 p1->element = p1->next->element; 194 p1->next->element = temp; 195 } 196 p1 = p1->next; 197 } 198 p1 = (*pHead); 199 } 200 } 201 int OrrderList(Node **pHead, int length) 202 { 203 Node *p1, *p2; 204 p1 = (*pHead); 205 p2 = (Node *)malloc(sizeof(Node)); 206 if (p2 == NULL) 207 exit(0); 208 memset(p2, 0, sizeof(Node)); 209 printf("Enter the value of the element to be inserted:"); 210 scanf("%d", &p2->element); 211 if (p2->element < p1->element) 212 { 213 p2->next = p1; 214 (*pHead) = p2; 215 return 1; 216 } 217 while(p1->next != NULL && p2->element > (p1->next->element)) 218 p1 = p1->next; 219 if (p1->next == NULL) 220 { 221 p2->next = NULL; 222 p1->next = p2; 223 return 1; 224 } 225 else 226 { 227 p2->next = p1->next; 228 p1->next = p2; 229 return 1; 230 } 231 } 232 /* 14.从单链表中删除表头结点 */ 233 void DelHeadList(Node **pHead) 234 { 235 Node *p1; 236 p1 = (*pHead); 237 (*pHead) = (*pHead)->next; 238 free(p1); 239 } 240 /* 15.从单链表中删除表尾结点 */ 241 void DelLastList(Node **pHead) 242 { 243 Node *p1, *p2; 244 p1 = (*pHead); 245 p2 = p1->next; 246 while(p2->next != NULL) 247 { 248 p2 = p2->next; 249 p1 = p1->next; 250 } 251 p1->next = NULL; 252 free(p2); 253 } 254 /* 16.从单链表中删除第n个结点 */ 255 void DelPos(Node **pHead, int length) 256 { 257 int n, i; 258 Node *p1, *p2; 259 p1 = (*pHead); 260 p2 = p1->next; 261 printf("Please enter the serial number number to delete:"); 262 scanf("%d", &n); 263 if (n < 1 || n > length) 264 exit(0); 265 for (i = 1; i < n - 1; ++i) 266 { 267 p2 = p2->next; 268 p1 = p1->next; 269 } 270 p1->next = p2->next; 271 free(p2); 272 } 273 /* 17.从单链表中删除值为x的第一个结点 */ 274 int Delx(Node **pHead) 275 { 276 Node *p1, *p2; 277 p1 = (*pHead); 278 p2 = p1->next; 279 int number; 280 printf("Please input is going to be deleted the value of x:"); 281 scanf("%d", &number); 282 if (number == (*pHead)->element) 283 { 284 (*pHead) = (*pHead)->next; 285 free(p1); 286 return 1; 287 } 288 while(p2 != NULL) 289 { 290 if (p2->element == number) 291 { 292 break; 293 } 294 p2 = p2->next; 295 p1 = p1->next; 296 } 297 if (p2 == NULL) 298 { 299 printf("X does not exist in the list "); 300 return 1; 301 } 302 else 303 { 304 p1->next = p2->next; 305 free(p2); 306 return 1; 307 } 308 } 309 /* 18.交换2个元素的位置 */ 310 void exchange2pos(Node **pHead, int length) 311 { 312 Node *p1, *p2; 313 int n1, n2, i, j, temp; 314 printf("Please enter the first number:"); 315 scanf("%d", &n1); 316 printf("Please enter the second number:"); 317 scanf("%d", &n2); 318 if (n1 < 1 || n1 > length || n2 < 1 || n2 > length) 319 exit(0); 320 p1 = p2 = (*pHead); 321 for (i = 1; i < n1; ++i) 322 { 323 p1 = p1->next; 324 } 325 for (j = 1; j < n2; ++j) 326 { 327 p2 = p2->next; 328 } 329 temp = p1->element; 330 p1->element = p2->element; 331 p2->element = temp; 332 } 333 /* 删除列表 */ 334 void clearList(Node **pHead) 335 { 336 Node *p1; 337 p1 = (*pHead); 338 while(p1 != NULL) 339 { 340 p1 = p1->next; 341 free((*pHead)); 342 (*pHead) = p1; 343 } 344 } 345 int main(int argc, char const *argv[]) 346 { 347 /* 1.初始化线性表,即置单链表的表头指针为空 */ 348 Node *pList = NULL; 349 int length = 0, n, addr, number; 350 /* 2.创建线性表,此函数输入不为正时终止读取数据*/ 351 printf("- - - - - - - - - 2 - - - - - - - - "); 352 creatList(&pList); 353 /* 5. 检查单链表是否为空 */ 354 isEmptyList(pList); 355 printList(pList); 356 /* 4.查询链表结点数并返回长度 */ 357 printf("- - - - - - - - - 4 - - - - - - - - "); 358 length = sizeList(pList); 359 printf("the Node length is:%d ", length); 360 /* 7.查找单链表中第n个结点中的元素 */ 361 printf("- - - - - - - - - 7 - - - - - - - - "); 362 printf("Please input node number (n):"); 363 scanf("%d", &n); 364 if (n > length || n < 1) 365 { 366 printf("N is not within the scope of "); 367 exit(0); 368 } 369 getElement(pList, n); 370 /* 8.从单链表中查找具有给定值number的第一个元素,返回该结点的地址 */ 371 printf("- - - - - - - - - 8 - - - - - - - - "); 372 addr = 0; 373 number; 374 printf("Please enter to find element value (number):"); 375 scanf("%d", &number); 376 addr = getElemAddr(pList, number); 377 if (addr == 0) 378 printf("List the element "); 379 else 380 printf("The location of the number is:%d ", addr); 381 /* 9.把单链表中第n个结点的值修改为number的值 */ 382 printf("- - - - - - - - - 9 - - - - - - - - "); 383 addr = 0; 384 number = 0; 385 printf("Please input to replace the serial number (n):"); 386 scanf("%d", &addr); 387 if (addr > length || addr < 0) 388 { 389 printf("N is not within the scope of "); 390 exit(0); 391 } 392 printf("Please input to replace the contents of the (number):"); 393 scanf("%d", &number); 394 modifyElem(&pList, addr, number); 395 printf("The revised list is: "); 396 printList(pList); 397 /* 10.向单链表的表头插入一个元素 */ 398 printf("- - - - - - - - - 10 - - - - - - - - "); 399 insertHeadList(&pList); 400 printList(pList); 401 /* 11.向单链表的末尾添加一个元素 */ 402 printf("- - - - - - - - - 11 - - - - - - - - "); 403 insertLastList(&pList, length); 404 printList(pList); 405 /* 12.向单链表中第n个结点位置插入元素值为x的结点 */ 406 printf("- - - - - - - - - 12 - - - - - - - - "); 407 isAddPos(&pList, length); 408 printList(pList); 409 /* 6.将线性表进行冒泡排序 */ 410 printf("- - - - - - - - - 6 - - - - - - - - "); 411 Arrange(&pList, length); 412 printList(pList); 413 /* 13.向有序单链表中插入元素x结点,使得插入后仍然有序 */ 414 printf("- - - - - - - - - 13 - - - - - - - - "); 415 OrrderList(&pList, length); 416 printList(pList); 417 /* 14.从单链表中删除表头结点 */ 418 printf("- - - - - - - - - 14 - - - - - - - - "); 419 DelHeadList(&pList); 420 printList(pList); 421 /* 15.从单链表中删除表尾结点 */ 422 printf("- - - - - - - - - 15 - - - - - - - - "); 423 DelLastList(&pList); 424 printList(pList); 425 /* 16.从单链表中删除第n个结点 */ 426 printf("- - - - - - - - - 16 - - - - - - - - "); 427 DelPos(&pList, length); 428 printList(pList); 429 /* 17.从单链表中删除值为x的第一个结点 */ 430 printf("- - - - - - - - - 17 - - - - - - - - "); 431 Delx(&pList); 432 printList(pList); 433 /* 18.交换2个元素的位置 */ 434 printf("- - - - - - - - - 18 - - - - - - - - "); 435 exchange2pos(&pList, length); 436 printList(pList); 437 /* 19.删除列表 */ 438 printf("- - - - - - - - - 19 - - - - - - - - "); 439 clearList(&pList); 440 printList(pList); 441 return 0; 442 }