删除重复元素

 

数据结构上机测试1:顺序表的应用

Time Limit: 1000MS Memory limit: 65536K

题目描述

在长度为n(n<1000)的顺序表中可能存在着一些值相同的“多余”数据元素(类型为整型),编写一个程序将“多余”的数据元素从顺序表中删除,使该表由一个“非纯表”(值相同的元素在表中可能有多个)变成一个“纯表”(值相同的元素在表中只能有一个)。

输入

第一行输入表的长度n;
第二行依次输入顺序表初始存放的n个元素值。

输出

第一行输出完成多余元素删除以后顺序表的元素个数;
第二行依次输出完成删除后的顺序表元素。

示例输入

12
5 2 5 3 3 4 2 5 7 5 4 3

示例输出

5
5 2 3 4 7
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 struct vode
 4 {
 5    int date;
 6    struct vode *next;
 7 };
 8 int main()
 9 {
10   struct vode *head,*tail,*p,*p1,*q;
11   head=(struct vode *)malloc(sizeof(struct vode ));
12   head->next=NULL;
13   tail=head;
14   int n;
15   scanf("%d",&n);
16   int i,sum=0;
17   for(i=1;i<=n;i++)
18   {
19       p=(struct vode *)malloc(sizeof(struct vode ));
20       p->next=NULL;
21       scanf("%d",&p->date);
22       tail->next=p;
23       tail=p;
24   }
25   p=head->next;
26   while(p)
27   {
28       p1=p;
29       q=p1->next;
30       while(q)
31       {
32           if(q->date==p->date)
33           {
34               q=q->next;
35               p1->next=q;
36               sum++;
37           }
38           else
39           {
40               p1=p1->next;
41               q=q->next;
42           }
43       }
44       p=p->next;
45   }
46   int j=0;
47   printf("%d
",n-sum);
48   p=head->next;
49   while(p)
50   {
51       if(j==0){printf("%d",p->date);j=1;}
52       else printf(" %d",p->date);
53       p=p->next;
54   }
55   printf("
");
56   return 0;
57 }
View Code
原文地址:https://www.cnblogs.com/kuangdaoyizhimei/p/3264601.html