顺序表应用2:多余元素删除之建表算法

顺序表应用2:多余元素删除之建表算法

Time Limit: 3 ms Memory Limit: 600 KiB

Submit Statistic

Problem Description

一个长度不超过10000数据的顺序表,可能存在着一些值相同的“多余”数据元素(类型为整型),编写一个程序将“多余”的数据元素从顺序表中删除,使该表由一个“非纯表”(值相同的元素在表中可能有多个)变成一个“纯表”(值相同的元素在表中只保留第一个)。
要求:
1、必须先定义线性表的结构与操作函数,在主函数中借助该定义与操作函数调用实现问题功能;
2、本题的目标是熟悉在顺序表原表空间基础上建新表的算法,要在原顺序表空间的基础上完成完成删除,建表过程不得开辟新的表空间;
3、不得采用原表元素移位删除的方式。

Input

第一行输入整数n,代表下面有n行输入;
之后输入n行,每行先输入整数m,之后输入m个数据,代表对应顺序表的每个元素。

Output

输出有n行,为每个顺序表删除多余元素后的结果

Sample Input

4
5 6 9 6 8 9
3 5 5 5
5 9 8 7 6 5
10 1 2 3 4 5 5 4 2 1 3

Sample Output

6 9 8
5
9 8 7 6 5
1 2 3 4 5

#include <iostream>
#include<bits/stdc++.h>
#define max 10001
using namespace std;
typedef int element;
typedef struct
{
   element *elem;
    int length;
    int listsize;
}sq;
void  initlist (sq &L )
{
    L.elem=(element*)malloc(max *sizeof(element));//申请max大小的空间
    L.length=0;
    L.listsize=max;
}
void cr(sq &L,int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&L.elem[i]);
    }
    L.length=n;
}


void del(sq*L)
{
    int k=-1;
    for(int i=0;i<L->length;i++)
    {
        int j=0;
        while(j<=k&&L->elem[j]!=L->elem[i])
            j++;
                
        if(k==-1||j>k)

            L->elem[++k]=L->elem[i];
    }
    L->length=k+1;
}
void pr(sq &L)
{
    int i;
    for(i=0;i<L.length-1;i++)
      printf("%d ",L.elem[i]);
      printf("%d\n",L.elem[L.length-1]);
}
int main()
{
    int n,m;
    sq L;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&m);
        initlist(L);
        cr(L,m);
        del(&L);
        pr(L);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值