c++去除有序顺序表中的重复数

#include<iostream>

#define MaxSize 50
#define ElemType int
using namespace std;

typedef struct {
	ElemType data[MaxSize];
	int length;
}SqList;

void Init(SqList &L) {
	ElemType a;
	int n;  //初始的个数
	cout << "Please enter the number of elements : ";
	cin >> n;
	cout << "Please enter elements:";
	for (int i = 0; i < n; i++) {
		cin >> a;
		L.data[i] = a;
	}
	L.length = n;
}

bool Place_to_repeat(SqList &L) {

	if (L.length == 0) return false;

	int i = 1;
	int k = 1;  //表示存在的个数
	ElemType temp = L.data[0]; //作为第一个比较数
	while (i < L.length ) {
		if (temp != L.data[i]) {
			L.data[k] = L.data[i];
			k++;
			i++;
			temp = L.data[i];  //作为新的比较数
		}
		else {
			i++;
		}
	}
	L.length = k;
	return true;
}

void Print(SqList &L) {   //输出顺序表中的元素
	int i;
	for (i = 0; i < L.length; i++) {
		cout << L.data[i] << " ";
	}
	cout << endl;
}

int main() {

	SqList L;
	Init(L);  //初始化
	Place_to_repeat(L);
	Print(L);
	system("pause");
	return 0;
}

/*
15
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
*/


猜你喜欢

转载自blog.csdn.net/coolsunxu/article/details/80114228
今日推荐