//List.cc, list class implementation file
#include <iostream.h>
#include <stddef.h>
#include <assert.h>
#include "List.h"
#include "Node.h"
List::List(): head(NULL), size(0){} //default constructor
List::List(const List& list):size(list.size) //copy constructor
{ //copys the size
if (list.head==NULL) //checks to see if its an
head=NULL; //empty list, if so sets the
else //pointer to null
{
head=new Node; //creates new node
assert(head != NULL); //checks to make sure dynamic
//memory worked if not exits
head->setdata(list.head->showdata()); //sets the new nodes data area
//equal to the original lists
//data area
Node* nptr = head;
for (Node* ptr = list.head->shownext(); ptr != NULL; ptr = ptr->
shownext())
{ //runs through the list and
//copies it until the original
//list ends (ptr == NULL)
nptr->setnext(new Node); //creates the next node
assert(nptr->shownext() != NULL); //makes sure the new node exist
nptr = nptr->shownext(); //moves the pointer to the new
//node
nptr->setdata(ptr->showdata()); //and copies the data in the
//original node to the
//corresponding node in the
//new list
}
nptr->setnext(NULL); //ends the list buy setting the
//last nodes next pointer to NULL
}
}
List::~List() //destructor
{
while (!isempty()) //runs through the list until the
//size is equal to 0, each time
//it deletes the first node in
//the list
del(1);
}
const List& List::operator =(const List& list)
{ //overloaded = operator
if (&list != this) //makes sure they are not the
{ //same to begin with
//of the old list
while (!isempty()) //deletes stuff in host list
{ //if there was anything there
del(1);
}
size=list.size; //sets the new size to the size
if (list.head==NULL) //if the old list is empty
head=NULL; //sets the host head ptr to NULL
else
{ //otherwise it creates a head
//node for a new list
head=new Node;
assert(head != NULL); //makes sure the node exists
head->setdata(list.head->showdata()); //copies the data of the first
//node
Node* nptr = head; //creates a new pointer to the
//head of the list
for (Node* ptr = list.head->shownext(); ptr != NULL; ptr = ptr->
shownext())
{ //copy constructor code
nptr->setnext(new Node); //creates a new node linked to
//the head node
assert(nptr->shownext() != NULL); //makes sure the new node is
//there
nptr = nptr->shownext(); //moves the pointer to the new
//node
nptr->setdata(ptr->showdata()); //copies the data portion
}
nptr->setnext(NULL); //sets the last pointers next
//pointer to NULL
}
}
return *this;
}
bool List::isempty() const //func to show if list is empty
{
return bool(size==0);
}
int List::listlength() const //returns the current size of
{ //the list object
return size;
}
void List::output() const //prints out the contents of the
{ //entire list, may not be used
Node* ptr=head; //within a cout statement in the
while (ptr != NULL) //driver program
{
cout << ptr -> showdata() << " ";
ptr=ptr -> shownext();
}
cout << endl;
}
bool List::insert(int pos, int newitem) //node insertion function
{
if (pos<1 || pos>size+1) //makes sure inserting in a valid
return false; //position
size++; //increases the lists size by 1
Node* newptr=new Node; //creates a pointer to the new
//node
assert(newptr != NULL); //makes sure dynamic mem worked
newptr -> setdata(newitem); //copies the incoming data into
//the new node
if (pos==1) //case 1, pos == 1, then sets the
//new nodes next ptr to the
//head of the original list
{
newptr -> setnext(head);
head=newptr; //resets head to point to the
//newly created new head node
return true;
}
else //case2, the new node is to be
//inserted after the first node
{
Node* cur = ptrto(pos-1); //creates a pointer to point to
//the node before the insertion
//spot
newptr -> setnext(ptrto(pos)); //sets the new pointers next ptr
//to point to the node after
//the insertion point
cur -> setnext(newptr); //sets the previous nodes next
//ptr to the new node
return true;
}
return false;
}
bool List::del(int pos) //delete function
{
if (pos<1 || pos>size) //checks for valid position
return false;
size--; //decreases the lists size
Node* cur;
if (pos==1 && head->shownext() != NULL) //case1, the position is one and
//list contains nodes
{
cur=head; //sets a cur ptr to node 1
head=head -> shownext(); //moves head ptr up one
}
else if (pos >1 && head->shownext() != NULL) //case2, pos>1 and the list
{ //contains nodes
Node* pred=ptrto(pos-1); //makes a ptr to the node before
//the one to be deleted
cur=pred -> shownext(); //marks the node to be deleted
pred -> setnext(cur -> shownext()); //continues the list from the
//before the node to be deleted
//to after the node to be del
}
cur -> setnext(NULL); //deleting the node
delete cur;
return true;
}
bool List::find(int pos, int& data) const //find function
{
if (pos<1 || pos>size) //checks for valid position
return false;
Node* ptr=ptrto(pos); //creates a pointer to the pos
data=ptr -> showdata(); //copied the data and mails it
return true; //back, returns true
}
Node* List::ptrto(int pos) const //creates a pointer to the give
{ //position
if (pos<1 || pos>size) //checks for valid position
return NULL;
Node* ptr=head; //creates a ptr to the head node
for (int i=1; i<pos; i++) //runs a loop to get to the
ptr=ptr -> shownext(); //desired node
return ptr; //returns that node
}