/*------------------------------------------------------------------------------
Description:  List class
Author: Chris Huyler
Language: Builder C++
Name: List.h
Date: March 30, 1999
------------------------------------------------------------------------------*/

#include "node.h"
template <class E>
class list {
	public:
    	list();
        	~list();
        void insert(E item);
        bool remove(E target);
        bool isEmpty() const;
        bool isFront(E& item);
        bool isBack(E& item);
        void setIterator();
        bool more();
        E next();
    private:
        void clear();
        node<E> *front,*back;
        node<E> *cursor;
        int size;
};

template<class E>
list<E>::list() {
	front = NULL;
   back = NULL;
    size = 0;
}

template<class E>
void list<E>::clear() {
	node<E> *prev;
    prev = front;
    while(prev != NULL) {
    	front = front -> next;
        delete prev;
        prev = front;
    }
    front = NULL;
    back = NULL;
    size = 0;
}

template<class E>
list<E>::~list() {
	clear();
}

template<class E>
bool list<E>::isEmpty() const {
	return(size==0);
}
template<class E>
bool list<E>::isFront(E& item) {
   bool temp;
	if(isEmpty())
   	temp = true;
   else
   	temp = item <= front->itm;
   return(temp);
}
template<class E>
bool list<E>::isBack(E& item) {
   bool temp;
	if(isEmpty())
   	temp = true;
   else
   	temp = item >= back->itm;
   return(temp);
}
template<class E>
void list<E>::insert(E item)
{

/*	node<E> *ptr;
    ptr = new node<E>(item);
    ptr -> next = front;
    size++;
    front = ptr;    */

// new insert orders the data from least to greatest
	node<E> *ptr,*prev,*curr;
	ptr = new node<E>(item);
	curr = front;
   if((curr == NULL) || (curr->itm > item))
   { 													// case for empty list
   	ptr->next = curr;							// or item goes at front
      front = ptr;
      if(curr == NULL)
      	back = front;                    // if one node, front and back point to it
   }
   else
   {
   	prev = front;
      while(curr != NULL)
      {
      	if(item > curr->itm)
         {
         	prev = curr;
            curr = curr->next;
         }
         else
         {
         	ptr->next = prev->next;
            prev->next = ptr;
            if(ptr->next == NULL)
         		back = ptr;         // make back point to last node in list
            curr = NULL;
         }
         if(curr == NULL)
         	prev->next = ptr;
		}
	}
   size++;
}

template<class E>
bool list<E>::remove(E target)
{
	node<E> *prev = NULL;
   bool found = false;
   node<E> *cursor = front;
   while((!found) && (cursor!=NULL)) {
   	if((cursor->itm) == target)
      {
      	found = true;
         if (cursor == front) 			// if first item in list
         {
         	front = front -> next;     // set front to next item
         	if(cursor == back)  			// if last item in list also
         		back = front;     		// set back to front
         }
         else
         {
         	prev -> next = cursor -> next;
            if(cursor == back)
            	back = prev;
         }
         size--;
         delete cursor;

		}
      else
      {
      	prev = cursor;
         cursor = cursor -> next;
      }
	}
   return(found);
}

template<class E>
void list<E>::setIterator() {
	cursor = front;
}

template<class E>
bool list<E>::more() {
	return(cursor != NULL);
}

template<class E>
E list<E>::next() {
	node<E> *ptr = cursor;
    cursor = cursor -> next;
    return(ptr -> itm);
}



