#include<bits/stdc++.h> using namespace std; typedef struct Node * ptrtonode; typedef struct Node { int data,order; ptrtonode next; } node; int main() { ptrtonode head=new node(); head->next=NULL; int n,a,b; char c; scanf("%d",&n); int o=1; for(int i=1; i<=n; i++) { ptrtonode q; ptrtonode p; cin>>c; if(c=='H') { p=new node(); scanf("%d",&a); p->data=a; p->order=o; o++; p->next=head->next; head->next=p; } else if(c=='D') { cin>>a; if(a==0) { p=head->next; head->next=p->next; free(p); } else { q=head->next; while(q->order!=o) { q=q->next; } p=q->next; q->next=p->next; } } else if(c=='I') { scanf("%d%d",&a,&b); p=head->next; while(p->order!=a) { p=p->next; } q=new node(); q->data=b; q->order=o; o++; q->next=p->next; p->next=q; } } head=head->next; while(head) { cout<<head->data<<" "<<head->order<<endl; head=head->next; } return 0; }哪里有错误
时间: 2023-12-19 16:05:15 浏览: 187
这段代码的错误是使用了 `free()` 函数,而 `free()` 函数是 C 语言的函数,在 C++ 中应该使用 `delete` 或 `delete[]` 来释放内存。另外,使用 `cin` 读入字符时应该加上 `#include <iostream>`。下面是修改过的代码:
```c++
#include<iostream>
using namespace std;
typedef struct Node * ptrtonode;
typedef struct Node {
int data, order;
ptrtonode next;
} node;
int main() {
ptrtonode head = new node();
head->next = NULL;
int n, a, b;
char c;
cin >> n;
int o = 1;
for(int i = 1; i <= n; i++) {
ptrtonode q;
ptrtonode p;
cin >> c;
if(c == 'H') {
p = new node();
cin >> a;
p->data = a;
p->order = o;
o++;
p->next = head->next;
head->next = p;
}
else if(c == 'D') {
cin >> a;
if(a == 0) {
p = head->next;
head->next = p->next;
delete p;
}
else {
q = head->next;
while(q->order != o) {
q = q->next;
}
p = q->next;
q->next = p->next;
delete p;
}
}
else if(c == 'I') {
cin >> a >> b;
p = head->next;
while(p->order != a) {
p = p->next;
}
q = new node();
q->data = b;
q->order = o;
o++;
q->next = p->next;
p->next = q;
}
}
head = head->next;
while(head) {
cout << head->data << " " << head->order << endl;
head = head->next;
}
return 0;
}
```
阅读全文