-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
181 lines (159 loc) · 4.44 KB
/
Solution.cs
File metadata and controls
181 lines (159 loc) · 4.44 KB
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
173
174
175
176
177
178
179
180
181
public class MyCircularDeque
{
public class DoubleLinkedList
{
public DoubleLinkedList(int value)
{
Value = value;
Previous = Next = null;
}
public int Value { get; set; }
public DoubleLinkedList Previous { get; set; }
public DoubleLinkedList Next { get; set; }
}
private readonly int _maxLength;
private int _currentLength;
private DoubleLinkedList _list = null;
private DoubleLinkedList _head = null;
private DoubleLinkedList _tail = null;
public MyCircularDeque(int k)
{
_maxLength = k;
_currentLength = 0;
_head = _tail = _list = null;
}
public bool InsertFront(int value)
{
if (_list is null)
{
_head = _tail = _list = new DoubleLinkedList(value);
_currentLength++;
return true;
}
if (_currentLength >= _maxLength) return false;
var newNode = new DoubleLinkedList(value);
newNode.Next = _head;
_head.Previous = newNode;
_head = newNode;
_currentLength++;
return true;
}
public bool InsertLast(int value)
{
if (_list is null)
{
_head = _tail = _list = new DoubleLinkedList(value);
_currentLength++;
return true;
}
if (_currentLength >= _maxLength) return false;
var newNode = new DoubleLinkedList(value);
newNode.Previous = _tail;
_tail.Next = newNode;
_tail = newNode;
_currentLength++;
return true;
}
public bool DeleteFront()
{
if (_head is null) return false;
_currentLength--;
if (_currentLength == 0)
{
_head = _tail = _list = null;
return true;
}
_head = _head.Next;
if (_head is not null)
{
_head.Previous = null;
}
return true;
}
public bool DeleteLast()
{
if (_tail is null) return false;
_currentLength--;
if (_currentLength == 0)
{
_head = _tail = _list = null;
return true;
}
_tail = _tail.Previous;
if (_tail is not null)
{
_tail.Next = null;
}
return true;
}
public int GetFront()
{
if (_head is null) return -1;
return _head.Value;
}
public int GetRear()
{
if (_tail is null) return -1;
return _tail.Value;
}
public bool IsEmpty()
{
return _currentLength == 0;
}
public bool IsFull()
{
return _currentLength == _maxLength;
}
}
public class Solution
{
public List<string> Execute(string[] actions, int[][] values)
{
List<string> result = [];
MyCircularDeque circularDeque = new(0);
for (int i = 0; i < actions.Length; i++)
{
switch (actions[i])
{
case "MyCircularDeque":
circularDeque = new MyCircularDeque(values[i][0]);
result.Add("null");
break;
case "insertLast":
result.Add(circularDeque.InsertLast(values[i][0]).ToString());
break;
case "insertFront":
result.Add(circularDeque.InsertFront(values[i][0]).ToString());
break;
case "getRear":
result.Add(circularDeque.GetRear().ToString());
break;
case "isFull":
result.Add(circularDeque.IsFull().ToString());
break;
case "deleteLast":
result.Add(circularDeque.DeleteLast().ToString());
break;
case "getFront":
result.Add(circularDeque.GetFront().ToString());
break;
default:
result.Add("null");
break;
}
}
return result;
}
}
/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque obj = new MyCircularDeque(k);
* bool param_1 = obj.InsertFront(value);
* bool param_2 = obj.InsertLast(value);
* bool param_3 = obj.DeleteFront();
* bool param_4 = obj.DeleteLast();
* int param_5 = obj.GetFront();
* int param_6 = obj.GetRear();
* bool param_7 = obj.IsEmpty();
* bool param_8 = obj.IsFull();
*/