-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.cpp
84 lines (68 loc) · 963 Bytes
/
2.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
/*
*Éè¼Æ°üº¬minº¯ÊýµÄÕ»
*/
#include <iostream>
using namespace std;
#define MAXSIZE 1024
class Stack {
public:
Stack();
~Stack();
void Push(int x);
void Pop(int &x);
void Min(int &m);
private:
int *data;
int *min;
int top;
};
Stack::Stack()
{
data = new int[MAXSIZE];
min = new int[MAXSIZE];
top = -1;
}
Stack::~Stack()
{
delete [] data;
delete [] min;
}
void Stack::Push(int x)
{
if (top+1 >= MAXSIZE){
cout << "error:stack is full" << endl;
return;
}
if (top == -1 || x < min[top])
min[top+1] = x;
else
min[top+1] = min[top];
data[++top] = x;
}
void Stack::Pop(int &x)
{
if (top == -1) {
cout << "error:stack is empty" << endl;
return;
}
x = data[top--];
}
void Stack::Min(int &m)
{
m = min[top];
}
int main()
{
Stack *st = new Stack();
int min,x;
st->Push(2);
st->Push(4);
st->Push(3);
st->Push(1);
st->Min(min);
cout << min << endl;
st->Pop(x);
st->Min(min);
cout << min << endl;
return 0;
}