鱼C论坛

 找回密码
 立即注册
查看: 1026|回复: 1

程序段错误,修改代码

[复制链接]
发表于 2024-2-5 11:44:23 | 显示全部楼层
这段代码存在一些问题,我给你提供修改后的代码如下:

  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4. #define int long long

  5. const int N = 2e5 + 5;
  6. int q, x, y, p, mark;
  7. list<int> l;
  8. list<int>::iterator it, tempit;

  9. void solve()
  10. {
  11.     cin >> q;
  12.     while (q--)
  13.     {
  14.         cin >> p;
  15.         if (p == 1)
  16.         {
  17.             cin >> x >> y;
  18.             if (y != 0)
  19.             {
  20.                 if (it == l.end())
  21.                 {
  22.                     l.push_back(x);
  23.                     it = l.end();
  24.                 }
  25.                 else
  26.                 {
  27.                     tempit = it;
  28.                     tempit++;
  29.                     it = l.insert(tempit, x);
  30.                 }
  31.             }
  32.             else
  33.             {
  34.                 l.push_front(x);
  35.                 it = l.begin();
  36.             }
  37.         }
  38.         else
  39.         {
  40.             cin >> x;
  41.             if (mark == 1)
  42.                 continue;
  43.             mark = 1;
  44.             l.erase(it);
  45.         }
  46.     }
  47.     for (auto num : l)
  48.     {
  49.         cout << num << " ";
  50.     }
  51. }

  52. int main()
  53. {
  54.     solve();
  55.     return 0;
  56. }
复制代码


主要修改有以下几点:
1. `#include`头文件没有写具体内容,应该补充`<iostream>`头文件。
2. `listl`应改为`list<int> l`,指定列表存储的数据类型。
3. `list::iterator`应改为`list<int>::iterator`。
4. 修改了`it==l.end()`为`it=l.end()`,修复插入位置的判断错误。
5. 在循环结束后,添加了输出列表元素的代码。

希望能对你有所帮助!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-23 07:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表