Example #1
0
int main()
{
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

	ArrayStack<int> s;
	//LinkedStack<int> s;

	const int N = 2;
	for (int i = 0; i < N; ++i)
		s.push(i + 1);

	for (int i = 0; i < N; ++i)
	{
		std::cout << s.peek() << std::endl;
		//s.pop();
	}

	ArrayStack<int> other = s;
	//LinkedStack<int> other = s;
	//other = s;

	s.makeEmpty();

	std::cout << (other.isEmpty() ? 0 : other.peek()) << std::endl;

	std::cout << (s.isEmpty() ? "YES" : "NO") << std::endl;

	return 0;
}
Example #2
0
int main()
{
    int  num;       // the number to be converted
    string numStr;  // string used for input
    int  remainder; // remainder when num is divided by 2
    ArrayStack <int> stackOfRemainders; // remainders
    char response;           // user response
    do
    {
        cout << "Enter positive integer to convert: ";
        cin >> numStr;
        num = atoi(numStr.c_str());
        while (num > 0)
        {
            remainder = num % 2;
            stackOfRemainders.push(remainder);
            num /= 2;
        }

        cout << "Base-two representation: ";
        while ( !stackOfRemainders.isEmpty() )
        {
            remainder = stackOfRemainders.peek();
            stackOfRemainders.pop();
            cout << remainder;
        }
        cout << endl;
        cout << "\nMore (Y or N)? ";
        cin >> response;
    }
    while (response == 'Y' || response == 'y');

    return EXIT_SUCCESS;

}  // end main
Example #3
0
void Sort(ArrayStack<int> &stack) {
    ArrayStack<int> helper;
    while (!stack.empty()) {
        int cur = stack.pop();
        int count = 0;
        while (!helper.empty() && helper.peek() < cur) {
            count++;
            stack.push(helper.pop());
        }
        helper.push(cur);
        while (count--)
            helper.push(stack.pop());
    }

    while (!helper.empty())
        stack.push(helper.pop());
}
Example #4
0
int main() {
  ArrayStack<int> s;
  s.push(10);
  cout << s.peek() << endl;
  return 0;
}