自引用结构--之链表删除元素

代码如下,很简单,不说明:

 1 //This is c program code!
 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 3   * 文档信息: *** :~/testTmp.c
 4   * 版权声明: *** :(魎魍魅魑)MIT
 5   * 联络信箱: *** :guochaoxxl@163.com
 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: 
");
40     Stu *head1 = head;
41     for(int i = 0; i < size; i++){
42         printf("%s
", head1->name);
43         head1 = head1->next;
44     }
45 
46     printf("delete after: 
");
47     Stu *head2 = head;
48     head2 = head2->next;
49     for(int i = 0; i < size; i++){
50         printf("%s
", 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

增加点功能:

 1 //This is c program code!
 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 3   * 文档信息: *** :~/testTmp.c
 4   * 版权声明: *** :(魎魍魅魑)MIT
 5   * 联络信箱: *** :guochaoxxl@163.com
 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: 
");
40     Stu *head1 = head;
41     for(int i = 0; i < size; i++){
42         printf("%s
", head1->name);
43         head1 = head1->next;
44     }
45 
46     printf("delete tail after: 
");
47     Stu *head2 = head;
48     tmp = head2;
49     while(tmp != NULL){
50         if(tmp->next->next == NULL){
51             tmp->next = NULL;
52         }
53         tmp = tmp->next;
54     }
55     while(NULL != head2->next){                                                                          
56         printf("%s
", head2->name);
57         head2 = head2->next;
58     }
59 
60     printf("delete head after: 
");
61     Stu *head3 = head;
62     head3 = head3->next;
63     while(NULL != head3->next){
64         printf("%s
", head3->name);
65         head3 = head3->next;
66     }
67 
68     return 0;
69 }    

结果:

delete before: 
lina
mina
bina
tina
dina
delete tail after: 
lina
mina
bina
tina
delete head after: 
mina
bina
tina
原文地址:https://www.cnblogs.com/guochaoxxl/p/14051962.html