c++代码之-------动态数据结构

//IStack.h
#include <iostream>
#include <cassert>

class Link
{
public:
	Link(Link* pnext,int id):pNext(pnext),_id(id){}
	Link* Next() const {return pNext;}
	int Id() const {return _id;}
private:
	Link* pNext;
	int _id;
};
class List
{
public:
	List():pHead(0){}
	~List(){
		while(pHead!=0){
			Link* temp=pHead;
			pHead=pHead->Next();
			delete temp;
		}
	}
	void Add(int id){
		Link* temp=new Link(pHead,id);
		pHead=temp;
	}
	const Link* GetHead() const {return pHead;}
private:
	Link* pHead;
};
class HTable
{
public:
	const List& Find(const char* str) const
	{
		int i=hash(str);
		return _list[i];
	}
	void Add(const char* str,int id) {
	    int i=hash(str);
		_list[i].Add(id);
	}
private:
	int hash(const char* str) const{
		assert(str!=0&&str[0]!=0);
		unsigned h=str[0];
		for(int i=1;i!=std::strlen(str);i++)
			h=(h<<4)+str[i];
		return h%sizeHTableSize;
	}
	static const int sizeHTableSize=127;
	List _list[sizeHTableSize];
};
class StringBuffer
{
public:
	StringBuffer():_curOffset(0){}
	bool WillFit(int len) const{
		return _curOffset+len+1<maxBufferSize;
	}
	void Add(const char *str){
		assert(WillFit(std::strlen(str)));
		std::strcpy(&_buffer[_curOffset],str);
		_curOffset=_curOffset+std::strlen(str)+1;
	}
	int GetOffset() const{
		return _curOffset;
	}
	const char *  GetString(int offset)const {
		return _buffer+offset;
	}
	bool IsEqual(int offset,const char* str) const{
		assert(offset+std::strlen(str)+1<maxBufferSize);
		return std::strcmp(_buffer+offset,str)==0;
	}
private:
	static const int maxBufferSize=500;
	char  _buffer[maxBufferSize];
	int   _curOffset;
};
class StringTable
{
private:
	static const int idNoFound=-1;
	static const int maxStrings=100;
	HTable       _hTable;
	int          _offset[maxStrings];
	int          _currId;
	StringBuffer _strBuffer;
public:
	StringTable():_currId(0){}
	int ForceAdd(char const * str){
		int len=std::strlen(str);
		if(_currId==maxStrings||!_strBuffer.WillFit(len))
			return idNoFound;
		_offset[_currId]=_strBuffer.GetOffset();
		_strBuffer.Add(str);
		_hTable.Add(str,_currId);
		++_currId;
		return _currId-1;
	}
	int Find(char const * str) const{
		const List& list =_hTable.Find(str);
		for (const Link* temp=list.GetHead();temp!=0;temp=temp->Next())
		{
			if(_strBuffer.IsEqual(_offset[temp->Id()],str))
				return _offset[temp->Id()];
		}
		return idNoFound;
	}
	const char* GetString(int id) const{
		assert(0<id&&id<_currId);
		return _strBuffer.GetString(_offset[id]);
	}
};
#include <iostream>
#include <string>
#include "IStack.h"

#define   KEYNUMUp                       0x48             //键盘上键
#define   KEYNUMDown                     0x50             //键盘下键
#define   KEYNUMLeft                     0x4b             //键盘左键
#define   KEYNUMRight                    0x4d             //键盘右键
#define   KEYNUMPageUp                   0x49             //键盘向上翻页键
#define   KEYNUMPageDown                 0x51             //键盘向下翻页键

int main()
{
	StringTable stringTable;
	stringTable.ForceAdd("one");
	stringTable.ForceAdd("two");
	stringTable.ForceAdd("chenyu");
	int id=stringTable.Find("two");
	std::cout<<"one is find and the offset is "<<id<<std::endl;
	std::cin.get();
	return 0;
}


你可能感兴趣的:(c++代码之-------动态数据结构)