Skip to content

Commit 33f5897

Browse files
committed
feat: update stack data structure.
1 parent c9d6373 commit 33f5897

File tree

2 files changed

+64
-43
lines changed

2 files changed

+64
-43
lines changed

basic_data_structure/stack/stack.cpp

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
1-
/**
1+
/*
22
* @Author: Chacha
33
* @Date: 2018-12-05 22:58:19
44
* @Last Modified by: Chacha
5-
* @Last Modified time: 2018-12-08 22:23:42
5+
* @Last Modified time: 2021-03-07 22:08:39
66
*/
77

88
#include <iostream>
99
#include <vector>
1010
#include <stack>
1111
using namespace std;
1212

13-
class MyStack {
14-
private:
15-
vector<int> data; // store elements
16-
17-
public:
18-
/** Insert an element into the stack. */
19-
void push(int x) {
20-
data.push_back(x);
21-
}
13+
class MyStack
14+
{
15+
private:
16+
vector<int> data; // store elements
2217

23-
/** Checks whether the queue is empty or not. */
24-
bool isEmpty() {
25-
return data.empty();
26-
}
18+
public:
19+
/** Insert an element into the stack. */
20+
void push(int x)
21+
{
22+
data.push_back(x);
23+
}
2724

28-
/** Get the top item from the queue. */
29-
int top() {
30-
return data.back();
31-
}
25+
/** Checks whether the queue is empty or not. */
26+
bool isEmpty()
27+
{
28+
return data.empty();
29+
}
3230

33-
/** Delete an element from the queue. Return true if the operation is successful. */
34-
bool pop() {
35-
if (isEmpty()) {
36-
return false;
37-
}
31+
/** Get the top item from the queue. */
32+
int top()
33+
{
34+
return data.back();
35+
}
3836

39-
data.pop_back();
40-
return true;
37+
/** Delete an element from the queue. Return true if the operation is successful. */
38+
bool pop()
39+
{
40+
if (isEmpty())
41+
{
42+
return false;
4143
}
44+
45+
data.pop_back();
46+
return true;
47+
}
4248
};
4349

4450
/***********************************************************************************
@@ -60,30 +66,37 @@ class MyStack {
6066
*
6167
* Source: https://leetcode-cn.com/explore/learn/card/queue-stack/218/stack-last-in-first-out-data-structure/877/
6268
************************************************************************************/
63-
class MinStack {
64-
public:
65-
stack<int> s1;
66-
stack<int> s2;
67-
68-
MinStack() {
69-
69+
class MinStack
70+
{
71+
public:
72+
stack<int> s1;
73+
stack<int> s2;
74+
75+
MinStack()
76+
{
7077
}
7178

72-
void push(int x) {
79+
void push(int x)
80+
{
7381
s1.push(x);
74-
if (s2.empty() || x <= getMin()) s2.push(x);
82+
if (s2.empty() || x <= getMin())
83+
s2.push(x);
7584
}
7685

77-
void pop() {
78-
if (s1.top() == getMin()) s2.pop();
86+
void pop()
87+
{
88+
if (s1.top() == getMin())
89+
s2.pop();
7990
s1.pop();
8091
}
8192

82-
int top() {
93+
int top()
94+
{
8395
return s1.top();
8496
}
8597

86-
int getMin() {
98+
int getMin()
99+
{
87100
return s2.top();
88101
}
89102
}
@@ -97,14 +110,18 @@ class MinStack {
97110
* int param_4 = obj.getMin();
98111
*/
99112

100-
int main() {
113+
int
114+
main()
115+
{
101116
MyStack s;
102117
s.push(1);
103118
s.push(2);
104119
s.push(3);
105120

106-
for (int i = 0; i < 4; ++i) {
107-
if (!s.isEmpty()) {
121+
for (int i = 0; i < 4; ++i)
122+
{
123+
if (!s.isEmpty())
124+
{
108125
cout << s.top() << endl;
109126
}
110127
cout << (s.pop() ? "true" : "false") << endl;

basic_data_structure/tree/tree.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
* @Author: Chacha
33
* @Date: 2021-03-05 19:11:18
44
* @Last Modified by: Chacha
5-
* @Last Modified time: 2021-03-05 19:15:47
5+
* @Last Modified time: 2021-03-07 22:08:29
66
*/
77

88
#include <iostream>
99
#include <vector>
1010
#include <queue>
1111
using namespace std;
1212

13+
/**
14+
* 树是一种非线性数据结构,根据子节点数量可分为 「二叉树」 和 「多叉树」,最顶层的节点称为「根节点 root」。
15+
* 以二叉树为例,每个节点包含三个成员变量:「值 val」、「左子节点 left」、「右子节点 right」 。
16+
*/
1317
struct TreeNode
1418
{
1519
int val;

0 commit comments

Comments
 (0)