나의 흔적
[c++][Stack/Queue] 본문
#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;
}
void isEmpty() {
cout << "Memory is Empty!" << endl;
cout << "-------------------------" << endl;
}
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;
}
}
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();
// pop한 값을 temp에 넣고 return 한다.
temp = p[Memory::getRear()];
setRear(getRear() - 1);
return temp;
}
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 한다.
temp = p[MyQueue::getFront()];
// shift 작업(해야함)
for (int i = 0; i < Memory::getRear(); i++) {
p[i] = p[i + 1];
}
return temp;
}
void main()
{
Memory *mem;
MyStack s;
MyQueue q;
int button;
int tempSel = 0;
int tempData = 0;
int sayYesOrNo = 0;
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:
cout << "-------------------------" << endl;
if (mem->getRear() <= ARR_MIN) {
mem->isEmpty();
}
else {
cout << "Pop 연산 결과입니다." << endl;
cout << "값 : " << mem->Pop() << endl;
cout << "-------------------------" << endl;
}
break;
default:
break;
}
cout << "계속하시겠습니까?" << endl;
cout << "1. Yes" << "\t" << "2. No" << endl;
cout << "-------------------------" << endl;
cin >> sayYesOrNo;
} while (sayYesOrNo == 1);
}