1. 程式人生 > 實用技巧 >自引用結構--之連結串列指定值刪除元素

自引用結構--之連結串列指定值刪除元素

程式碼:

 1 //This is c program code!
 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 3   * 文件資訊: *** :~/testTmp4.c
 4   * 版權宣告: *** :(魎魍魅魑)MIT
 5   * 聯絡信箱: *** :[email protected]
 6   * 建立時間: *** :2020年11月28日的上午10:18
 7   * 文件用途: *** :資料結構與演算法分析-c語言描述
 8   * 作者資訊: *** :guochaoxxl(
http://cnblogs.com/guochaoxxl) 9 * 修訂時間: *** *2020年第47周 11月28日 星期六 下午09:00 (333天) 10 * 檔案描述: *** :自行新增 11 * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/ 12 #include <stdio.h> 13 #include <string.h> 14 #include <stdlib.h> 15 16 typedef struct _stu{
17 char name[10]; 18 struct _stu *next; 19 } Stu; 20 21 int main(int argc, char **argv) 22 { 23 Stu *head; 24 Stu *tmp; 25 head = tmp = (Stu *)malloc(sizeof(Stu)); 26 tmp->next = NULL; 27 28 char *stuName[] = {"lina", "mina", "bina", "tina", "dina"}; 29 int size = sizeof
(stuName)/sizeof(stuName[0]); 30 31 for(int i = 0; i < size; i++){ 32 strcpy(tmp->name, stuName[i]); 33 Stu *tmpN = (Stu *)malloc(sizeof(Stu)); 34 tmpN->next = NULL; 35 tmp->next = tmpN; 36 tmp = tmpN; 37 } 38 39 printf("delete before: \n"); 40 Stu *head1 = head; 41 for(int i = 0; i < size; i++){ 42 printf("%s\n", head1->name); 43 head1 = head1->next; 44 } 45 46 printf("delete 'bina' after: \n"); 47 Stu *head2 = head; 48 tmp = head2; 49 while(tmp->next != NULL){ 50 if(!(strcmp(tmp->next->name, "bina"))){ 51 tmp->next = tmp->next->next; 52 } 53 tmp = tmp->next; 54 } 55 while(NULL != head2->next){ 56 printf("%s\n", head2->name); 57 head2 = head2->next; 58 } 59 60 return 0; 61 }

  程式碼簡單,不多說了。