标签:des style blog io ar os sp for 数据
12 5 2 5 3 3 4 2 5 7 5 4 3
5 5 2 3 4 7
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <ctype.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node *creat(int n) //顺序序创建链表
{
int i;
struct node *head, *p, *tail;
head = new node;
head->next = NULL;
tail=head;
for(i=0; i<n; i++)
{
p=new node;
cin>>p->data;
p->next=NULL;
tail->next=p;
tail=p;
}
return head;
}
int main()
{
int n;
cin>>n;
int i, j;
int len;
struct node *head, *w, *q;
struct node *h1, *p, *tail, *dd;
head=creat(n);
//删除重复元素,重复的保留最前面的
h1=new node;
h1->next=NULL;
tail=h1;
len=0;
w=head->next;
delete(head);
for(i=0; i<n; i++)
{
if(h1->next==NULL) //此时链表为空
{
p=new node;
p->data = w->data;
p->next=NULL;
tail->next=p;
tail=p;
len++;
q=w;
w=w->next;
delete(q); //将原链表的节点删除
}
else //若不为空
{
dd=h1->next;
int ff=1;
for(j=0; j<len; j++) //检查是否出现过
{
if(dd->data == w->data )
{
ff=0;
break;
}
dd=dd->next;
}
if(ff==1 )//没出现过
{
p=new node;
p->data = w->data;
p->next=NULL;
tail->next=p;
tail=p;
len++;
q=w;
w=w->next;
delete(q);
}
else if(ff==0 )
{
w=w->next;
}
}
//w=w->next;
}
cout<<len<<endl;
w=h1->next;
for(int k=0; k<len; k++)
{
if(k==0)
cout<<w->data;
else
cout<<" "<<w->data;
w=w->next;
}
cout<<endl;
return 0;
}
标签:des style blog io ar os sp for 数据
原文地址:http://www.cnblogs.com/yspworld/p/4094372.html