首页 > 其他 > 详细

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

时间:2014-01-17 08:45:24      阅读:518      评论:0      收藏:0      [点我收藏+]

题目描述

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

输入

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

输出

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

示例输入

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

示例输出

5
5 2 3 4 7
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. using namespace std;  
  4. struct node  
  5. {  
  6.     int x;  
  7.     struct node *next;  
  8. }*head,*a,*b;  
  9. int main()  
  10. {  
  11.     int n, i, s;  
  12.     scanf("%d",&n);  
  13.     head = new node;  
  14.     head->next = NULL;  
  15.     node *p = head;  
  16.     for(i = 0; i < n; i++)  
  17.     {  
  18.         node *q = new node;  
  19.         scanf("%d",&q->x);  
  20.         q->next=NULL;  
  21.         p->next=q;  
  22.         p=q;  
  23.     }  
  24.     for(node *p=head->next;p!=NULL;p=p->next)  
  25.     {  
  26.         s = p->x;  
  27.         a = p;  
  28.         b = a->next;  
  29.         while(b!=NULL)  
  30.         {  
  31.             if(b->x==s)  
  32.             {  
  33.                 a->next = b->next;  
  34.                 delete(b);  
  35.                 b=a->next;  
  36.                 n--;  
  37.             }  
  38.             else  
  39.             {  
  40.                 a = a->next;  
  41.                 b = a->next;  
  42.             }  
  43.         }  
  44.     }  
  45.     printf("%d\n",n);  
  46.     for(node *p=head->next;p!=NULL;p=p->next)  
  47.     {  
  48.         printf("%d ",p->x);  
  49.     }  
  50.     return 0;  
  51. }  

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

原文:http://www.cnblogs.com/haichun/p/3522755.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!