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

来源:互联网 发布:网络实名制利大于弊 编辑:程序博客网 时间:2024/06/05 09:12

Time Limit: 1000MS Memory limit: 65536K

题目描述

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

输入

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

输出

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

示例输入

125 2 5 3 3 4 2 5 7 5 4 3

示例输出

55 2 3 4 7

提示

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

  1. #include<stdio.h>   
  2. #include<stdlib.h>   
  3. struct node   
  4. {   
  5.     int date;   
  6.     struct node *next;   
  7. };   
  8. struct node *head=NULL,*p,*q,*tail;   
  9. int main()   
  10. {   
  11.     int n,flag,k=1;   
  12.     scanf("%d",&n);   
  13.     while(n--)   
  14.     {   
  15.         if((p=(struct node*)malloc(sizeof(struct node)*1))==NULL)   
  16.             return 1;   
  17.         scanf("%d",&p->date);   
  18.         p->next=NULL;   
  19.         if(head==NULL)   
  20.         {   
  21.             //head->next=p;   
  22.             head=p;   
  23.             q=p;   
  24.         }   
  25.         else  
  26.         {   
  27.             tail=head;flag=1;   
  28.             while(tail!=NULL)   
  29.             {   
  30.                    
  31.                 if(tail->date==p->date)   
  32.                 {   
  33.                     flag=0;   
  34.                     break;   
  35.                 }   
  36.                 tail=tail->next;   
  37.             }   
  38.              if(flag)   
  39.             {   
  40.                    q->next=p;   
  41.                    q=q->next;   
  42.                    k++;   
  43.             }   
  44.         }   
  45.     }   
  46.             printf("%d\n",k);   
  47.             q=head;   
  48.             while(q!=NULL)   
  49.             {   
  50.                 printf("%d",q->date);   
  51.                 if(q->next!=NULL)   
  52.                 printf(" ");   
  53.                 q=q->next;   
  54.             }   
  55.        
  56.             return 0;   
  57.  }    
0 0
原创粉丝点击