关于pta上题，用链表解答是出现段错误

0
[待解决问题]

I x：表示编号为x的人从队尾进入队伍

C x y:表示编号为x的人插队，排在y前一位

L x：表示编号为x的人从队伍中离开（这个人有可能还会回来）

4 4
I 1
I 2
C 3 2
C 4 1

4 1 3 2

include<bits/stdc++.h>

using namespace std;
struct q{
int number;
struct q* next;
};
void rudui(int n,struct q** tail){//末尾入队操作
struct q* temp=new q{n,NULL};
(tail)->next=temp;
tail=temp;
}
void chadui(int x,int y, struct q
struct q
struct q* befor=temp;
while(temp->number!=y){
befor=temp;
temp=temp->next;
}
struct q* cha=new q{x,NULL};
} else {
befor->next=cha;
cha->next=temp;
}

}
struct q* befor=temp;
while(temp->number!=x){
befor=temp;
temp=temp->next;
}
}else{
befor->next=temp->next;
}
}
int main(){
int n,m;
struct q* tail;
struct q* body;
cin>>n>>m;
int i;
for(i=0;i<m;i++){
char c;
int x;
cin>>c>>x;
if(c
'I'){
rudui(x,&tail);
}else if(c'C'){
int y;
cin>>y;
}else if(c
'L'){
}
}
while(body!=NULL){//输出
cout<<body->number<<' ';
body=body->next;
}

``````return 0;
``````

}

GGBond* | 菜鸟二级 | 园豆：204

0

``````#include <iostream>
#include <unordered_map>

using namespace std;

struct Node {
int id;
Node* next;
Node(int id) : id(id), next(nullptr) {}
};

class Queue {
private:
Node* tail;
unordered_map<int, Node*> idToNode;

public:

void enqueue(int id) {
Node* newNode = new Node(id);
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
idToNode[id] = newNode;
}

void dequeue(int id) {
Node* prev = nullptr;
while (curr && curr->id != id) {
prev = curr;
curr = curr->next;
}

if (prev) {
prev->next = curr->next;
} else {
}
if (curr == tail) {
tail = prev;
}
idToNode.erase(id);
delete curr;
}

void insertBefore(int id, int beforeId) {
Node* beforeNode = idToNode[beforeId];
Node* newNode = new Node(id);
} else {
while (prev->next && prev->next != beforeNode) {
prev = prev->next;
}
if (prev->next == beforeNode) {
newNode->next = beforeNode;
prev->next = newNode;
}
}
idToNode[id] = newNode;
}

void printQueue() {
cout << "Bai Ren Zhan" << endl;
return;
}
while (curr) {
cout << curr->id << " ";
curr = curr->next;
}
cout << endl;
}
};

int main() {
int N, M;
cin >> N >> M;
Queue q;
char type;
int id, beforeId;
for (int i = 0; i < M; ++i) {
cin >> type;
if (type == 'I') {
cin >> id;
q.enqueue(id);
} else if (type == 'C') {
cin >> id >> beforeId;
q.insertBefore(id, beforeId);
} else if (type == 'L') {
cin >> id;
q.dequeue(id);
}
}
q.printQueue();
return 0;
}
``````

您需要登录以后才能回答，未注册用户请先注册