//
// teststk.cpp
//
// Test of array-based stack implementation in stack.hpp
//
// Computer Science S-111, Harvard University
//

#include <iostream.h>
#include "../util/getinput.hpp"
#include "stack.hpp" 


//
// stack_action - used by stack::traverse to print each item on
// a stack of standard strings
//
void
stack_action(int pos, char* item)
{
    cout << pos << " " << item << "\n";
}

int
main()
{
    stack<char *> stk(10);
    char buf[80];
    char *str;

    cout << "\nCommands:";
    cout << "\nu <string>: Push string onto stack";
    cout << "\no: Pop string from stack";
    cout << "\nl: List stack";
    cout << "\nq: Quit\n";

    while (true) {
	cout << "\n[PuSH string | PoP | lIST | qUIT] : ";
	cin.getline(buf, 80);

	if (*buf == 'q')
	    exit(1);
	else if (*buf == 'u') {
	    str = strsave(buf + 1);
	    if (!stk.push(str))
		cout << "Stack is full\n";
	}
	else if (*buf == 'o') {
	    if (!stk.pop(str))
		cout << "Stack is empty\n";
	    else 
		cout << " got <" << str << ">\n";
	}
	else if (*buf == 'l') {
	    if (stk.empty())
		cout << "Stack is empty\n";
	    else
		stk.traverse(&stack_action);
	}
    }
}



