-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackArray.h
66 lines (50 loc) · 868 Bytes
/
StackArray.h
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
#pragma once
class Stack
{
// SIZE - ìàêñèìàëüíîå êîëè÷åñòâî ýëåìåíòîâ â ñòåêå (áîëüøå 10 èõ íå áóäåò!)
public:
enum { SIZE = 10, EMPTY = 0 };
private:
int ar[SIZE]; // â êà÷åñòâå õðàíèëèùà ýëåìåíòîâ ñòåêà èçáðàí ìàññèâ
int top = EMPTY; // èíäåêñ âåðøèíû ñòåêà
public:
void Clear()
{
top = EMPTY;
}
bool IsEmpty()
{
return top == EMPTY;
}
bool IsFull()
{
return top == SIZE;
}
int GetCount()
{
return top;
}
// "çàòàëêèâàíèå" (äîáàâëåíèå) ýëåìåíòà â ñòåê
void Push(int value)
{
if (IsFull())
throw "Stack overflow!";
ar[top++] = value;
}
// èçâëå÷åíèå ýëåìåíòà èç ñòåêà
int Pop()
{
if (IsEmpty())
throw "Stack is empty!";
top--;
return ar[top];
}
};
/*
// code for main:
Stack st;
while (st.IsFull() == false)
st.Push(rand() % 90 + 10);
while (st.IsEmpty() == false)
cout << st.Pop() << " ";
*/