-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList.cpp
52 lines (44 loc) · 906 Bytes
/
linkedList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
using namespace std;
struct Node {
Node *prev, *next;
int num;
Node(int num_ = -1) : prev(NULL), next(NULL), num(num_) {}
};
Node* nil;
void init() {
nil = new Node();
nil->prev = nil;
nil->next = nil;
}
void printList() {
Node* cur = nil->next;
for(; cur != nil; cur = cur->next) {
cout << cur->num << "->";
}
cout << endl;
}
void insert(Node* v, Node* p = nil) {
v->next = p->next;
p->next->prev = v;
p->next = v;
v->prev = p;
}
void erase(Node *v) {
if (v==nil) return;
v->prev->next = v->next;
v->next->prev = v->prev;
delete v;
}
int main() {
int N; cin >> N;
init();
vector<int> A(N); for(auto &a : A) cin >> a;
reverse(A.begin(), A.end());
for (int i = 0; i < N; ++i) {
Node* node = new Node(A[i]);
insert(node);
}
printList();
}