#include <iostream.h>
#include <stdlib.h>
#include"LinkedList.h"

// constructor
List::List()
{
  int Null=0;
  headNode = new Node();
  headNode->setNext(NULL);
  currentNode=NULL;
  lastCurrentNode=NULL;
  size=0;
}

int List::sizelist()
{
 return size;
}

// add() class methd
void List::add(int addobject)
{
 Node* newNode = new Node();
 newNode->set(addobject);

 if(currentNode!= NULL)
     {
       newNode->setNext(currentNode->getNext());
       currentNode->setNext(newNode);
       lastCurrentNode = currentNode;
       currentNode=newNode;
      }
 else
     {
       newNode->setNext(NULL);
       headNode->setNext(newNode);
       lastCurrentNode = headNode;
       currentNode = newNode;
     }
    size++;
 }

void List::traverse(List list)
{
    Node* savedCurrentNode  =  list.currentNode;
    list.currentNode  =  list.headNode;
    
    for(int i = 1; list.next(); i++)
    {
        cout << " Element " << i << "   " << list.get()<<endl;
    }
    list.currentNode  =  savedCurrentNode;
}

 // get() class method
 int List::get()
 {
  if(currentNode!=NULL)
  return currentNode->get();
 }

 Node* List::getHead()
 {
  return headNode;
 }
 // next() class method
 bool List::next()
{
  if(currentNode==NULL) return false;
  lastCurrentNode=currentNode;
  currentNode = currentNode->getNext();

  if(currentNode==NULL || size==0)
  return false;
  else
  return true;
}

void List::reverse(Node * headNode)
{
   Node* temp=headNode->getNext();
   int arr[size];
   for (int x=0;x<size;x++)
      {
            if(temp->getNext()!=NULL)
            {
               arr[x]=temp->get();
               temp=temp->getNext();
            }
           else
               arr[x]=temp->get();   
      }
   temp = headNode->getNext();
   for(int x=size-1;x>-1;x--)
   {
    temp->set(arr[x]);
    temp=temp->getNext();
   }
}

void List::addLists(List list, List list1)
{
    Node * temp=list.getHead();
    Node * temp1=list1.getHead();
    while(temp->getNext()!=NULL)
    {
      temp=temp->getNext();
    }
    temp1=temp1->getNext();
    delete list1.headNode;
    temp->setNext(temp1);
    size=list.sizelist()+list1.sizelist();
}

void List::copy(Node * headNode)
{
 List clist;
 Node * temp=headNode->getNext();
 for(int x=0;x<size;x++)
 {
  clist.add(temp->get());
  temp=temp->getNext();
 }
 cout<<"The elements of new copy list 'clist' are"<<endl;
 clist.traverse(clist);
}

void List::replace(int node)
{
 int position;
 int counter=1;
 cout<<"Enter position where you want to replace = ";
 cin>>position;
 if (position>size)
     cout<<position<<" does not exits";
 else
 {
  Node * temp=headNode->getNext();
   while(position!=counter)
   {
     temp=temp->getNext();
     counter++;
   }
  temp->set(node);
 }
}

int List::find(int node)
{
  Node * temp=headNode->getNext();
 for (int x=0;x<size;x++)
 {
   if(node==temp->get())
      return 1;
   else
      temp=temp->getNext();
 }
 return 0;
}

void List::remove()
{
 int remove;
 cout<<"Enter the element to remove = ";
 cin>>remove;
 if(find(remove)==0)
    cout<<"Number doest not exists in LinkedList"<<endl;
 else{
 Node * previousNode=headNode;
 currentNode=previousNode->getNext();
 lastCurrentNode=currentNode->getNext();
 while (lastCurrentNode!=NULL)
 {
   if(remove==currentNode->get())
   {
      previousNode->setNext(lastCurrentNode);
      delete currentNode;
      currentNode=lastCurrentNode;
      lastCurrentNode=lastCurrentNode->getNext();
      size--;
   }
   else
   {
      previousNode=currentNode;
      currentNode=lastCurrentNode;
      lastCurrentNode=lastCurrentNode->getNext();
   }
  }
if(currentNode->getNext()==NULL)
  { 
    if(remove==currentNode->get())
       { 
        delete currentNode;
        previousNode->setNext(NULL);
       }
   }          
 }
}

