나의 브을로오그으

[c++] 10845번 : 큐 본문

알고리즘/BaekJoon

[c++] 10845번 : 큐

__jhp_+ 2022. 5. 10. 11:55

https://www.acmicpc.net/problem/10845

 

10845번: 큐

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

enum Instructor {
	Push,
	Pop,
	Size,
	Empty,
	Front,
	Back
};


template <typename T>
struct Node {
	T data;
	Node<T>* next;
	Node<T>* prev;
	Node(T data) {
		this->data = data;
		next = nullptr;
		prev = nullptr;
	}
};

template <typename T>
class Queue {
public:
	Queue() {
		left = new Node<T>(-1);
		right = new Node<T>(-1);

		left->prev = nullptr;
		left->next = right;
		right->prev = left;
		right->next = nullptr;

		length = 0;
		capacity = 1;
	}
	~Queue() {
		while (left != nullptr)
		{
			Node<T>* nextNode = left->next;
			delete left;
			left = nextNode;
		}
	}

	void push(T data) {
		if (length == capacity) {
			capacity *= 2;
		}
		Node<T>* newNode = new Node<T>(data);
		
		Node<T>* prevNode = right->prev;
		prevNode->next = newNode;
		right->prev = newNode;

		newNode->next = right;
		newNode->prev = prevNode;
		++length;

		return;
	}

	T pop() {
		if (length == 0) {
			return -1;
		}

		Node<T>* delNode = left->next;
		T data = delNode->data;

		Node<T>* nextNode = delNode->next;
		nextNode->prev = left;
		left->next = nextNode;

		delete delNode;
		--length;

		return data;
	}

	int size() const {
		return length;
	}

	int empty() const {
		return length == 0;
	}

	T front() {
		if (length == 0) {
			return -1;
		}
		else
		{
			return left->next->data;
		}
	}

	T back() {
		if (length == 0) {
			return -1;
		}
		else
		{
			return right->prev->data;
		}
	}
private:
	Node<T>* left;
	Node<T>* right;
	int length;
	int capacity;
};

vector<string> split(string input, char delimiter) {
	vector<string> instructor;
	stringstream ss(input);
	string temp;

	while (getline(ss, temp, delimiter)) {
		instructor.push_back(temp);
	}
	return instructor;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	string inst[6] = { "push", "pop", "size", "empty", "front", "back"};
	Queue<int> q;
	string input = "";

	int N = 0;
	cin >> N;
	cin.ignore();

	for (int i = 0; i < N; ++i)
	{
		input.clear();
		getline(cin, input);
		vector<string> output = split(input, ' ');

		for (int j = 0; j < 6; ++j)
		{
			if (output[0] == inst[j]) 
			{
				switch (j)
				{
				case Instructor::Push:
					q.push(atoi(output[1].c_str()));
					break;
					case Instructor::Pop:
						cout << q.pop() << '\n';
					break;
					case Instructor::Size:
						cout << q.size() << '\n';
					break;
					case Instructor::Empty:
						cout << q.empty() << '\n';
					break;
					case Instructor::Front:
						cout << q.front() << '\n';
					break;
					case Instructor::Back:
						cout << q.back() << '\n';
					break;
				}
			}
		}
	}


	return 0;
}

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

[c++] 10989번 : 수 정렬하기3  (0) 2022.05.10
[c++] 10866번 : 덱  (0) 2022.05.10
[c++] 10828번 : 스택  (0) 2022.05.08
[c++] 10816번 : 숫자 카드2  (0) 2022.05.05
[c++] 10814번 : 나이순 정렬  (0) 2022.05.05