All pastes #87425 Raw Edit

Something

public text v1 · immutable
·published 2006-07-13 23:11 UTC
rendered paste body
#include "list.h"
#include "node.h"
#include "employee.h"
#include <iostream>
#include <new>
#include <stdexcept>
using namespace std;
 
List::List()
{
        pos = 0;
        front = 0;
}
 
List::~List()
{
        Node * temp;
		while(front != NULL)
        {
                temp = front->next;
                delete front;
                front = temp;
        }
        cout << endl;
		
}

List& List::operator=(List & L)
{
        if(this==&L)
                return *this;
        Node n;
        front=0;
        pos=0;
        Node * temp=0;
        Node * start;
        start = L.front;
        while(start != temp)
        {
                temp=FindNode(start,temp);
                if(temp==0)
                {
                        break;
                }
                AddAtFront(*temp);
        }
 
        return *this;
}

Node * List::FindNode(Node * start, Node * end)
{
        Node * temp = start;
        Node * test;
        while(temp != end)
        {
                test = temp->next;
                if(test == end)
                {
                        return temp;
                }
                else
                {
                        temp = test;
                }
        }
        
        return end;
}
 
List::List(List & L)
{
        Node n;
        front=0;
        pos=0;
        Node * temp=0;
        Node * start;
        start = L.front;
        while(start != temp)
        {
                temp=FindNode(start,temp);
                if(temp==0)
                {
                        return;
                }
                AddAtFront(*temp);
        }
 
}

bool List::IsFull()
{
	return false;
}

bool List::IsEmpty()
{
    return front==0;
}
 
bool List::EndOfList()
{
        if(pos==NULL)
                return false;
        if(pos->next==NULL)
                return true;
        else
                return false;
}
 
void List::GetNext(Node &v)
{
        if(IsEmpty())
        {
			throw "empty list";
        }
 
        if(pos==NULL)
        {
            pos=front;
			v = *pos;
            return;
        }
 
        pos=pos->next;
        if(pos==NULL)
        {
            pos=front;
        }
        v = *pos;
        return;
}
 
void List::Reset()
{
        pos = NULL;
}
 
void List::AddAtFront(Node &v)
{
        Node * nPtr;
        if(!IsFull())
        {
                nPtr = new Node();
                nPtr->next=front;
                nPtr->data=v.data;
                front = nPtr;
				pos = front;
        }
}
 
void List::AddAtRear(Node &v)
{
        Node * nPtr;
        if(!IsFull())
        {
            if(front==0)
            {
                 AddAtFront(v);
                 return;
            }
            else
            {
                 Node * temp;
                 temp = FindNode(front, 0);
                 if(temp == 0)
                 {
                     return;
                 }
                 nPtr = new Node();
                 temp->next = nPtr;
				 nPtr->next = NULL;
                 nPtr->data = v.data;
            }
        }
}
 
void List::FindItem(Node & v, bool & found)
{
        found=false;
        if(IsEmpty())
        {
                return;
        }
        Node n;
        Reset();
        while(!EndOfList())
        {
                GetNext(n);
                if(n.CompareTo(v)==0)
                {
                        found=true;
                        break;
                }
        }
        return;
}

void List::DeleteItem()
{
	Node * tempPtr = 0;
	
	if(pos == front)
	{
		front = front->next;
		delete pos;
		Reset();
	}
	else
	{
		tempPtr = front;
		while(tempPtr != NULL)
		{
			if(tempPtr->next == pos)
			{
				tempPtr->next=pos->next;
				delete pos;
				Reset();
				break;
			}
			else
			{
				tempPtr = tempPtr->next;
			}
		}
	}
}

void List::InsertInOrder(Node &v)
{
	if(front == 0)
	{
		AddAtFront(v);
		return;
	}
	else
	{
		Node vv;
		Node * oldPos = pos;
		Reset();
		while(!EndOfList())
		{
			GetNext(vv);
			if(v.CompareTo(vv) <= 0)
			{
				// we found where it goes
				InsertBefore(v);
				pos = oldPos;
				return;
			}
		}
		// it is greater than all items in the list add at back
		AddAtRear(v);
		pos = oldPos;
	}
}

void List::InsertBefore(Node &v)
{
	if(front == 0 || pos == 0 || pos == front)
	{
		AddAtFront(v);
	}
	else
	{
		Node * temp = FindNode(front, pos);
		if(temp == 0)
		{
			throw "Bad return from FindNode in InsertBefore.";
		}
		Node * nPtr = new Node();
		nPtr->data = v.data;
		nPtr->next = pos;
		temp->next = nPtr;
	}
}