본문 바로가기

알고리즘

<C++ 알고리즘> 글자 거꾸로 출력(스택활용)

#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;

void reverse(stack<char>& s, string str)
{
	try
	{
		if (str.empty())
			throw str;
		else
		{
			for (int i = 0; i < str.size(); ++i)
			{
				s.push(str[i]);
			}
		}
	}

	catch (string str)
	{
		cout << "스택이 비어있습니다." << endl;
	}
}

void printStack(stack<char>& s)
{
	int size = s.size();
	for (int i = 0; i < size; ++i)
	{
		cout << s.top();
		s.pop();
	}
	cout << endl;
}

int main()
{
	stack<char> s1;
	reverse(s1, "abcdef");
	printStack(s1);
	
	return 0;
}