Go
package main
import (
"bufio"
"fmt"
"os"
)
type Node struct {
val, id int
prev, next *Node
}
func NewNode(val, id int) *Node {
return &Node{val: val, id: id, prev: nil, next: nil}
}
type List struct {
head, tail *Node
id int
ids map[int]*Node
}
func NewList() *List {
list := &List{
head: nil,
tail: nil,
id: 1,
ids: make(map[int]*Node),
}
return list
}
func (l *List) HeadInsert(x int) {
s := NewNode(x, l.id)
l.id++
if l.head == nil {
l.head, l.tail = s, s
} else {
s.next = l.head
l.head.prev = s
l.head = s
}
l.ids[s.id] = s
}
func (l *List) TailInsert(x int) {
s := NewNode(x, l.id)
l.id++
if l.tail == nil {
l.tail, l.head = s, s
} else {
s.prev = l.tail
l.tail.next = s
l.tail = s
}
l.ids[s.id] = s
}
func (l *List) DeleteKthInserted(k int) {
p := l.ids[k]
delete(l.ids, p.id)
if p == l.head && p == l.tail {
l.head, l.tail = nil, nil
} else if p == l.head {
l.head = p.next
l.head.prev = nil
} else if p == l.tail {
l.tail = p.prev
l.tail.next = nil
} else {
p.prev.next = p.next
p.next.prev = p.prev
}
}
func (l *List) KthPrevInsert(k, x int) {
p := l.ids[k]
if p == l.head {
l.HeadInsert(x)
} else {
s := NewNode(x, l.id)
l.id++
s.prev = p.prev
s.next = p
p.prev.next = s
p.prev = s
l.ids[s.id] = s
}
}
func (l *List) KthNextInsert(k, x int) {
p := l.ids[k]
if p == l.tail {
l.TailInsert(x)
} else {
s := NewNode(x, l.id)
l.id++
s.next = p.next
s.prev = p
p.next.prev = s
p.next = s
l.ids[s.id] = s
}
}
func (l List) Display() {
p := l.head
for p != nil {
fmt.Printf("%d ", p.val)
p = p.next
}
fmt.Println()
}
func main() {
in := bufio.NewReader(os.Stdin)
var m int
fmt.Fscanln(in, &m)
list := NewList()
var k, x int
for i := 0; i < m; i++ {
var op string
fmt.Fscan(in, &op)
switch op {
case "L":
fmt.Fscan(in, &x)
list.HeadInsert(x)
case "R":
fmt.Fscan(in, &x)
list.TailInsert(x)
case "D":
fmt.Fscan(in, &k)
list.DeleteKthInserted(k)
case "IL":
fmt.Fscan(in, &k, &x)
list.KthPrevInsert(k, x)
case "IR":
fmt.Fscan(in, &k, &x)
list.KthNextInsert(k, x)
}
}
list.Display()
}
C++
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
class List {
public:
List(): head_(nullptr), tail_(nullptr), ids_(unordered_map<int, Node*>()), id_(1) {}
~List() {
while (head_) {
Node *next = head_->next_;
delete head_;
head_ = next;
}
}
void HeadInsert(int x) {
auto s = new Node(x, id_++);
if (head_ == nullptr) {
head_ = s;
tail_ = head_;
} else {
s->next_ = head_;
head_->prev_ = s;
head_ = s;
}
ids_[s->id_] = s;
}
void TailInsert(int x) {
auto s = new Node(x, id_++);
if (tail_ == nullptr) {
tail_ = s;
head_ = tail_;
} else {
s->prev_ = tail_;
tail_->next_ = s;
tail_ = s;
}
ids_[s->id_] = s;
}
void DeleteKthInserted(int k) {
Node *p = ids_[k];
ids_.erase(p->id_);
if (p == head_ && p == tail_) {
head_ = tail_ = nullptr;
} else if (p == head_) {
head_ = p->next_;
head_->prev_ = nullptr;
} else if (p == tail_) {
tail_ = p->prev_;
tail_->next_ = nullptr;
} else {
p->prev_->next_ = p->next_;
p->next_->prev_ = p->prev_;
}
delete p;
}
void KthPrevInsert(int k, int x) {
Node *p = ids_[k];
if (p == head_) {
HeadInsert(x);
} else {
auto s = new Node(x, id_++);
s->prev_ = p->prev_;
s->next_ = p;
p->prev_->next_ = s;
p->prev_ = s;
ids_[s->id_] = s;
}
}
void KthNextInsert(int k, int x) {
Node *p = ids_[k];
if (p == tail_) {
TailInsert(x);
} else {
auto s = new Node(x, id_++);
s->prev_ = p;
s->next_ = p->next_;
p->next_->prev_ = s;
p->next_ = s;
ids_[s->id_] = s;
}
}
void Display() {
Node *p = head_;
while (p) {
cout << p->val_ << " ";
p = p->next_;
}
cout << endl;
}
private:
struct Node {
int val_, id_;
Node *prev_, *next_;
Node(int val, int id): val_(val), id_(id), prev_(nullptr), next_(nullptr) {}
};
Node *head_, *tail_;
unordered_map<int, Node*> ids_;
int id_;
};
int main() {
int M;
cin >> M;
List list;
int x, k;
while (M--) {
string op;
cin >> op;
if (op == "L") {
cin >> x;
list.HeadInsert(x);
} else if (op == "R") {
cin >> x;
list.TailInsert(x);
} else if (op == "D") {
cin >> k;
list.DeleteKthInserted(k);
} else if (op == "IL") {
cin >> k >> x;
list.KthPrevInsert(k, x);
} else if (op == "IR") {
cin >> k >> x;
list.KthNextInsert(k, x);
}
}
list.Display();
return 0;
}