#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LISTSIZE 100
typedef struct{/*顺序表元素的定义*/
int *elem;
int length;
int listsize;
}Sqlist;
int SqInitial(Sqlist *L){
L->elem=(int *)malloc(LISTSIZE*sizeof(int));
if(!L->elem) exit(-1);/*存储分配失败*/
L->length=0;
L->listsize=LISTSIZE;
return 1;
}
int ListInsert(Sqlist *L,int i,int e){/*在指定位置插入一个元素*/
if (i<1||i>L->length+1) printf("ERROR");
int *q=&(L->elem[i-1]);
int *p;
for (p=&(L->elem[L->length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L->length;
return 1;
}
int Delete(Sqlist *L,int a,int k){/*通过向前移动位置来删除指定位置的元素*/
int i,j;
for (i=k+1;i<L->length;i++){
if (L->elem[i]!=a)
continue;
else {
for (j=i+1;j<L->length;j++){
L->elem[j-1]=L->elem[j];
}
L->length--;
i--;/*每次循环后都要减一来保证i等于k+1*/
}
}
return 1;
}
int main(){
int m;
scanf("%d",&m);
while(m){
Sqlist L;
int t=1,d,n,i;
SqInitial(&L);
scanf("%d",&n);
for (t=1;t<=n;t++){
scanf("%d",&d);
ListInsert(&L,t,d);
}
for (i=0;i<L.length;i++)
Delete(&L,L.elem[i],i);
for (t=1;t<=L.length;t++)
printf("%d ",L.elem[t-1]);
printf ("\n");
m--;
}
}
刚开始写完后,出现了一个错误,只要输入几个相同的数,就会输出两个一样的数(应该只输出一个的),检查了好长时间,原来是循环的过程中忘记把 i 的值减一了,导致每个数第二次的比较出现错误。上午听了一上午的课,又写了一下午,终于做出了这道题,真的好难啊!!!不过,终于迈出了第一步,值得高兴,继续努力,加油!