Time Limit: 1000MS Memory limit: 65536K
题目描述
在长度为n(n<1000)的顺序表中可能存在着一些值相同的“多余”数据元素(类型为整型),编写一个程序将“多余”的数据元素从顺序表中删除,使该表由一个“非纯表”(值相同的元素在表中可能有多个)变成一个“纯表”(值相同的元素在表中只能有一个)。
输入
第一行输入表的长度n;
第二行依次输入顺序表初始存放的n个元素值。
第二行依次输入顺序表初始存放的n个元素值。
输出
第一行输出完成多余元素删除以后顺序表的元素个数;
第二行依次输出完成删除后的顺序表元素。
第二行依次输出完成删除后的顺序表元素。
示例输入
12 5 2 5 3 3 4 2 5 7 5 4 3
示例输出
5 5 2 3 4 7
提示
用尽可能少的时间和辅助存储空间。
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int date;
- struct node *next;
- };
- struct node *head=NULL,*p,*q,*tail;
- int main()
- {
- int n,flag,k=1;
- scanf("%d",&n);
- while(n--)
- {
- if((p=(struct node*)malloc(sizeof(struct node)*1))==NULL)
- return 1;
- scanf("%d",&p->date);
- p->next=NULL;
- if(head==NULL)
- {
- //head->next=p;
- head=p;
- q=p;
- }
- else
- {
- tail=head;flag=1;
- while(tail!=NULL)
- {
- if(tail->date==p->date)
- {
- flag=0;
- break;
- }
- tail=tail->next;
- }
- if(flag)
- {
- q->next=p;
- q=q->next;
- k++;
- }
- }
- }
- printf("%d\n",k);
- q=head;
- while(q!=NULL)
- {
- printf("%d",q->date);
- if(q->next!=NULL)
- printf(" ");
- q=q->next;
- }
- return 0;
- }