// linkedList.cc P. Conrad 4/28/06
// linked list library file for CISC181
// demo some linked list functions


#include <iostream>
using std::cout;
using std::endl;

#include "linkedList.h"

// print all elements in the list on cout
void printList(Node_S *head)
{
  for (Node_S *p = head; p; p=p->next)
    cout << p->data << endl;
}

// return true if x in the list, o/w false
bool isInList(int x, Node_S *head)
{
  for (Node_S *p = head; p; p=p->next)
    {
      if ( p->data == x   )
	{
	  return true;
	}
    }

  return false;
}

// return true if x in the list, o/w false
bool isInListv2(int x, Node_S *head)
{

  Node_S *p = head;

  while (p!=NULL)
    {
      if (p->data == x)
	return true;

      p = p->next;
    }
  
  return false;

}

// count how many times x appears,
// assuming list may contain duplicates

int countOccurences(int x, Node_S* head)
{

  int count = 0;
  
  for (Node_S *p=head; p!=NULL; p=p->next)
    {
      if (p->data==x)
	count ++;
    }

  return count;

}

void insertAtTail(int x, 
		  Node_S **headPtr, 
		  Node_S **tailPtr)
{

  Node_S *p = new Node_S;

  // put the data into it
  p->data = x;
  p->next = NULL;

  if ( (*headPtr) == NULL ) // if the head is null
    (*headPtr) = p;  
  else
    (*tailPtr)->next = p ;

  (*tailPtr) = p;


}
