#include <iostream>
#include "_INTSLL.h"

SllList::~SllList()
{
     for(NodeInt *p; !isEmpty();)
     {
          p = head->next;
          delete head;
          head = p;
     }
}

void SllList::addHead(int n)
{
     head = new NodeInt(n, head);
     if(tail == 0)
          tail = head;
}

void SllList::addTail(int n)
{
     if(tail != 0)
     {
          tail->next = new NodeInt(n);
          tail = tail->next;
     }
     else
          head = tail = new NodeInt(n);
}

int SllList::deleteHead()
{
     int n = head->info;
     NodeInt *TEMP = head;
     if(head == tail) // if only 1 node is in list
          head = tail = 0;
     else
          head = head->next;
     delete TEMP;
     return n;
}

int SllList::deleteTail()
{
     int n = tail->info;
     if(head == tail) // if 1 node on list
     {
          delete head;
          head = tail = 0;
     }
     else
     {  // if nodes on list > 1
          NodeInt *TEMP;  // find predecessor of tail
          for(TEMP = head; TEMP->next != tail; TEMP = TEMP->next);
          delete tail;
          tail = TEMP; // predecessor of tail becomes tail;
          tail->next = 0;
     }
     return n;
}

void SllList::deleteNode(int n)
{
     if(head != 0) // if list is not empty
     {
          if(head == tail && n == head->info) // if only 1
          {
               delete head;
               head = tail = 0;
          }
          else if(n == head->info) // if more then 1 node in list
          {
               NodeInt *TEMP = head->next;
               head = head->next;
               delete TEMP; // delete old head
          }
          else // if more then 1 node in list
          {
               NodeInt *before, *TEMP;
               for(before = head, TEMP = head->next; TEMP != 0 && !(TEMP->info == n); before = before->next, TEMP = TEMP->next);
               if(TEMP != 0)
               {
                    before->next = TEMP->next;
                    if(TEMP == tail)
                         tail = before;
                    delete TEMP;
               }
          }
     }
}

bool SllList::InList(int n) const
{
     NodeInt *TEMP;
     for(TEMP = head; TEMP != 0 && !(TEMP->info == n); TEMP = TEMP->next);
     return TEMP != 0;
}