自引用結構--之連結串列刪除元素
阿新 • • 發佈:2020-11-28
程式碼如下,很簡單,不說明:
1 //This is c program code! 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= 3 * 文件資訊: *** :~/testTmp.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日 星期六 上午10:18 (第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 after: \n"); 47 Stu *head2 = head; 48 head2 = head2->next; 49 for(int i = 0; i < size; i++){ 50 printf("%s\n", head2->name); 51 head2 = head2->next; 52 } 53 54 return 0; 55 }
執行結果:
delete before:
lina
mina
bina
tina
dina
delete after:
mina
bina
tina
dina