티스토리 뷰

ArrayBaseStack.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
#ifndef __AB_STACK_H__
#define __AB_STACK_H__
 
#define STACK_LEN    100
typedef int Data;
 
class ArrayStack
{
private:
    Data stackArr[STACK_LEN];
    int topIndex;
 
public:
    ArrayStack()
    {
        topIndex = -1;
    }
    bool SIsEmpty();
 
    void SPush(Data data);
    Data SPop();
    Data SPeek();
};
 
#endif

cs


.ArrayBaseStack.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
#include<iostream>
#include"ArrayBaseStack.h"
using namespace std;
 
bool ArrayStack::SIsEmpty()
{
    if (topIndex == -1)
        return true;
    else
        return false;
}
 
void ArrayStack::SPush(Data data)
{
    topIndex += 1;
    stackArr[topIndex] = data;
}
 
Data ArrayStack::SPop()
{
    int rIdx;
    
    if (SIsEmpty())
    {
        cout << "Stack is empty!" << endl;
        exit(-1);
    }
        
    rIdx = topIndex;
    topIndex -= 1;
 
    return stackArr[rIdx];
}
 
Data ArrayStack::SPeek()
{
    if (SIsEmpty())
    {
        cout << "Stack is empty!" << endl;
        exit(-1);
    }
 
    return stackArr[topIndex];
}
 
int main()
{
    ArrayStack stack;
 
    stack.SPush(1);
    stack.SPush(2);
    stack.SPush(3);
    stack.SPush(4);
    stack.SPush(5);
 
    while (!stack.SIsEmpty())
        cout << stack.SPop() << " ";
 
    return 0;
}
cs


Comments