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

来源:互联网 发布:淘宝 国外怎么用 编辑:程序博客网 时间:2024/06/08 02:21

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

Problem Description

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

Input

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

Output

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

Example Input

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

Example Output

5
5 2 3 4 7.

Hint

用尽可能少的时间和辅助存储空间。

代码块

代码块语法遵循标准markdown代码,例如:

#include<stdlib.h>#include<string.h>#include<iostream>#include<algorithm>struct node{    int data;    struct node *next;}node;struct node *s,*p,*l,*q,*r,*pre,*w;int main(){    int n;    int i;    l=(struct node *)malloc(sizeof(node));//建表     l->next=NULL;    scanf("%d",&n);    w=l;    for(i=1;i<=n;i++)    {        s=(struct node *)malloc(sizeof(node));        scanf("%d",&s->data);        w->next=s;        w=s;    }    w->next=NULL;    p=l->next;    while(p&&p->next!=NULL)    {        q=p->next;        pre=p;        while(q)        {            if(q->data==p->data)            {                pre->next=q->next;                free(q);                q=pre->next;                n--;            }            else            {                pre=q;                q=q->next;            }        }        p=p->next;    }    printf("%d\n",n);    r=l->next;    while(r&&r->next!=NULL)    {        printf("%d ",r->data);        r=r->next;    }    printf("%d\n",r->data);    return 0;}
原创粉丝点击