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

 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
",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("
");
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
",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 }
原文地址:https://www.cnblogs.com/Angfe/p/10473354.html