/*6.删除递增有序顺序表中的重复元素,并统计移动元素次数,要求时间性能最好。
实验测试数据基本要求:
第一组数据:顺序表元素为 (1,2,3,4,5,6,7,8,9)
第二组数据:顺序表元素为 (1,1,2,2,2,3,4,5,5,5,6,6,7,7,8,8,9)
第三组数据:顺序表元素为 (1,2,3,4,5,5,6,7,8,8,9,9,9,9,9)
*/
#include <iostream>
#include<stdlib.h>
using namespace std;
#define max 100
typedef int element;
typedef struct sList {
element data[max];
int listLen;
}seqList;
void initialList(seqList* L) {
L->listLen = 0;
}
int listDelete(seqList* L) {
int i= L->listLen,k=0,s=0;
for (i = L->listLen; i > 0; i--) {
if (L->data[i] == L->data[i - 1])
for (int j = i; j > 0; j--) {
L->data[j - 1] = L->data[j];
L->listLen--;
k++;//k记录移动次数
}
s += k;
}
return s;
}
bool listAdd(seqList* L) {
if (L->listLen == max)
return false;
else {
element x = 1;
cout << "请输入一组数据:(9999退出)";
while (x != 9999) {
cin >> x;
if (x == 9999)
break;
L->data[L->listLen++] = x;
}
return true;
}
}
void listPrint(seqList* L) {
for (int i = 0; i < L->listLen; i++) {
cout << L->data[i] << " ";
}
}
int main() {
seqList* L = new seqList;
initialList(L);
listAdd(L);
listDelete(L);
listPrint(L);
int s= listDelete(L);
cout << "移动次数:" << s << endl;
}
想问一下,这个代码是哪里有问题吗?为什么打印结果是这个