-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_栈.cpp
90 lines (84 loc) · 1.25 KB
/
_栈.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
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
#include <iostream>
#include <string>
using namespace::std;
class stack {
private:
int mSize;
int top;
int* bottom;
public:
stack(int mSize)
{
this->mSize = mSize;
this->bottom = new int[mSize];
this->top = 0;
}
bool push(int val)
{
if (this->top == this->mSize - 1)
return false;
else
{
this->top++;
this->bottom[this->top] = val;
return true;
}
}
int pop(void)
{
if (this->top)
{
(this->top)--;
return this->bottom[this->top + 1];
}
else
{
return -1;
}
}
bool clear(void)
{
this->top = 0;
return true;
}
bool reSize(int mSize)
{
delete []this->bottom;
this->mSize = mSize;
this->top = 0;
this->bottom = new int[mSize];
if (this->bottom)
return true;
else
return false;
}
};
int main()
{
int n;
cout << "please input the size of the stack:" << endl;
cin >> n;
stack stack_(n);
string str;
cout << "please input action:" << endl;
cin >> str;
if (str == "push")
{
cout << "please input number:" << endl;
cin >> n;
}
while (str != "q")
{
if (str == "push")
stack_.push(n);
if (str == "pop")
cout << stack_.pop() << endl;
cout << "please input action:" << endl;
cin >> str;
if (str == "push")
{
cout << "please input number:" << endl;
cin >> n;
}
}
}