Skip to content

Commit e613587

Browse files
committed
feat: add stack gif image, update coding style
1 parent 334884d commit e613587

File tree

6 files changed

+73
-47
lines changed

6 files changed

+73
-47
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@
2222
"C_Cpp.errorSquiggles": "Disabled",
2323
"fileheader.LastModifiedBy": "Chacha",
2424
"fileheader.Author": "Chacha",
25-
"editor.tabSize": 4
25+
"editor.tabSize": 4,
26+
"cSpell.words": [
27+
"inorder"
28+
]
2629
}

basic_data_structure/heap/heap.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @Author: Chacha
3+
* @Date: 2021-03-03 16:06:05
4+
* @Last Modified by: Chacha
5+
* @Last Modified time: 2021-03-03 17:30:01
6+
*/
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
/**
2-
* @Author: Chacha
3-
* @Date: 2018-12-30 13:12:05
2+
* @Author: Chacha
3+
* @Date: 2018-12-30 13:12:05
44
* @Last Modified by: Chacha
55
* @Last Modified time: 2018-12-30 17:00:28
66
*/
77

88
#include<iostream>
99
using namespace std;
1010

11-
/**
11+
/**
1212
* Struct definition for singly-linked list(单向链表)
1313
* Source: https://zh.wikipedia.org/wiki/链表
1414
*/
1515
struct ListNodeA {
1616
int val;
1717
ListNodeA *next;
18+
1819
ListNodeA(int x) : val(x), next(NULL) {}
1920
};
2021

