
#include "linklist.h"
#include <iostream>
using namespace std;

//main functions
void printList(TList<float>*);
void insertItem(TList<float>*);
void deleteItem(TList<float>*);

int main (void)
{
	bool loopAgain;
	int i, choice;

	//********** Create a new empty list and populate it*******
	TList <float> MyList;
     
    // populate it with numbers
    for(i=5;i<10;i++) {
         MyList.insertAfter(i);
  		 MyList.gotoNextNode();
	}

    loopAgain = true;
    //**************************Main program loop*************
	while(loopAgain)
    {
       cout<<"1: Add item to list"<<endl;
	   cout<<"2: Delete item from list"<<endl;
	   cout<<"3: Print list"<<endl;
	   cout<<"4: Exit"<<endl;
	   cout<<"enter choice: ";
	   cin>>choice;
	   switch (choice)
	   {
	   case 1: 	
	   	   insertItem(&MyList);
		   break;
	   case 2: 
		   deleteItem(&MyList);
		   break;
	   case 3: 
		   printList(&MyList);
	       break;
	   case 4: 
		   loopAgain = false;
		   break;
	   default: cout<<"selection not recognised"<<endl;
	   }
	}
	return 0;
}

void printList(TList <float> *pList)
{ 
	
    pList->resetCurrent();
	//print all items in list consecutively
	while (!pList->endOfList())
	{
		pList->gotoNextNode();
		cout<<pList->getNodeItem()<<endl;
	}
}  

void insertItem(TList<float> *pList)
{
	float data;
	cout <<"endl<<Enter item to add: ";
	cin >> data;
	//start at beginning of list, stop if end is reached
    pList->resetCurrent();

	while (!pList->endOfList())
	{
		  if (pList->getNextNodeItem() > data)
			  break;  //otherwise stop when correct position is found
		  pList->gotoNextNode();
	}
	//now insert data
    pList->insertAfter(data);
}


void deleteItem(TList<float>* pList)
{
	int data;
	cout <<"endl<<Enter item to delete: ";
	cin >> data;
	//start at beginning of list, stop if end is reached
    pList->resetCurrent();

	while (!pList->endOfList())
	{
		  if (pList->getNextNodeItem() == data)
			  break;  //otherwise stop when correct position is found
		  pList->gotoNextNode();
	}
	//now insert data
    pList->deleteNext();
}



