单链表普通实现
作者:
东方不会敲代码
,
2024-04-06 20:54:55
,
所有人可见
,
阅读 12
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+10;
typedef struct node {
int num;
node* next;
}*pointer;
pointer res[N];
int top = 1;
void H(int x,pointer head) {
pointer p = new node;
p->num = x;
res[top++] = p;
p->next = head->next;
head->next = p;
}
void D(int k){
pointer p = res[k];
p->next = p->next->next;
}
void I(int k,int x){
pointer p = new node;
p->num = x;
res[top++] = p;
pointer q = res[k];
p->next = q->next;
q->next = p;
}
int main() {
int M; cin >> M;
pointer head = new node;
head->next = nullptr;
res[0]=head;
while (M--) {
char ch; scanf(" %c", &ch);
if (ch == 'H') {
//cout<<"H(x)"<<endl;
int x;cin >> x;
H(x, head);
}
else if (ch == 'D') {
int k;
cin >> k;
D(k);
}
else if (ch == 'I') {
//cout<<"I(k,x)"<<endl;
int k, x;
cin >> k >> x;
I(k,x);
}
}
pointer p = head->next;
while (p!=nullptr) {
cout << p->num << " ";
p = p->next;
}
}
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~