본문 바로가기

잡다한 IT/자료구조

스택, 큐 클래스 설계

<스택 클래스 설계>


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
#include<iostream>
#include<vector>
using namespace std;
 
 
class STACK
{
private:
    int top;
    vector<int> st;
    int MAX;
public:
    STACK() {};
    ~STACK() {};
    void init();
    bool isEmpty();
    bool isFull();
    void push();
    void pop();
};
 
void STACK::init()
{
    top = 0;
    MAX = 5;
    st.resize(MAX);
}
 
bool STACK::isEmpty()
{
    if (top == 0)
        return true;
 
    return false;
}
 
bool STACK::isFull()
{
    if (top == MAX)
        return true;
 
    return false;
}
 
void STACK::push()
{
    if (isFull()) {
        cout << "꽉참" << endl;
        return;
    }
 
    int tmp;
    cout << "push 할 숫자를 입력하세요:";
    cin >> tmp;
 
    st[top++= tmp;
}
 
void STACK::pop()
{
    if (isEmpty()) {
        cout << "텅빔" << endl;
        return;
    }
 
    cout << st[--top] << endl;
 
}
 
int main()
{
    STACK st;
    st.init();
    st.push();
    st.push();
    st.push();
    st.push();
    st.push();
    st.push();
 
    st.pop();
    st.pop();
    st.pop();
    st.pop();
    st.pop();
    st.pop();
 
    return 0;
}
cs

<큐 클래스 설계>

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
91
92
93
94
95
96
97
98
99
#include<iostream>
 
using namespace std;
 
class QUEUE
{
private:
    int arr[100];
    int MAX;
    int rear;
    int front;
 
public:
    QUEUE() {};
    ~QUEUE() {};
    void init();
    bool isFull();
    bool isEmpty();
    void push();
    void pop();
};
 
void QUEUE::init()
{
    rear = 0;
    front = 0;
    MAX = 5;
}
 
bool QUEUE::isFull()
{
    if ((rear + 1) % MAX == front)
    {
        cout << "꽉참" << endl;
        return true;
    }
 
    return false;
    
}
 
bool QUEUE::isEmpty()
{
    if (front == rear)
    {
        return true;
    }
    return false;
}
 
void QUEUE::push()
{
    if (isFull())
    {
        return;
    }
 
    int tmp;
    cout << "넣을 값 입력:";
    cin >> tmp;
    arr[rear++= tmp;
 
    if (rear == MAX)
        rear = 0;
}
 
void QUEUE::pop()
{
    if (isEmpty())
    {
        return;
    }
 
    cout << arr[front++<< endl;
    
    if (front == MAX)
        front = 0;
 
}
 
int main()
{
    QUEUE q;
    q.init();
 
    q.push();
    q.push();
    q.push();
    q.push();
    q.push();
    q.pop();
    q.pop();
    q.pop();
    q.pop();
    q.pop();
 
 
    return 0;
}
cs


반응형

'잡다한 IT > 자료구조' 카테고리의 다른 글

퀵소트 vs 머지소트  (0) 2018.05.12
Sort , Search  (0) 2018.05.07
연결 리스트로 완전 이진 탐색(BST) 구현  (0) 2018.04.24
연결리스트를 통한 큐 구현  (0) 2018.04.24
연결리스트를 통한 스택 구현  (0) 2018.04.24