#include "myList.h" void List::add(Object &newNode) { Node *newElement = new Node(&newNode); if(head != 0)//if there is already a node just add item to head of list { newElement->next = head; head = newElement; noEntries++; } else//there is only one entry therefore head and tail point to the same place { newElement->next = head; head = newElement; tail = newElement; noEntries++; } } void List::push_back(Object &newNode) { Node *newElement = new Node(&newNode); if(head != 0) { tail->next = newElement; tail = newElement; noEntries++; } else { tail = newElement; head = newElement; noEntries++; } } //start at the head of the list //delete items as you move along //stop when at the end i.e. when //the next pointer value is 0 List::~List() { while(head!=0) { Node *temp = head; head = head->next; delete temp; //delete what temp points at! } } Object &List::at(int index) { Node *temp = head; int i; for(i = 0; i < index; ++i) { temp = temp->next; } return &temp->info; } int List::size() { int i = 0; Node *temp = head; while(temp != tail) { ++i; } return i; } void List::erase(int index) { Node *temp = at(index); delete temp; //Might actually need to use the destructor at this point in which case it would be //something like ~Node(temp); where that deletes all variables and references relating to temp } //Tell each item in the list to print itself //start at the front i.e. at the head of the list ostream &List::print(ostream &co) { Node *temp = head; //point wherever head is pointing (head always points to current first element) while (temp!=0) //while there are actually elements int the list { temp->info->printOn(co);//info is a pointer to the the first element in the list (the list itself). Call printOn for that list temp = temp->next; //printOn is a friend of linked list and therefore friend to all other classes used in LinkedList.h } return co; //ostream used is cout therefore return type returns a reference to an ostream in this case returning cout } ostream &List::printAt(ostream &co, int index) { Node *temp = at(index); temp->info->printOn(co); return co; }