문제

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

 

 

내 코드

#include <string>
#include <iostream>
using namespace std;

struct Node
{
	Node* prev = nullptr;
	int value;

	Node(int val)
	{
		this->value = val;
	}
};

class Stack
{
private:
	Node* start = nullptr;
	Node* end = nullptr;
	int size = 0;
public:
	void Push(int val)
	{
		Node* newNode = new Node(val);
		if (start == nullptr)
		{
			start = end = newNode;
		}
		else
		{
			newNode->prev = end;
			end = newNode;
		}
		size++;
	}

	int Pop()
	{
		if (size > 0)
		{
			int value = end->value;
			Node* temp = end->prev;
			end = nullptr;
			delete end;
			end = temp;
			size--;
			return value;
		}
		return -1;
	}

	int Size()
	{
		return size;
	}

	int Empty()
	{
		if (size == 0) { return 1; }
		return 0;
	}

	int Top()
	{
		if (size > 0) { return end->value; }
		return -1;
	}
};


int main(void) {
	int N;
	Stack myS;
	string command;
	int val;

	cin >> N;

	for (int i = 0; i < N; i++)
	{
		cin >> command;
		if (command == "push")
		{
			cin >> val;
			myS.Push(val);
		}
		else if (command == "pop")
		{
			cout << myS.Pop() << endl;
		}
		else if (command == "size")
		{
			cout << myS.Size() << endl;
		}
		else if (command == "empty")
		{
			cout << myS.Empty() << endl;
		}
		else if (command == "top")
		{
			cout << myS.Top() << endl;
		}
	}
	return 0;
}

큐 풀면서 스텍도 비슷해서 같이 풀어봄

Posted by pi92

블로그 이미지
pi92

공지사항

Yesterday
Today
Total

달력

 « |  » 2025.11
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

최근에 올라온 글

최근에 달린 댓글

글 보관함