从有序表中删除重复元素,使表中所有元素值均不相同。
#include <iostream>
using namespace std;
typedef int ElemType;
#define Maxsize 100
#define OK 1
#define ERROR 0
typedef struct SqList
{
ElemType data[Maxsize];
int length;
}SqList;
void Init_SqList(SqList& L)
{
L.length = 0;
}
void delete_samevalue(SqList &L)
{
int k = 0;
for (int i = 1; i < L.length; i++)
{
if (L.data[k] != L.data[i])
L.data[++k] = L.data[i];
}
L.length = k+1;
}
//删除重复元素3
//length=7
//0 1 2 3 4 5 6
//5 3 3 2 2 1 10
/*从有序表中删除重复元素,使表中所有元素值均不相同*/
int main(void)
{
SqList L;
Init_SqList(L);
L.data[0] = 1;
L.data[1] = 2;
L.data[2] = 2;
L.data[3] = 3;
L.data[4] = 3;
L.data[5] = 4;
L.data[6] = 10;
L.length = 7;
for (int i = 0; i < L.length; i++)
cout << L.data[i] << " ";
cout << endl;
delete_samevalue(L);
for (int i = 0; i < L.length; i++)
cout << L.data[i] << " ";
cout << endl;
return 0;
}