数据结构上机测试1:顺序表的应用
Time Limit: 1000MS Memory limit: 65536K
题目描述
输入
第二行依次输入顺序表初始存放的n个元素值。
输出
第二行依次输出完成删除后的顺序表元素。
示例输入
12 5 2 5 3 3 4 2 5 7 5 4 3
示例输出
5 5 2 3 4 7
提示
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#define MAX 1002
using namespace std;
typedef struct
{
int *elem;
int length;
}Sq;
void initList(Sq *L)
{
L->elem = (int *)malloc(MAX*4);
if( !L->elem )
{
exit(-1);
}
L->length = 0;
}
void create(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 m)
{
int *p,*q;
p = &L->elem[m];
for(q = p;q < p + L->length; q++)
{
*q = *(q+1);
}
L->length--;
}
void locList(Sq *L)
{
int *p,*q;
p = L->elem;
while( p != L->elem + L->length )
{
q = p+1;
while( q != L->elem + L->length )
{
if(*q == *p)
{
Del(L, (q - L->elem));
q--;
}
q++;
}
p++;
}
}
void display(Sq *L)
{
int i;
printf("%d\n",L->length);
for(i = 0;i < L->length; i++)
{
printf(i != L->length - 1 ? "%d " : "%d\n", L->elem[i]);
}
}
int main()
{
int n;
Sq L;
scanf("%d",&n);
initList(&L);
create(&L, n);
locList(&L);
display(&L);
return 0;
}
我终于补完了,好开心啊!