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

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


/*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;
}

想问一下,这个代码是哪里有问题吗?为什么打印结果是这个

img

  • 写回答

2条回答 默认 最新

  • threenewbee 2023-03-18 13:28
    关注

    调试输出下,看看 delete 对不对

    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 关于#c语言#的问题:我在vscode和codeblocks中编写c语言时出现打不开源文件该怎么办
  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表
  • ¥15 Altair EDEM中生成一个颗粒,并且各个方向没有初始速度
  • ¥15 系统2008r2 装机配置推荐一下
  • ¥500 服务器搭建cisco AnyConnect vpn
  • ¥15 悬赏Python-playwright部署在centos7上
  • ¥15 psoc creator软件有没有人能远程安装啊
  • ¥15 快速扫描算法求解Eikonal方程咨询