/*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,k=0,s=0;
for (i=0; i< L->listLen - 1 ; i++) {
if (L->data[i] == L->data[i + 1]) {
for (int j = i + 1; j < L->listLen - 1; j++) {
L->data[j - 1] = L->data[j];
L->listLen--;
k++;//k记录移动次数
}
i--;
}
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;
}
这个代码没有报错,但是输完表中元素后就什么也打印不出来了,为什么呢,
然后我在main函数里把listDelete(L);注释掉,又可以打印出表中内容了,我感觉是 listDelete函数有问题,可是不知道哪里有问题