顺序表的应用
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic Discuss
Problem Description
在长度为n(n<1000)的顺序表中可能存在着一些值相同的“多余”数据元素(类型为整型),编写一个程序将“多余”的数据元素从顺序表中删除,使该表由一个“非纯表”(值相同的元素在表中可能有多个)变成一个“纯表”(值相同的元素在表中只能有一个)。
Input
第一行输入表的长度n;
第二行依次输入顺序表初始存放的n个元素值。
Output
第一行输出完成多余元素删除以后顺序表的元素个数;
第二行依次输出完成删除后的顺序表元素。
Sample Input
12
5 2 5 3 3 4 2 5 7 5 4 3
Sample Output
5
5 2 3 4 7
Hint
用尽可能少的时间和辅助存储空间。
//实参为地址&或者为指针,则形参都要用指针接收。
//如果你使用的变量x是个结构体,应该用.访问其成员,如:x.num
//如果你使用的变量x是个结构体指针,应该用->访问其成员,如:x->num
//如果当前变量x是个结构体而你却使用了x->num的方式访问,就会出现上面的报错。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <bits/stdc++.h>
using namespace std;
typedef int Elemtype;
typedef struct
{
Elemtype *elem;
int length;
int listsize;
}Sqlist;
int initlist_Sq(Sqlist *L,int maxsize)
{
L->elem=(Elemtype*)malloc(sizeof(Elemtype)*maxsize);
if(NULL == L->elem)
return -1;
else
{
L->length=0;
L->listsize=maxsize;
}
return 0;
}
void creat(Sqlist *L,int n)
{
int i;
for(i=0;i<n;i++)
{
cin>>L->elem[i];
}
L->length=n;
}
void printf(Sqlist *L)
{
int i;
for(i=0;i<L->length-1;i++)
printf("%d ",L->elem[i]);
printf("%d\n",L->elem[L->length-1]);
}
void del(Sqlist *L)//此处的L为结构体指针。
{
int i,j,k;
for(i=0;i<=L->length-1;i++)
{
for(j=i+1;j<=L->length-1;j++)
{
if(L->elem[i]==L->elem[j])
{
for(k=j+1;k<=L->length-1;k++)
{
L->elem[k-1]=L->elem[k];
}
L->length--;
j--;
}
}
}
}
int main()
{
Sqlist L;//定义L为结构体不是结构体指针。
int n;
cin>>n;
initlist_Sq(&L,1001);
creat(&L,n);
del(&L);
printf("%d\n",L.length);//访问结构体指针与结构体不同
printf(&L);
return 0;
}