//	Implementation File:	linked.cpp

#include "linked.h"

//	List IMPLEMENTATION
list::list()	//Constructor
:	front(NULL), rear(NULL) //member initializer list
{
}

list::list(const list & L) //Copy Constructor
{
	
	front=rear=NULL;
	for (node *temp = L.front; temp!=NULL; temp=temp->next){
		add_rear(temp->data);
	}
}
		

list::~list() //Destructor
{
	node *temp;
	while (front!=NULL){
		temp = front->next;
		delete front;
		front = temp;
	}
}

	
list & list::add_front (const int number)
{
	node * temp=new node;

	temp->data=number;
	temp->next=front;
	front=temp;

	if (rear==NULL)	//list empty to start
		rear=front;

	return *this;
}


//Pre-Condition:  Non-empty list
list & list::remove_front ()
{
	node * temp=front;

	front=front->next;
	delete temp;

	if (front==NULL)	//list empty
		rear=NULL;

	return *this;
}

list & list::add_rear (const int number)
{
	node * temp = new node;

	temp->data=number;
	temp->next=0;
	if (rear!=NULL)
		rear->next=temp;
	rear=temp;

	if (front==NULL)	//list empty to start
		front=rear;

	return *this;
}
	
//Pre-Condition:  Non-empty list
list & list::remove_rear ()
{
	node *p, *followp=NULL;

	for (p=front; p!=rear; p=p->next)
		followp=p;

	if (followp!=NULL)
		followp->next=0;
	delete p;

	rear=followp;
	if (rear==NULL)	//list empty
		front=NULL;

	return *this;
}

bool list::empty() const
{
	return (front==NULL);
}

//	Auxiliary function IMPLEMENTATION

ostream & operator<< (ostream & out, list & L)
{
	node *temp;

	for (temp=L.front; temp!=NULL; temp=temp->next)
		out << temp->data << endl;

	return out;
}

/**************************************************************************/
// ListIterator IMPLEMENTATION
ListIterator::ListIterator(const list & L) //Constructor
{
	current = L.front;
}

ListIterator::ListIterator(const ListIterator & I) //Copy Constructor
{
	current = I.current;
}
	
	//"next" places data from current node into
	//parameter and advances current pointer
	//returns true if successful
	//false if at end of list

bool ListIterator::next(int & number)
{
	if (current==NULL)
		return false;

	number = current->data;
	current = current->next;

	return true;
}
