#include "list.h"

/*
constructor's purpose is to make nodes i.e. dynamic variables containing 1
or more values and a pointer variable that references the next node, else NULL
*/

Node::Node(int _val, Node* _next)
{
  next = _next;
  value = _val;
}

Node* Node::prefix(int value, Node* head)
{
  head = new Node(value,head);
  return head;
}


Node* Node::searchElem(Node* head,int searchKey)
{
  Node* trav = head;
  while(searchKey != trav->getValue() && trav->getNext() != NULL)
    trav = trav->getNext();
  
  if( searchKey == trav->getValue() ) 
    return trav;
  else 
    return NULL;
}



/*
FUNCTION: appends in the middle or at the end of a linked list
INPUT: 
1. pointer to the previous node you are appending to
2. the value contained in the successive node you are appending
*/
void Node::insert(Node* prev, int value)
{
  prev->setNext(new Node(value,prev->next));
}

/*
FUNCTION: removes a node and deallocates the memory assigned to its dynamic variable
INPUT:
1. pointer to the previous node
2. pointer to the node we would like to remove
*/
void Node::remove(Node* prev, Node* target)
{
  prev->next = target->next;
  delete target; 
  /*
  memory allocated to dynamic variables comes from the freestore area of main memory which is limited, for this reason we use delete to restore/deallocate the memory used to the operating system which governs the freestore so we can use this memory for other processes
  */
}

int Node::getValue()
const
{
  return value;
}

Node* Node::getNext()
const
{
  return next;
}

void Node::setValue(int _value)
{
  value = _value;
}

void Node::setNext(Node* _next)
{
  next = _next;
}