#include<iostream.h>
#include "stack.h"


LinkedList::LinkedList(int a)
:value(a), next(0)
{
}

LinkedList::~LinkedList()
{
	delete next;
}

ostream& operator<< (ostream& out, LinkedList& x)
{
	x.display(out);
	return out;
}

void LinkedList::display(ostream& out)
{
	

	out << value;
	if (next == 0)
		return;
	else
	{
		cout << "  ->  "; 
		next->display(out);
	}
}


bool LinkedList::contains(int a)
{
	
	if (value == a)
		return true;
	else 
	{ 
		if (next == 0)
			return false;
		else
			return next->contains(a);
	}
}


void LinkedList::insert(int a)
{
	LinkedList* new_next = new LinkedList(a);
	new_next->next = next;
	next = new_next;
	return ;
}

void LinkedList::append(int a)
{
	// cout << "\nnext = " << next << endl;
	if (next)
	{
		next->append(a);
	}
	else
	{
		LinkedList* new_next = new LinkedList(a);
		next = new_next;
	}
		
}

int LinkedList::return_and_remove_last()
{
	int return_value;
	if (next->next == 0)
	{
		return_value = next->value;
		next = 0;
		return return_value;
	}
	else 
		return next->return_and_remove_last();
}


Stack::Stack()
{
	elements = new LinkedList(0);
}

Stack::Stack(int a)
{
	elements = new LinkedList(0);
	elements->append(a);
}

void Stack::push(int a)
{
	cout << "\npushing " << a << endl;
	elements->append(a);
}

int Stack::pop()
{
	if (empty())
		return 0;
	else
		return elements->return_and_remove_last();
}

bool Stack::empty()
{
	return elements->next == 0;
}

ostream& operator<< (ostream& out, Stack& x)
{
	(x.elements)->display(out);
	return out;
}