@@ -25,6 +26,7 @@ struct ListNodeA {
2526
public:
2627
int val;
2728
ListNodeB *next;
29+
2830
ListNodeB(int val) {
2931
this->val = val;
3032
this->next = NULL;
@@ -34,12 +36,13 @@ struct ListNodeA {
3436
/**
3537
* Class definition for doubly-linked list(双向链表)
3638
*/
37-
class DListNode() {
39+
class DListNode {
3840
public:
3941
int val;
40-
DListNode prev, next;
42+
DListNode *prev, *next;
43+
4144
DListNode(int val) {
42-
this.val = val;
43-
this.prev = this.next = NULL;
45+
this->val = val;
46+
this->prev = this->next = NULL;
4447
}
45-
}
48+
};

basic_data_structure/queue/queue.cpp

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* @Author: Chacha
3-
* @Date: 2018-12-03 17:14:17
2+
* @Author: Chacha
3+
* @Date: 2018-12-03 17:14:17
44
* @Last Modified by: Chacha
55
* @Last Modified time: 2018-12-12 21:50:07
66
*/
@@ -21,16 +21,21 @@ struct TreeNode {
2121
class MyQueue {
2222
private:
2323
// store elements
24-
vector<int> data;
24+
vector<int> data;
2525
// a pointer to indicate the start position
26-
int p_start;
26+
int p_start;
27+
2728
public:
28-
MyQueue() {p_start = 0;}
29+
MyQueue() {
30+
p_start = 0;
31+
}
32+
2933
/** Insert an element into the queue. Return true if the operation is successful. */
3034
bool enQueue(int x) {
3135
data.push_back(x);
3236
return true;
3337
}
38+
3439
/** Delete an element from the queue. Return true if the operation is successful. */
3540
bool deQueue() {
3641
if (isEmpty()) {
@@ -39,32 +44,34 @@ class MyQueue {
3944
p_start++;
4045
return true;
4146
};
47+
4248
/** Get the front item from the queue. */
4349
int Front() {
4450
return data[p_start];
4551
};
52+
4653
/** Checks whether the queue is empty or not. */
4754
bool isEmpty() {
4855
return p_start >= data.size();
4956
}
5057
};
5158

5259
/***********************************************************************************
53-
* Design your implementation of the circular queue.
54-
* The circular queue is a linear data structure in which the operations
55-
* are performed based on FIFO (First In First Out) principle and
56-
* the last position is connected back to the first position to make a circle.
60+
* Design your implementation of the circular queue.
61+
* The circular queue is a linear data structure in which the operations
62+
* are performed based on FIFO (First In First Out) principle and
63+
* the last position is connected back to the first position to make a circle.
5764
* It is also called "Ring Buffer".
5865
* Your implementation should support following operations:
5966
* 1. MyCircularQueue(k): Constructor, set the size of the queue to be k.
6067
* 2. Front: Get the front item from the queue. If the queue is empty, return -1.
6168
* 3. Rear: Get the last item from the queue. If the queue is empty, return -1.
62-
* 4. enQueue(value): Insert an element into the circular queue.
69+
* 4. enQueue(value): Insert an element into the circular queue.
6370
* Return true if the operation is successful.
6471
* 5. deQueue(): Delete an element from the circular queue. Return true if the operation is successful.
6572
* 6. isEmpty(): Checks whether the circular queue is empty or not.
6673
* 7. isFull(): Checks whether the circular queue is full or not.
67-
*
74+
*
6875
* Example:
6976
* MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3
7077
* circularQueue.enQueue(1); // return true
@@ -76,7 +83,7 @@ class MyQueue {
7683
* circularQueue.deQueue(); // return true
7784
* circularQueue.enQueue(4); // return true
7885
* circularQueue.Rear(); // return 4
79-
*
86+
*
8087
* Source: https://leetcode-cn.com/explore/learn/card/queue-stack/216/queue-first-in-first-out-data-structure/865/
8188
************************************************************************************/
8289

@@ -95,7 +102,7 @@ class MyCircularQueue {
95102
tail = -1;
96103
size = k;
97104
}
98-
105+
99106
/** Insert an element into the circular queue. Return true if the operation is successful. */
100107
bool enQueue(int value) {
101108
if (isFull()) {
@@ -109,44 +116,44 @@ class MyCircularQueue {
109116
data[tail] = value;
110117
return true;
111118
}
112-
119+
113120
/** Delete an element from the circular queue. Return true if the operation is successful. */
114121
bool deQueue() {
115122
if (isEmpty()) {
116123
return false;
117124
}
118-
125+
119126
if (head == tail) {
120127
head = -1;
121128
tail = -1;
122129
return true;
123130
}
124-
131+
125132
head = (head + 1) % size;
126133
return true;
127134
}
128-
135+
129136
/** Get the front item from the queue. */
130137
int Front() {
131138
if (isEmpty()) {
132139
return -1;
133140
}
134141
return data[head];
135142
}
136-
143+
137144
/** Get the last item from the queue. */
138145
int Rear() {
139146
if (isEmpty()) {
140147
return -1;
141148
}
142149
return data[tail];
143150
}
144-
151+
145152
/** Checks whether the circular queue is empty or not. */
146153
bool isEmpty() {
147154
return head == -1;
148155
}
149-
156+
150157
/** Checks whether the circular queue is full or not. */
151158
bool isFull() {
152159
return ((tail + 1) % size) == head;
@@ -168,11 +175,11 @@ class MyCircularQueue {
168175
/**
169176
* Implement Queue by Two Stacks
170177
* As the title described, you should only use two stacks to implement a queue's actions.
171-
* The queue should support push(element),
178+
* The queue should support push(element),
172179
* pop() and top() where pop is pop the first(a.k.a front) element in the queue.
173180
* Both pop and top methods should return the value of first element.
174181
* LIFO + LIFO ==> FIFO
175-
*
182+
*
176183
* Example:
177184
* For push(1), pop(), push(2), push(3), top(), pop(), you should return 1, 2 and 2
178185
*/
@@ -189,7 +196,7 @@ class MyQueue {
189196
void push(int element) {
190197
stack1.push(element);
191198
}
192-
199+
193200
void adjust() {
194201
if (stack2.empty()) {
195202
while (!stack1.empty()) {
@@ -198,7 +205,7 @@ class MyQueue {
198205
}
199206
}
200207
}
201-
208+
202209
int pop() {
203210
adjust();
204211
int temp = stack2.top();
@@ -215,9 +222,9 @@ class MyQueue {
215222
/**
216223
* BFS - Template 1
217224
* Return the length of the shortest path between root and target node.
218-
*
225+
*
219226
* 1. As shown in the code, in each round, the nodes in the queue are the nodes which are waiting to be processed.
220-
* 2. After each outer while loop, we are one step farther from the root node.
227+
* 2. After each outer while loop, we are one step farther from the root node.
221228
* The variable step indicates the distance from the root node and the current node we are visiting.
222229
*/
223230
int BFS(TreeNode root, TreeNode target) {
@@ -245,7 +252,7 @@ int BFS(TreeNode root, TreeNode target) {
245252
/**
246253
* BFS - Template 2
247254
* Return the length of the shortest path between root and target node.
248-
*
255+
*
249256
* There are two cases you don't need the hash set used:
250257
* 1. You are absolutely sure there is no cycle, for example, in tree traversal;
251258
* 2. You do want to add the node to the queue multiple times.
@@ -287,4 +294,4 @@ int main() {
287294
if (!q.isEmpty()) {
288295
cout << q.Front() << endl;
289296
}
290-
}
297+
}

basic_data_structure/stack/stack.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
/**
2-
* @Author: Chacha
3-
* @Date: 2018-12-05 22:58:19
2+
* @Author: Chacha
3+
* @Date: 2018-12-05 22:58:19
44
* @Last Modified by: Chacha
55
* @Last Modified time: 2018-12-08 22:23:42
66
*/
77

88
#include <iostream>
99
#include <vector>
10+
#include <stack>
1011
using namespace std;
1112

1213
class MyStack {
1314
private:
1415
vector<int> data; // store elements
16+
1517
public:
1618
/** Insert an element into the stack. */
1719
void push(int x) {
1820
data.push_back(x);
1921
}
22+
2023
/** Checks whether the queue is empty or not. */
2124
bool isEmpty() {
2225
return data.empty();
2326
}
27+
2428
/** Get the top item from the queue. */
2529
int top() {
2630
return data.back();
2731
}
32+
2833
/** Delete an element from the queue. Return true if the operation is successful. */
2934
bool pop() {
3035
if (isEmpty()) {
3136
return false;
3237
}
38+
3339
data.pop_back();
3440
return true;
3541
}
@@ -41,7 +47,7 @@ class MyStack {
4147
* 2. pop() -- Removes the element on top of the stack.
4248
* 3. top() -- Get the top element.
4349
* 4. getMin() -- Retrieve the minimum element in the stack.
44-
*
50+
*
4551
* Example:
4652
* MinStack minStack = new MinStack();
4753
* minStack.push(-2);
@@ -51,7 +57,7 @@ class MyStack {
5157
* minStack.pop();
5258
* minStack.top(); --> Returns 0.
5359
* minStack.getMin(); --> Returns -2.
54-
*
60+
*
5561
* Source: https://leetcode-cn.com/explore/learn/card/queue-stack/218/stack-last-in-first-out-data-structure/877/
5662
************************************************************************************/
5763
class MinStack {
@@ -60,23 +66,23 @@ class MinStack {
6066
stack<int> s2;
6167

6268
MinStack() {
63-
69+
6470
}
65-
71+
6672
void push(int x) {
6773
s1.push(x);
6874
if (s2.empty() || x <= getMin()) s2.push(x);
6975
}
70-
76+
7177
void pop() {
7278
if (s1.top() == getMin()) s2.pop();
7379
s1.pop();
7480
}
75-
81+
7682
int top() {
7783
return s1.top();
7884
}
79-
85+
8086
int getMin() {
8187
return s2.top();
8288
}
@@ -96,10 +102,11 @@ int main() {
96102
s.push(1);
97103
s.push(2);
98104
s.push(3);
105+
99106
for (int i = 0; i < 4; ++i) {
100107
if (!s.isEmpty()) {
101108
cout << s.top() << endl;
102109
}
103110
cout << (s.pop() ? "true" : "false") << endl;
104111
}
105-
}
112+
}

basic_data_structure/stack/stack.gif

1.88 MB
Loading

0 commit comments

Comments
 (0)