顺序表应用1:多余元素删除之移位算法

来源:互联网 发布:公知什么意思 编辑:程序博客网 时间:2024/06/10 16:33

Problem Description

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

Input

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

Output

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

Example Input

45 6 9 6 8 93 5 5 55 9 8 7 6 510 1 2 3 4 5 5 4 2 1 3

Example Output

6 9 859 8 7 6 51 2 3 4 5
 
#include<stdio.h>#define MAXSIZE 10000typedef int ElementType;struct node{    ElementType data[MAXSIZE];    int last;};void create(struct node *p,int m){    int i;    for(i=0;i<m;i++)    {        scanf("%d",&p->data[i]);    }    p->last=m-1;}void dele (struct node *p,int j){    int i;    for(i=j;i<p->last;i++)    {        p->data[i]=p->data[i+1];    }    p->last--;}void search (struct node *p){    int i,j;    for(i=0;i<p->last;i++)    {        for(j=i+1;j<=p->last;j++)        {            if(p->data[i]==p->data[j])            {                dele(p,j);                j--;            }        }    }}void show (struct node *p){    int i;    for(i=0;i<=p->last;i++)    {        if(i!=p->last)            printf("%d ",p->data[i]);        else            printf("%d\n",p->data[i]);    }}int main(){    struct node  p;    int n,m;    scanf("%d",&n);    while(n--)    {        scanf("%d",&m);        create(&p,m);        search(&p);        show(&p);    }    return 0;}
0 0
原创粉丝点击