/*
 *	Autor: Krzysztof Trynkiewicz, rok I inf.stos.
 *	mail: chris.trynkiewicz@gmail.com
 *	Program imituje stos za pomoca list
 *	Version: 1.0
 *	Last modified: 10-06-07
*/

	/* Includes */
	#include <iostream>
	#include <fstream>
	#include <string>
	#include <list>
	/* --- Koniec Includes */

using namespace std;

string s;

struct rekord{
    string key;
    rekord *next;
};

class Stos{
    rekord* top;  //wskaznik do pierwszego elementu listy
    int num;    //ilosc elementow

	public:
    	Stos(void) {num = 0; top = NULL;}
    	int size(void) { return num; }
    	bool empty(void) { return (top==NULL); }
    	string stos_top(void) { if ( top != NULL ) return top->key; else cout << "Stos jest pusty.";}
    	void push(string s);
    	string pop(void);
} stos;

void Stos::push(string s){
	rekord *first = new rekord;
	first -> next = top;
	first -> key = s;
	num++;
	top = first;
}

string Stos::pop(void) {
	if (top == NULL) {
   		cout << "Stos jest pusty.\n";
    	return "";
    } else {
    	string temp = top -> key;
    	rekord *ptemp = top;
    	top = top -> next;
    	num--;
    	delete ptemp;
    	return temp;
    }
}

int main(void){

	/* Przedstawienie */
	printf("\n\tAutor: Krzysztof Trynkiewicz, I rok inf.stos.\n\tmail: chris.trynkiewicz@gmail.com\n");
	printf("\tProgram imituje stos za pomoca list\n\n");
	/* --- Koniec Przedstawienia */
	
	
	ifstream fin("stos.in");
	ofstream fout("stos.out");

	while(!fin.eof()){
  		fin >> s;
    	stos.push(s);
    }

	cout << "Pierwszy element stosu: " << stos.stos_top() << endl;

	while (!stos.empty()) {
    	cout << stos.pop() << endl;
    }
    
	fin.close();
	fout.close();
	
	return 0;
}