알고리즘/DataStructure
[C++] 자료구조/Queue 구현
Jay, Lee
2022. 1. 4. 12:36
#include <iostream>
#include <string>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = NULL;
}
};
class LinkedList {
public:
Node* f;
Node* r;
LinkedList() {
f = NULL;
r = NULL;
}
~LinkedList() {
}
int front() {
return f->data;
}
int end() {
return r->data;
}
void addBack(int data) {
Node* n = new Node(data);
if (f == NULL) {
f = r = n;
}
else {
r->next = n;
r = n;
}
}
void removeFront() {
Node* old = f;
int show = old->data;
cout << show << "\n";
f = old->next;
delete old;
if (f == NULL) {
r = NULL;
}
}
};
class LinkedQueue {
public:
LinkedList* s;
int n;
int max_size;
LinkedQueue(int size) {
this->s = new LinkedList();
this->n = 0;
this->max_size = size;
}
int size() {
return n;
}
bool isEmpty() {
return (n == 0);
}
void front() {
if (isEmpty()) {
cout << "Empty" << '\n';
}
else {
cout << s->front() << "\n";
}
}
void rear() {
if (isEmpty()) {
cout << "Empty" << '\n';
}
else {
cout << s->end() << "\n";
}
}
void enqueue(int x) {
if (size() == max_size) {
cout << "Full" << "\n";
}
else {
s->addBack(x);
n++;
}
}
void dequeue() {
if (isEmpty()) {
cout << "Empty" << "\n";
}
else {
s->removeFront();
n--;
}
}
};