チェーンテーブル


チェーンテーブルの配列ベースの実装:

typedef int ItemType;
typedef void (*FuncT)(ItemType &_item);
class List
{
	public:
		List();
		List(int num,const ItemType &_item);
		List(const List & _list);
		bool isEmpty()const;
		bool isFull()const;
		int getLength()const;
		int getMaxSize()const;
		void insert(int index,const ItemType &_item=ItemType());
		void remove(const ItemType &_item);
		void erase(int index);
		void retrieve(int index,ItemType &_item);
		void traverse(FuncT visit);
	private:
	        enum{MAX=20};
		ItemType alist[MAX];
		int size;

};
#include<cstddef>
#include<cassert>
#include"list_array2.h"
List::List():size(0)
{
}
List::List(int num,const ItemType &_item)
{
	size=num;
	for(int i=0;i<num;i++)
		alist[i]=_item;
}
List::List(const List &_list)
{
	size=_list.size;
	for(int i=0;i<size;i++)
		alist[i]=_list.alist[i];
}
bool List::isEmpty()const
{
	return size==0;
}

bool List::isFull()const
{
	return size==MAX;
}
int List::getLength()const
{
	return size;
}
int List::getMaxSize()const
{
	return MAX;
}
void List::insert(int index,const ItemType &_item)
{
	assert(!isFull());
	assert(index>0&&index<=size);
	size++;
	int i;
	for(i=size-1;i>=index;i--)
		alist[i]=alist[i-1];
	alist[i]=_item;
}
void List::remove(const ItemType &_item)
{
	assert(!isEmpty());
	int p;
	for(p=0;(_item!=alist[p])&&(p<size);p++)
	{
	}
	assert(p!=size-1);
	size--;
	int i;
	for(i=p;i<size;i++)
		alist[i]=alist[i+1];
        alist[i]=ItemType();
}
void List::erase(int index)
{
	assert(index>0&&index<=size);
	size--;
	int i;
	for(i=index-1;i<size;i++)
		alist[i]=alist[i+1];
	alist[i]=ItemType();
}
void List::retrieve(int index,ItemType &_item)
{
	assert((index>0)&&(index<=size));
	_item=alist[index-1];
}
void List::traverse(FuncT visit)
{
    for(int i=0;i<size;i++)
	    visit(alist[i]);
}

チェーンテーブルは配列実装2に基づいています(配列項目は配列インデックスによってリンクされます):
typedef int ItemType;
typedef void (*FuncT)(ItemType &_item);
class List
{
	public:
		List();
		bool isEmpty()const;
		int getLength()const;
		void insert(int index,ItemType &_item);//ignore exception
		void remove(int index);//ignore exception
		void retrieve(int index,ItemType &_item);//ignore exception
		void traverse(FuncT visit);
	private:
		enum{MAX=20};
		class Node
		{
			private:
				ItemType item;
				int next;
			public:
				Node();
				Node(ItemType &_item,int _next);
				int getNext()const;
				void setNext(int _next);
				void setItem(ItemType _item);
				 ItemType & getItem();
		};
		Node alist[MAX];
		int head;
		int free;
		int size;
		int find(int index)const;
};
#include"list_array.h"
List::Node::Node():item(),next(-1)
{
}
List::Node::Node(ItemType &_item,int _next)
{
	item=_item;
	next=_next;
}
int List::Node::getNext()const
{
	return next;
}
void List::Node::setNext(int _next)
{
	next=_next;
}
void List::Node::setItem(ItemType _item)
{
	item=_item;
}
ItemType &List::Node::getItem()
{
	return item;
}
List::List()
{
	size=0;
	free=0;
        head=-1;
	int i;
	for(i=free;i<MAX-1;i++)
		alist[i].setNext(i+1);
	alist[i].setNext(-1);
}
int List::find(int index)const
{
	if((index<1)||(index>size))
		return -1;
	else
	{
		int i=1;
		int temp=head;
		while(i<index)
		{
			temp=alist[temp].getNext();
			i++;
		}
		return temp;
	}
}

bool List::isEmpty()const
{
	return (size==0);
}
int List::getLength()const
{
	return size;
}
void List::insert(int index,ItemType &_item)
{
	size++;
	if(head==-1)
	{
		head=0;
		alist[head]=Node(_item,-1);
		free=1;
	}
	else
	{
		if(index==1)
		{
			int temp=free;
			free=alist[free].getNext();
			alist[temp]=Node(_item,head);
			head=temp;
		}
		else
		{
			int pre=find(index-1);
			int temp=free;
			free=alist[free].getNext();
			alist[temp]=Node(_item,alist[pre].getNext());
			alist[pre].setNext(temp);
		}
	}
}
void List::remove(int index)
{

	if(index==1)
	{
		int temp=head;
		head=alist[head].getNext();
		alist[temp].setNext(free);
		alist[temp].setItem(ItemType());
		free=temp;
	}
	else
	{
		int pre=find(index-1);
		int temp=free;
		free=alist[pre].getNext();
		alist[pre].setNext(alist[free].getNext());
		alist[free].setNext(temp);
		alist[free].setItem(ItemType());
	}
	size--;
}
void List::retrieve(int index,ItemType &_item)
{
	int cur=find(index);
	_item=alist[cur].getItem();
}
void List::traverse(FuncT visit)
{
	int cur=head;
	while(cur!=-1)
	{
		visit(alist[cur].getItem());
		cur=alist[cur].getNext();
	}
}

