#include
#include
#include
#define LISTINCREASMENT 100
#define LISTSIZE 10
#define OVERFLOW -1
#define OK 1
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}Sqlist;
int SqInitial(Sqlist &L)
{
L.elem=(ElemType *)malloc(LISTSIZE*sizeof(ElemType));
if(!L.elem) exit(OVERFLOW);
L.length=0;
L.listsize=LISTSIZE;
return OK;
}
int ListInsert(Sqlist &L,int i,ElemType e)
{
if(i<1||i>L.length+1) printf("ERROR");
if(L.length>=L.listsize)
{
ElemType*newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREASMENT)*sizeof(ElemType));
if(!newbase) return OVERFLOW;
L.elem=newbase;
L.listsize+=LISTINCREASMENT;
}
ElemType * q=&(L.elem[i-1]);
ElemType * p;
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return OK;
}
void purge(Sqlist &L)
{
int i,j,k;
for(i=0;i
j=i+1;
while(j
++j;
else
{
for(k=j+1;k
--L.length;
}
}
}
int main()
{
Sqlist L;
int i,n,d;
scanf("%d",&n);
SqInitial(L);
for(i=1;i<=n;i++)
{
scanf("%d",&d);
ListInsert(L,i,d);
}
purge(L);
printf("%d\n",L.length);
for(i=1;i<=L.length;i++)
{
printf("%d%c",L.elem[i-1],(i==L.length)?'\n':' ');
}
return 0;
}
/**************************************
Problem id : SDUT OJ A
User name : wy150428臧志栋
Result : Accepted
Take Memory : 284K
Take Time : 0MS
Submit Time : 2016-08-02 20:25:32
**************************************/
三个函数
SqInitial 用于创建与清空顺序表
ListInsert 用于给已经创建好的顺序表添加数据,并且对于输入结果进行判断
purge 逐一的浏览顺序表 若有重复 指针后移动
该算法的算法的时间复杂度为: O(L.length3)