zoukankan      html  css  js  c++  java
  • 数据结构上机测试1:顺序表的应用

    题目描述

    在长度为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);  
    46.     for(node *p=head->next;p!=NULL;p=p->next)  
    47.     {  
    48.         printf("%d ",p->x);  
    49.     }  
    50.     return 0;  
    51. }  
  • 相关阅读:
    [mock open]PyUnit执行单元测试时使用字符串模拟文件对象
    bottle 0.5中的key-value数据库
    bottle模板中的替换
    返回不同值的小技巧
    带有参数的装饰器
    常用命令速查
    SQLAlchemy多线程下事务隔离机制详解
    Bancor 协议浅析
    Flask中 endpoint 解析
    pip 相关问题
  • 原文地址:https://www.cnblogs.com/haichun/p/3522755.html
Copyright © 2011-2022 走看看