Queue 와 Stack
-
LIFO(Last in, First out)의 Stack
-
FIFO(First in, First out)의 Queue
소 스 코 드
#include<iostream>
#include<crtdbg.h>
template<typename T>
class NODE {
private:
T value;
NODE*next;
public:
NODE() = delete;
NODE(const T& v, NODE * n) : value{ v }, next{ n }{}
void SetNext(NODE *n) { next = n; }
NODE* GetNext()const { return next; }
int GetValue()const { return value; }
};
// LIFO
template<typename T>
class STACK {
private:
NODE<T> *top;
int size;
public:
STACK() : top{ nullptr }, size{ 0 } {}
~STACK() {
int i{ 0 };
NODE<T> * temp{ top };
while (nullptr != temp) {
top = temp->GetNext();
delete temp;
temp = top;
}
}
void Push(const T& v) {
NODE<T>* temp{ new NODE<T>(v, nullptr) };
if (nullptr == top) {
top = temp;
}
else {
temp->SetNext(top);
top = temp;
}
++size;
}
void Pop() {
if (nullptr == top) {
std::cout << "Empty STACK \n";
return;
}
NODE<T>* temp{ top };
top = temp->GetNext();
delete temp;
--size;
}
T Top() const{
if (nullptr == top) return -1;
return top->GetValue();
}
int Size() const {
return size;
}
bool Empty() const {
if (nullptr == top) return true;
else return false;
}
};
// FIFO
template<typename T>
class QUEUE {
private:
NODE<T> *head;
NODE<T> *tail;
int size;
public:
QUEUE() :head{ nullptr }, tail{ nullptr }, size{ 0 } {}
~QUEUE(){
NODE<T> *temp{ head };
while (nullptr != temp) {
head = temp->GetNext();
delete temp;
temp = head;
}
}
public:
void Push(const T& v) {
NODE<T> *temp{ new NODE<T>(v, nullptr) };
if (nullptr == head) head = tail = temp;
else {
tail->SetNext(temp);
tail = temp;
}
++size;
}
void Pop() {
if (nullptr == head) {
std::cout << "Empty Queue \n";
return;
}
NODE<T> *temp{ head };
head = temp->GetNext();
delete temp;
--size;
}
T Front() const {
if (nullptr == head) return -1;
return head->GetValue();
}
T Back() const {
if (nullptr == head) return -1;
return tail->GetValue();
}
int Size() const {
return size;
}
bool Empty() const {
if (nullptr == head) return true;
else return false;
}
};
// STACK
/*
int main() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
STACK<int> st;
for (int i = 0; i < 50; ++i) {
st.Push(i + 1);
}
std::cout << st.Top() << '\n';
std::cout << st.Empty() << '\n';
for (int i = 0; i < 52; ++i) {
st.Pop();
}
std::cout << st.Size() << '\n';
for (int i = 0; i < 50; ++i) {
st.Push(i + 1);
}
std::cout << st.Top() << '\n';
std::cout << st.Size() << '\n';
return 0;
}
*/
// Queue
//int main() {
// QUEUE<int> que;
//
//
// std::cout << que.Empty() << ", " << que.Size() << ", " << que.Front() << ", " << que.Back() << '\n';
//
// for (int i = 0; i < 50; ++i) que.Push(i + 1);
//
// std::cout << que.Empty() << ", " << que.Size() << ", " << que.Front() << ", " << que.Back() << '\n';
//
// que.Pop();
//
// std::cout << que.Empty() << ", " << que.Size() << ", " << que.Front() << ", " << que.Back() << '\n';
//
// for (int i = 0; i < 50; ++i) que.Pop();
//
// std::cout << que.Empty() << ", " << que.Size() << ", " << que.Front() << ", " << que.Back() << '\n';
//
//
// for (int i = 0; i < 50; ++i) que.Push(i + 1);
//
// std::cout << que.Empty() << ", " << que.Size() << ", " << que.Front() << ", " << que.Back() << '\n';
//
// que.Pop();
//
// return 0;
//}
'[ 공 부 ] > [ C + + ]' 카테고리의 다른 글
[ C++ / STL ] Vector 구현해 보기 (0) | 2020.07.25 |
---|---|
[C++] 비트 연산자 사용 (0) | 2020.07.10 |
상속 - 순수 가상 함수 (0) | 2019.11.18 |
Class 상속 Virtual (0) | 2019.11.14 |
Operator의 사용 (0) | 2019.11.13 |