-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0707_MyLinkedList.cc
172 lines (158 loc) · 2.99 KB
/
Problem_0707_MyLinkedList.cc
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <assert.h>
#include <iostream>
#include "UnitTest.h"
using namespace std;
class MyLinkedList
{
private:
class Node
{
public:
int val;
Node *pre;
Node *next;
Node(int val)
{
this->val = val;
this->pre = nullptr;
this->next = nullptr;
}
};
Node *head;
Node *tail;
size_t size;
public:
MyLinkedList()
{
head = new Node(0);
tail = new Node(0);
head->next = tail;
tail->pre = head;
size = 0;
}
~MyLinkedList() {}
int get(int index)
{
if (index < 0 || index >= size)
{
return -1;
}
Node *anchor;
if (index + 1 < size - index) // 多一个头结点 + 1
{
anchor = head;
for (int i = 0; i <= index; i++) // 因为多一个头结点,所以取 <=
{
anchor = anchor->next;
}
}
else
{
anchor = tail;
for (int i = 0; i < size - index; i++)
{
anchor = anchor->pre;
}
}
return anchor->val;
}
void addAtHead(int val) { addAtIndex(0, val); }
void addAtTail(int val) { addAtIndex(size, val); }
void addAtIndex(int index, int val)
{
if (index < 0 || index > size)
{
return;
}
Node *pre, *anchor;
if (index < size - index)
{
pre = head;
for (int i = 0; i < index; i++)
{
pre = pre->next;
}
anchor = pre->next;
}
else
{
anchor = tail;
for (int i = 0; i < size - index; i++)
{
anchor = anchor->pre;
}
pre = anchor->pre;
}
size++;
Node *cur = new Node(val);
cur->pre = pre;
cur->next = anchor;
pre->next = cur;
anchor->pre = cur;
}
void deleteAtIndex(int index)
{
if (index < 0 || index >= size)
{
return;
}
Node *pre, *anchor;
if (index < size - index)
{
pre = head;
for (int i = 0; i < index; i++)
{
pre = pre->next;
}
anchor = pre->next->next;
}
else
{
anchor = tail;
for (int i = 0; i < size - index - 1; i++)
{
anchor = anchor->pre;
}
pre = anchor->pre->pre;
}
size--;
Node *p = pre->next;
pre->next = anchor;
anchor->pre = pre;
delete p;
}
};
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList* obj = new MyLinkedList();
* int param_1 = obj->get(index);
* obj->addAtHead(val);
* obj->addAtTail(val);
* obj->addAtIndex(index,val);
* obj->deleteAtIndex(index);
*/
void testMyLinkedList()
{
MyLinkedList list;
list.addAtHead(1);
list.addAtTail(3);
list.addAtIndex(1, 2);
EXPECT_EQ_INT(2, list.get(1));
list.deleteAtIndex(1);
EXPECT_EQ_INT(3, list.get(1));
list.deleteAtIndex(2);
EXPECT_EQ_INT(3, list.get(1));
list.addAtIndex(1, 4);
EXPECT_EQ_INT(4, list.get(1));
list.deleteAtIndex(0);
EXPECT_EQ_INT(3, list.get(1));
list.deleteAtIndex(0);
EXPECT_EQ_INT(3, list.get(0));
list.deleteAtIndex(0);
EXPECT_SUMMARY;
}
int main()
{
testMyLinkedList();
return 0;
}