69,787
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
struct node {
int data;
node* next;
};
typedef struct node pnode;
void freeList(pnode* head) {
pnode* tmp;
while (head != nullptr) {
tmp = head;
head = head->next;
delete tmp;
}
}
int main() {
int n;
cin >> n;
pnode* head = new pnode;
int x;
cin >> x;
head->data = x;
head->next = NULL;
pnode* p = head;
pnode* tmp = head;
pnode* s;
for (int i = n;i!=1;i--) {
s = new pnode;
p->next = s;
p = s;
p->next = NULL;
cin >> x;
p->data = x;
}
int m,flag,k,d;
cin >> m;
while (m != 0) {
cin >> flag;
cin >> k;
if (flag == 0) {
cin >> d;
}
if (flag == 0&&k<=n) {
if (k == 0) {
tmp = new pnode;
tmp->data = d;
tmp->next = head;
head = tmp;
}
else {
p = head;
while (k != 1) {
p = p->next;
k--;
}
tmp = new pnode;
tmp->data = d;
tmp->next = p->next;
p->next = tmp;
}
}
if (flag == 1&&k!=0) {
p = head;
tmp = head;
if (k == 1) {
head = head->next;
tmp->next = NULL;
delete tmp;
}
else {
while (k != 1) {
if (p != head) {
tmp = tmp->next;
}
p = p->next;
k--;
}
tmp->next = p->next;
p->next = NULL;
delete p;
}
}
m--;
}
p= head;
while (p!= NULL) {
cout << p->data << " ";
p = p->next;
}
freeList(head);
return 0;
}
哪里有段错误啊,找不到,救救我
你输入了啥?啥时候段错误的?