初冀 2023-03-31 18:39 采纳率: 61%
浏览 12
已结题

顺序表删除重复元素为什么打印不出结果


/*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函数有问题,可是不知道哪里有问题

  • 写回答

2条回答 默认 最新

  • 真相重于对错 2023-03-31 18:55
    关注

    顺序表用快慢指针发就可以了

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月9日
  • 创建了问题 3月31日

悬赏问题

  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,
  • ¥15 angular项目错误