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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值