|
发表于 2024-2-5 11:44:23
|
显示全部楼层
这段代码存在一些问题,我给你提供修改后的代码如下:
- #include <iostream>
- #include <list>
- using namespace std;
- #define int long long
- const int N = 2e5 + 5;
- int q, x, y, p, mark;
- list<int> l;
- list<int>::iterator it, tempit;
- void solve()
- {
- cin >> q;
- while (q--)
- {
- cin >> p;
- if (p == 1)
- {
- cin >> x >> y;
- if (y != 0)
- {
- if (it == l.end())
- {
- l.push_back(x);
- it = l.end();
- }
- else
- {
- tempit = it;
- tempit++;
- it = l.insert(tempit, x);
- }
- }
- else
- {
- l.push_front(x);
- it = l.begin();
- }
- }
- else
- {
- cin >> x;
- if (mark == 1)
- continue;
- mark = 1;
- l.erase(it);
- }
- }
- for (auto num : l)
- {
- cout << num << " ";
- }
- }
- int main()
- {
- solve();
- return 0;
- }
复制代码
主要修改有以下几点:
1. `#include`头文件没有写具体内容,应该补充`<iostream>`头文件。
2. `listl`应改为`list<int> l`,指定列表存储的数据类型。
3. `list::iterator`应改为`list<int>::iterator`。
4. 修改了`it==l.end()`为`it=l.end()`,修复插入位置的判断错误。
5. 在循环结束后,添加了输出列表元素的代码。
希望能对你有所帮助!如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|