//
// list.cpp
// NodeTutorHelp
//
// Created by Kurtis on 3/25/13.
// Copyright (c) 2013 Kurtis. All rights reserved.
//
#include "list.h"
#include "node.h"
#include <iostream>
using namespace std;
//CONSTRUCTOR/DESTRUCTOR
list::list()
{
head = NULL;
}
list::~list() // called when object goes out of scope
{
node *cursor = head;
while (cursor != NULL)
{
node *nextNode = cursor->link_field;
delete cursor;
cursor = nextNode;
}
head = NULL;
}
// MEMBER FUNCTIONS
void list::insert_front(const double valueToAdd)
{
node* newNode = new node(valueToAdd, NULL); // create a new node to add to the list
newNode->link_field = head; //set new node's data field to the rest of the list
head = newNode;
}
void list::removeDublicateEntries()
{
node * cursorOne = head;
node * cursorTwo = head->link_field;
while(cursorOne != NULL)
{
cout << "enter first loop" << endl;
if (cursorOne)
cout << "cursor 1: " << cursorOne->data_field <<endl;
while(cursorTwo != NULL)
{
cout << "Enter Second Loop" << endl;
cout << cursorTwo->data_field << " Curse 2" << endl;
//compare it
if (cursorOne->data_field == cursorTwo->data_field)
{
//prev->next = current->next to delete
// in order to delete only one, I must find a way to set the link_field of the previous node to cursor 1 to
// the link field of the node that's to be deleted
cout << cursorOne->data_field << "being removed" << endl;
cursorOne->link_field = cursorTwo->link_field; //if they are the same, cursor1 link field must point to cursorTwo's
cursorTwo = NULL;
delete cursorTwo; //delete the node that cursor2 is pinting at if its the same as cursor1
cursorTwo = cursorOne; //set cursor2 to cursor1
}
cursorTwo = cursorTwo->link_field;
}
cursorOne = cursorOne->link_field;
if (cursorOne)
cursorTwo = cursorOne->link_field;
}
}
int list::count() const
{
const node *cursor; //Pointer to a node that
int answer = 0;
for (cursor = head; cursor != NULL; cursor = cursor ->link_field)
++answer;
return answer;
}
void list::print() const //print current linked list members
{
const node *cursor;
for (cursor = head; cursor != NULL; cursor = cursor->link_field)
//set cursor = to data member head, keep going as long as cursor != null, to increment, set cursor to link_field of the node it's currently pointing at for the next node in sequence
cout << cursor->data_field << endl;
}