All pastes #85530 Raw Edit

Untitled

public text v1 · immutable
·published 2006-07-12 00:38 UTC
rendered paste body
#include "list.h"
#include "node.h"
#include <iostream>
#include <new>
#include <stdexcept>
using namespace std;
 
List::List()
{
        pos = 0;
        front = 0;
}
 
List::~List()
{
        cout <<"destructor called " << endl;
 
        Node * temp;
        while(front != 0)
        {
                temp=front->next;
//              cout << front->data << " ";
                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)
{
// cout <<" calling copy constructor " << endl;
        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()
{
        /*
        try
        {
                Node * temp = new Node();
                delete temp;
                return false;
        }
        catch (bad_alloc e)
        {
                return true;
        }
        */ 

	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;
        }
}
 
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->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);
				// cout << " b " << v.data << " ";
				pos = oldPos;
				return;
			}
		}
		// it is greater than all items in the list add at back
		AddAtRear(v);
		// cout << " r " << v.data << " ";
		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->prev = pos;
		temp->next = nPtr;
	}
}

void List::Print()
{
	Node * temp = FindNode(front, 0);
//	cout << "end value = " << temp->data << endl;
}