코락 CoRock
코딩하는 락스타
코락 CoRock
  • 분류 전체보기 (393)
    • frameworks (19)
      • spring (19)
      • spring-boot (0)
      • testing (0)
    • languages (94)
      • java (39)
      • kotlin (0)
      • python (42)
      • r (13)
    • libraries (0)
    • programming (239)
      • android (13)
      • c (17)
      • cpp (22)
      • database (18)
      • design-pattern (4)
      • data-structures (11)
      • git (8)
      • hadoop (6)
      • html-css (7)
      • issue (4)
      • javascript (26)
      • jsp (34)
      • os (29)
      • php (6)
      • preferences (19)
      • etc (15)
    • discography (37)
      • k-pop (18)
      • pop (19)
    • blog (3)

블로그 메뉴

  • Programming
  • Java
  • JavaScript
  • Discography
  • K-Pop Songs
  • Pop Songs
  • Blog
  • Guestbook

공지사항

인기 글

태그

  • Android
  • python
  • jsp
  • r
  • 파이썬
  • linux
  • Spring
  • oracle
  • CentOS
  • javascript
  • Java
  • 자바스크립트

최근 댓글

최근 글

티스토리

반응형
hELLO · Designed By 정상우.
코락 CoRock

코딩하는 락스타

programming/cpp

[C++] Stack과 Queue의 Push, Pop 구현

2018. 3. 14. 13:26
반응형

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#define ARR_MAX        2
#define ARR_MIN        0
#include <iostream>
using namespace std;
 
class Memory {
    int *arr;
    int rear;
public:
    // constructor
    Memory();
 
    // setter and getter
    void setRear(int rear);
    int getRear();
 
    // push and pop operation
    void Push(int x);
    virtual int Pop() = 0;
    void isFull() {
        cout << "-------------------------" << endl;
        cout << "메모리 초과!" << endl;
        cout << "-------------------------" << endl;
    }
    int isEmpty() {
        if (getRear() < 0) {
            cout << "-------------------------" << endl;
            cout << "메모리 빔!" << endl;
            cout << "-------------------------" << endl;
            return 1;
        }
        return 0;
    }
    int* getArrayAddress();
};
 
Memory::Memory()
{
    rear = -1;
    arr = new int[ARR_MAX];
}
 
void Memory::setRear(int rear)
{
    this->rear = rear;
}
 
int Memory::getRear()
{
    return this->rear;
}
 
void Memory::Push(int x) {
    if (rear + 1 >= ARR_MAX) {
        isFull();
    }
    else {
        this->rear++;
        arr[rear] = x;
        cout << "-------------------------" << endl;
        cout << "Push push babay~" << endl;
        cout << "-------------------------" << endl;
    }
}
 
int* Memory::getArrayAddress()
{
    return this->arr;
}
 
class MyStack : public Memory {
    int *p;
    int temp;
public:
    MyStack();
    virtual int Pop();
};
 
MyStack::MyStack()
{
    this->p = NULL;
    temp = 0;
}
 
int MyStack::Pop()
{
    p = Memory::getArrayAddress();
 
    if (!isEmpty()) {
        // pop한 값을 temp에 넣고 return 한다.
        temp = p[Memory::getRear()];
        setRear(getRear() - 1);
 
        return temp;
    }
    else {
        return -9;
    }
}
 
class MyQueue : public Memory {
    int *p;
    int front;
    int temp;
public:
    // constructor
    MyQueue();
 
    // setter and getter
    void setFront(int front) { this->front = front; }
    int getFront() { return this->front; }
 
    virtual int Pop();
};
 
MyQueue::MyQueue()
{
    p = NULL;
    front = 0;
    temp = 0;
}
 
int MyQueue::Pop()
{
    p = Memory::getArrayAddress();
 
    // pop한 값을 temp에 넣고 return 한다.
    if (!isEmpty()) {
        temp = p[MyQueue::getFront()];
 
        // shift 작업(해야함)
        for (int i = 0; i < Memory::getRear(); i++) {
            p[i] = p[i + 1];
        }
        setRear(getRear() - 1);
 
        return temp;
    }
    else {
        return -9;
    }
}
 
void main()
{
    Memory *mem;
    MyStack s;
    MyQueue q;
 
    int button;
    int tempSel = 0;
    int tempData = 0;
    int data;
    int sayYesOrNo = 0;
    int check = -9;
 
    do {
        cout << "-------------------------" << endl;
        cout << "자료 구조를 선택하세요." << endl;
        cout << "-------------------------" << endl;
        cout << "1. Stack" << endl;
        cout << "2. Queue" << endl;
        cout << "3. Exit" << endl;
        cout << "-------------------------" << endl;
        cin >> button;
 
        // dynamic binding
        if (button == 1) { mem = &s; }
        else if (button == 2) { mem = &q; }
        else if (button == 3) {
            cout << "-------------------------" << endl;
            cout << "프로그램을 종료합니다." << endl;
            cout << "빠이루!" << endl;
            cout << "-------------------------" << endl;
            return;
        }
        else {
            cout << "-------------------------" << endl;
            cout << "잘못된 번호를 입력했습니다." << endl << "다시 입력하세요." << endl;
            cout << "-------------------------" << endl;
            continue;
        }
 
        cout << "-------------------------" << endl;
        cout << "수행할 기능을 선택하세요." << endl;
        cout << "-------------------------" << endl;
        cout << "1. Push Operation" << endl;
        cout << "2. Pop Operation" << endl;
        cout << "3. 뒤로가기" << endl;
        cout << "-------------------------" << endl;
        cin >> tempSel;
 
        switch (tempSel)
        {
        case 1:
            cout << "-------------------------" << endl;
            cout << "Push할 값을 입력하세요." << endl;
            cout << "-------------------------" << endl;
            cout << "==> ";
            cin >> tempData;
            mem->Push(tempData);
            break;
        case 2:    // pop 연산
            cout << "-------------------------" << endl;
            if(check != (data=mem->Pop()))
            { 
            cout << "값 : " << data << endl;
            cout << "-------------------------" << endl;
            }
            break;
        default:
            break;
        }
 
        cout << "계속하시겠습니까?" << endl;
        cout << "1. Yes" << "\t" << "2. No" << endl;
        cout << "-------------------------" << endl;
        cin >> sayYesOrNo;
 
    } while (sayYesOrNo == 1);
}
Colored by Color Scripter
cs

반응형
저작자표시 비영리 변경금지 (새창열림)
    'programming/cpp' 카테고리의 다른 글
    • [C++] #include <string> 직접 구현!
    • [C++] friend와 Operator Function
    • 동적 바인딩 3/14
    • 순수가상함수
    코락 CoRock
    코락 CoRock
    A COder dreaming of being a ROCKstar

    티스토리툴바