单链表中重复元素的删除

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

Time Limit: 1000MS Memory limit: 65536K

题目描述

按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除(值相同的元素只保留最后输入的一个)。

输入

第一行输入元素个数n; 
第二行输入n个整数。

输出

第一行输出初始链表元素个数; 
第二行输出按照逆位序所建立的初始链表;
第三行输出删除重复元素后的单链表元素个数;
第四行输出删除重复元素后的单链表。

示例输入

10
21 30 14 55 32 63 11 30 55 30

示例输出

10
30 55 30 11 63 32 55 14 30 21
7
30 55 11 63 32 14 21
 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,*p,*q,*p1;
11     head=(struct vode *)malloc(sizeof(struct vode ));
12     head->next=NULL;
13     int n,i;
14     scanf("%d",&n);
15     for(i=1;i<=n;i++)
16     {
17         p=(struct vode *)malloc(sizeof(struct vode ));
18         p->next=NULL;
19         scanf("%d",&p->date);
20         p->next=head->next;
21         head->next=p;
22     }
23     printf("%d
",n);
24     p=head->next;
25     int k=0;
26     while(p)
27     {
28         if(k==0)
29         {
30             printf("%d",p->date);
31             k=1;
32         }
33         else printf(" %d",p->date);
34         p=p->next;
35     }
36     printf("
");
37     p=head->next;
38     int sum=0;
39     while(p)
40     {
41         p1=p;
42         q=p1->next;
43         while(q)
44         {
45             if(p->date==q->date)
46             {
47                 q=q->next;
48                 p1->next=q;
49                 sum++;
50             }
51             else
52             {
53                 p1=p1->next;
54                 q=q->next;
55             }
56         }
57         p=p->next;
58     }
59     printf("%d
",n-sum);
60     p=head->next;
61      k=0;
62     while(p)
63     {
64         if(k==0)
65         {
66             printf("%d",p->date);
67             k=1;
68         }
69         else printf(" %d",p->date);
70         p=p->next;
71     }
72     printf("
");
73     return 0;
74 }
View Code
原文地址:https://www.cnblogs.com/kuangdaoyizhimei/p/3264603.html