首页 > 其他 > 详细

2122=数据结构实验之链表七:单链表中重复元素的删除

时间:2019-03-04 22:23:05      阅读:166      评论:0      收藏:0      [点我收藏+]
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 struct node
 4 {
 5     int data;
 6     struct node *next;
 7 };
 8 int main()
 9 {
10     int n,i;
11     scanf("%d",&n);
12     int count=n;
13     struct node*head,*p,*q,*k;
14     head=(struct node*)malloc(sizeof(struct node));
15     head->next=NULL;
16     for(i=0; i<n; i++)
17     {
18         p=(struct node*)malloc(sizeof(struct node));
19         scanf("%d",&p->data);
20         p->next=head->next;
21         head->next=p;
22     }//逆序建立链表。
23     printf("%d\n",count);
24     for(p=head->next; p; p=p->next)
25     {
26         printf("%d",p->data);
27         if(p->next!=NULL)printf(" ");
28     }
29     printf("\n");
30     for(p=head->next; p; p=p->next)
31     {
32         for(q=p; q->next!=NULL; )
33         {
34             if(p->data==q->next->data)
35             {
36                 k=q->next->next;
37                 q->next=k;
38                 count--;
39             }
40             else q=q->next;
41         }
42     }//注意一下这里的的范围,不然很容易超出范围去访问。
43     printf("%d\n",count);
44     for(p=head->next; p; p=p->next)
45     {
46         printf("%d",p->data);
47         if(p->next!=NULL)printf(" ");
48     }
49     return 0;
50 }

 

2122=数据结构实验之链表七:单链表中重复元素的删除

原文:https://www.cnblogs.com/Angfe/p/10473354.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!