#include<iostream>
using namespace std;
#include "OneWayTurn.h"
template <class T>
List<T>::List():pHead(NULL), pPrev(NULL),countElem(0)
{
}
template <class T>
List<T>::List(const List &list)
{
	Element *pTemp = list.pHead;
	while(pTemp != NULL)
	{
		this->addToHead(pTemp->data);
		pTemp = pTemp->next;
	}
}
template <class T>
List<T>::~List()
{
		Element *pTemp;
        while(pHead != NULL)
        {
			pTemp = pHead;
            pHead = pHead->next;
            delete pTemp;
        }
    }
template <class T>
void List<T>::addToEnd(T data)
 {
        Element *temp = new Element();

        if(pHead == NULL)
            pHead = temp;
        else
            pPrev->next = temp;

        temp->data = data;
        temp->next = NULL;
        pPrev = temp;
        countElem++;
    }
template<class T>
void List<T>::addToHead(T data)
{
	Element *temp = new Element();
	if (pPrev == NULL)
		pPrev = temp;
	temp->data = data;
	temp->next = pHead;

	pHead = temp;
	countElem++;
}
template <class T>
void List<T>::addToPosition(T data, int position)
{
	if (position >= countElem || position < 0)
		return;
	if (position == 0)
		this->addToHead(data);
	else if (position == countElem - 1)
		this->addToEnd(data);
	else
	{
		Element *pTemp = pHead;
		int curPosition = 0;
		while(curPosition != position - 1)
		{
			pTemp = pTemp->next;
			curPosition++;
		}
		Element *newElement = new Element();
		newElement->data = data;
		newElement->next = pTemp->next;
		pTemp->next = newElement;
	}
}
template <class T>
void List<T>::print()
{
	cout<<endl;
    Element *pTemp = pHead;
	//ofstream answer ("answer.txt", ios::trunc|ios::in|ios::out);
    while(pTemp != NULL)
    {
        cout << pTemp->data << endl;
        pTemp = pTemp->next;
    }
}
template <class T>
bool List<T>::IsEmpty()
{
        if(countElem == 0)
            return true;
        else
            return false;
}
template <class T>
int List<T>::search(T key)
{
	Element *ptemp = pHead;
	int k = 0;
	while(ptemp != NULL)
	{
		k++;
		if(ptemp->data == key)
		{
			return k;
		}
		ptemp = ptemp->next;
	}	cout<<ptemp->data<<endl;
	k = -1;
	return k;		
}
template <class T>
T List<T>::PrintToFound(int key)
{
	Element *ptemp = pHead;
	for(int i = 0; i < key-1; i++)
	{
		ptemp = ptemp->next;
	}
	return ptemp->data;
}
template <class T>
void List<T>::sort()
{
		Element *ptemp = pHead;
		Element *p;
		Element *k=new Element;
		for(;ptemp->next != NULL; ptemp=ptemp->next)
		{
			for(p=pHead;p->next != NULL; p=p->next)
			{
				if(p->next->data < p->data)
				{
					k->data=p->data;
					p->data=p->next->data;
					p->next->data=k->data;
				}
			}
		}
		return;
}
template <class T>
void List<T>::delFirst()
{
	if ( pHead != NULL )
	{
		Element *pTemp = pHead->next;
		delete []pHead;
		pHead = pTemp;
		countElem--;
	}
}
template <class T>
void List<T>::delPosition(int position)
{
	if(position >= countElem || position < 0)
		return;
	int curPosition = 0;
	if (position == 1)
		this->delFirst();
	else
	{
	Element *pTemp = pHead;
	while(curPosition != position-2)
	{
		pTemp = pTemp->next;
		curPosition++;
	}
	Element *newElement = new Element();
	newElement->next = pTemp->next->next;
	delete []pTemp->next;
	pTemp->next = newElement->next;
	countElem--;
	}
}
template <class T>
void List<T>::delLast()
{
	Element *pTemp = pHead;
	int curPosition = 0;
	while(curPosition < countElem-2)
	{
		pTemp = pTemp->next;
		curPosition++;
	}
	pPrev = pTemp;
	delete []pTemp->next;
	pPrev->next = NULL;
}

template <class T>
int List<T>::menu()
{
	int choise = 0;
	cout<<endl;
	cout<<" Добавить      (1)"<<endl;
	cout<<" Показать      (2)"<<endl;
	cout<<" Найти         (3)"<<endl;
	cout<<" Отсортировать (4)"<<endl;
	cout<<" Удалить       (5)"<<endl;
	cout<<" Выход         (6)"<<endl;
	cin>>choise;
	return choise;
}