チェーンテーブルのポインタベースの実装:
#include<cstddef>
typedef int ItemType;
typedef void (*FuncT)(ItemType &_item);
class List
{
	public:
		List();
		List(const List &_list);
		~List();
                bool isEmpty()const;
		int getLenght()const;
		void insert(int index,ItemType &_item);//ignore exception
		void remove(int index);//ignore exception
		void retrieve(int index,ItemType &_item)const;//ignore exception
		void traverse(FuncT visit);
	private:
		struct Node
		{
			ItemType item;
			Node * next;
		};
		int size;
		Node *head;
		Node *find(int index)const;
              

};
#include"list_pointer.h"
#include<cstddef>
#include<cassert>
List::List()
{
	size=0;
	head=NULL;
}
List::List(const List &_list)
{
	if(_list.head==NULL)
		head=NULL;
	else
	{
         	head=new Node;
           	assert(head!=NULL);
		head->item=_list.head->item;
		Node *newP=head,*temp=_list.head->next;
		while(temp!=NULL)
		{
			newP->next=new Node;
			assert(newP->next!=NULL);
			newP=newP->next;
			newP->item=temp->item;
			temp=temp->next;
		}
		newP->next=NULL;
	}
	size=_list.size;
}
List::~List()
{
	while(head!=NULL)
	{
		Node *temp=head;
                head=head->next;
		delete temp;
	}
}	
List::Node *List::find(int index)const
{
	if((index<=1)||(index>size))
		return NULL;
	else
	{
		Node *temp=head;
		for(int i=2;i<index;i++)
	                  	temp=temp->next;
	         return temp;
       	}
}                        

bool List::isEmpty()const
{
	return (head==NULL);
}
int List::getLenght()const
{
	return size;
}
void List::insert(int index,ItemType &_item)
{
	Node *newP=new Node;
	assert(newP!=NULL);
	newP->item=_item;
        size++;
	if(index==1)
	{
		newP->next=head;
		head=newP;
	}
	else
	{
		Node *pre=find(index);
		newP->next=pre->next;
		pre->next=newP;
	}
}
void List::remove(int index)
{       size--;
	Node *temp;
	if(index==1)
	{
		temp=head;
		head=head->next;
		temp->next=NULL;
		delete temp;
	}
	else
	{
		Node* pre=find(index);
		temp=pre->next;
		pre->next=pre->next->next;
		temp->next=NULL;
		delete temp;
	}
	
}
void List::retrieve(int index,ItemType &_item)const
{
	if(index==1)
		_item=head->item;
	else
	{
		Node *pre=find(index);
		_item=pre->next->item;
	}
}
void List::traverse(FuncT visit)
{
	Node *temp=head;
	while(temp!=NULL)
	{
		visit(temp->item);
		temp=temp->next;
	}
}

標準テンプレートライブラリクラスlist:
template<class T>
class std::list
{
	public:
		list();
		//Default costructor ;initializes an empty list;
		//precondition :None;
		//Postcondition :An empty list will  exists;
		list(size_type num,const T&val=T());
		//Constructor;initializes list to have num elements;
		//with value val
		//precondition :None;
		//postcondition:A list with num elements.
		list(const list<T>&anotherlist);
		//Constructor ;initialize a list to have the same 
		//elements with anotherlist
		//precodition :None;
		//postcondition :A list with same elements of the anotherlist will exists
		size_type size()const;
		//Determines the length of the list ,size_type is 
		//an integral type
		//precondition :none;
		//postcondition :Returns the number of items the 
		//list that are currently in the list
		size_type max_size()const;
		//determine the maxium number of items the list
		//can hold
		//precondition :NOde;
		//postcondition :Returns the maxium number of items
		bool empty()const;
		//determines whether a list is emtpy
		//precondition :None;
		//postcondition :return true if the list is empty,
		//otherwise returns false.
		iterator insert(iterator i,const T &val=T());
		//Insert a item val into a the list immediately
		//before the element specified by the iterator i,
		//precondition :the iterator must be initialized ,
		//even if the list is empty
		//Postcondition :Item val is inserted into the list
		//and an iterator to the newly inserted item is returned;
		void remove(const T&val);
		//Remove all items with value val from the list.
		//precondition :Node
		//postcondition:The list has no item with value val.
		iterator erase(iterator i);
		//remove the item in the list pointed to the iterator i
		//Precondition :the iterator must be initialized to 
		//point to an element in the list
		//postcondition :Returns an iterator to the item following
		//the removed item .If the removed is the last item in 
		//the list the iterator value will be the same as the 
		//value returned by end().
		iterator begin();
		//returns an iterator to the first item in the list
		//Precondition : None;
		//Postcondition :if the list is empty the iterator value 
		//will be the same as the value returned by end();
		iterator end();
		//Returns an iterator value that can used to test
		//whether the end of the list has been reached
		//precondition :iNone;
		//postcondition :the iterator value for the end of the
		//list is returned 

		void sort();
		//sorts elements according to the operator< and 
		//maintains the relative order of equal elements
		//precondition :None;
		//Postcondition :the list is sorted in ascending order.
};