나의 브을로오그으

[c++] 10828번 : 스택 본문

알고리즘/BaekJoon

[c++] 10828번 : 스택

__jhp_+ 2022. 5. 8. 10:41

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

 

10828번: 스택

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

www.acmicpc.net

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

using namespace std;

#define PUSH		0
#define POP			1
#define SIZE		2
#define TOP			3
#define EMPTY		4

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

template <typename T>
class Stack
{
public:
	Stack()
	{
		this->current = nullptr;
		this->front = new Node<T>();
		this->end = new Node<T>();
		this->length = 0;
		this->capacity = 1;

		front->prev = nullptr;
		front->next = end;

		end->prev = front;
		end->next = nullptr;
	}

	~Stack()
	{
		Node<T>* node = front;
		while (node != nullptr)
		{
			Node<T>* next = node->next;
			delete node;
			node = next;
		}
	}

	void push(T data)
	{
		if (length == capacity)
		{
			capacity *= 2;
		}

		Node<T>* newNode = new Node<T>(data);
		Node<T>* prevNode = end->prev;

		end->prev = newNode;
		prevNode->next = newNode;

		newNode->prev = prevNode;
		newNode->next = end;

		current = newNode;
		++length;
		return;
	}

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

		Node<T>* prevNode = current->prev;

		prevNode->next = end;
		end->prev = prevNode;

		delete current;
		current = prevNode;
		--length;

		return data;
	}

	int size() const
	{
		return length;
	}

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

	T top()
	{
		if (length == 0)
		{
			return -1;
		}
		return current->data;
	}

private:
	Node<T>* current;
	Node<T>* front;
	Node<T>* end;
	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[5] = { "push", "pop", "size", "top", "empty" };
	Stack<int> st;
	string input;
	int N = 0;
	cin >> N;
	cin.ignore();
	for (int i = 0; i < N; ++i)
	{
		getline(cin, input);
		vector<string> output = split(input, ' ');

		for (int j = 0; j < 5; ++j)
		{
			if (output[0] == inst[j])
			{
				switch (j)
				{
				case PUSH:
					st.push(stoi(output[1]));
					break;
				case POP:
					cout << st.pop() << '\n';
					break;
				case SIZE:
					cout << st.size() << '\n';
					break;
				case TOP:
					cout << st.top() << '\n';
					break;
				case EMPTY:
					cout << st.empty() << '\n';
					break;
				default:
					break;
				}
				break;
			}
		}
	}

	return 0;
}

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

[c++] 10866번 : 덱  (0) 2022.05.10
[c++] 10845번 : 큐  (0) 2022.05.10
[c++] 10816번 : 숫자 카드2  (0) 2022.05.05
[c++] 10814번 : 나이순 정렬  (0) 2022.05.05
[c++] 10773번 : 제로  (0) 2022.05.05