#include <assert.h>
#include <iostream.h>
#include "Student.h"

class List //class declarations and functions for the linked list
{
private:
	class StudentListPtr
	{
	public:
		Student info;
		StudentListPtr *next;
		StudentListPtr(Student newStudent, StudentListPtr * newNext)
			:info(newStudent), next(newNext)
		{ }
	};

	StudentListPtr * head;
	StudentListPtr * copy(StudentListPtr* list) const
	{
		return list==0 ? 0 
			:new StudentListPtr(list->info, copy(list->next));
	}

	void deleteList(StudentListPtr * list)
	{
		if (list != 0)
		{
			deleteList(list -> next);
			delete list;
		}
	}

 public:
	List()
		:head(0)
	{ }

	List(const List & studentList)
		:head(copy(studentList.head))
	{ }

	~List()
	{
		deleteList(head);
	}

	void insert(Student s)
	{
		head=new StudentListPtr(s, head);
	}

	bool isEmpty()
	{
		return head == NULL;
	}

	void remove(int idNum)
	{
		StudentListPtr* p1=head;
		StudentListPtr* p2=NULL;
		if (p1->info.IDNumber == idNum && p1->next == NULL)
		{
			delete p1;
			head = NULL;
		}
		else if (p1->info.IDNumber ==idNum && p1->next != NULL)
		{
			head = p1->next;
			delete p1;
		}
		else
		{
			while (p1->info.IDNumber != idNum)
			{
				p2 = p1;
				p1 = p1->next;
			}
			p2->next = p1->next;
			delete p1;
		}
	}

	bool isInClass(int idNum)
	{
		for (StudentListPtr* p = head; p != 0; p = p -> next)
			if (p->info.IDNumber == idNum)
				return true;
			return false;
	}

	void showName(int idNum)
	{
		StudentListPtr* p;
		for (p  = head; p != 0; p = p -> next)
			if (p->info.IDNumber == idNum)
				cout<<p->info.lastName<<'\t'<<p->info.firstName<<endl;
	}

	Student& retrieve (int idNum)
	{
		StudentListPtr* p;
		for (p=head; p!=0; p = p->next)
			if ( p->info.IDNumber==idNum )
				return p->info;
		return p->info;  // need to fix this line
	}

	friend ostream & operator << (ostream & out, List & s)
	{
		for (StudentListPtr * temp = s.head; temp != 0; temp = temp -> next )
			out << temp -> info << endl;
		return out;
	}
};