Little Jay

[C++] 자료구조/Queue 구현 본문

알고리즘/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--;
		}
	}

};

'알고리즘 > DataStructure' 카테고리의 다른 글

Quick Sort, MergeSort with 백준 2751번  (0) 2022.03.28
[C++] 자료구조/Stack 구현  (0) 2021.11.20
Linked List Simple Question  (0) 2021.09.25
[C++] 자료구조/LinkedList 구현  (0) 2021.08.28
[C++] 자료구조/Array 구현  (0) 2021.07.22
Comments