设计并验证以下算法:
设顺序表L中的数据元素为整数且非递增有序,删除其值相同的多余元素,即顺序表L中相同的元素只保留一个,并逆置删除后的顺序表L。
(1)根据键盘输入数据建立顺序表L
(2)输出顺序表L、删除值相同多余元素后的顺序表L、逆置的顺序表L。
(3)假设顺序表L的长度为n,要求以O(n)的时间复杂度完成对值相同多余元素的删除
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 1000
typedef struct LNode *List;
struct LNode {
int Data[MAXSIZE];
int Last;
};
List MakeEmpty();
bool Insert( List L, int X, int P);
void PrintNum(List L);
bool Deletesame(List L,List L1,List L2);
bool reserve(List L,List L1,List L2);
int main()
{
int N,X;
List L;
List L1;
List L2;
L = MakeEmpty();
L1 = MakeEmpty();
L2 = MakeEmpty();
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
int i = L->Last;
Insert(L, X, i+1);
}
PrintNum(L);
Deletesame(L,L1,L2);
reserve(L,L1,L2);
PrintNum(L1);
PrintNum(L2);
}
List MakeEmpty()
{
List L;
L = (List)malloc(sizeof(struct LNode));
L->Last = -1;
return L;
}
bool Insert(List L, int X, int P)
{
int j;
if (L->Last == MAXSIZE-1)
{
printf("FULL");
return false;
}
for (j = L->Last;j >= P;j--)
L->Data[j + 1] = L->Data[j];
L->Data[P] = X;
L->Last++;
return true;
}
void PrintNum(List L)
{
for(int i=0;i<=L->Last;i++)
{
printf("%d ",L->Data[i]);
}
printf("\n");
}
bool Deletesame(List L,List L1,List L2)
{ int i,j;
int x=0;
int a[100]={0};
for(i=0;i<=L->Last;i++)
{
j=L->Data[i];
if(a[j]!=1)
{
L1->Data[x]=j;
x++;
L1->Last++;
}
a[j]=1;
}
return true;
}
bool reserve(List L,List L1,List L2)
{
int i=0;
int k = L1->Last + 1 ;
while(k)
{
L2->Data[i] = L1->Data[k-1];
i++;
L2->Last++;
k--;
}
return true;
}