⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alist.h

📁 每条数据库记录包括城市名(任意长的字符串)和城市的坐标(用整数x和y表示)。你的数据库应该允许插入记录、按照名字或者坐标删除或检索记录
💻 H
字号:
#include <string.h>
#define DefaultListSize 1024
template <class Elem>
class AList
{
		
	public:
		int maxSize;
		int listSize;
		int fence;
		Elem* listArray;
		AList(int size = DefaultListSize)
		{
			maxSize = size;
			listSize = fence = 0;
			listArray = new Elem[maxSize];
		}
		~AList() { delete [] listArray;}
		void clear()
		{
			delete []listArray;
			listSize = fence = 0;
			listArray = new Elem[maxSize];
		}
		bool insert (const Elem&);
		bool append (const Elem&);
		bool remove ();
		void setStart () { fence = 0;}
		void setEnd ()   {fence = listSize;}
		void prev ()     {if (fence != 0) fence--;}
		void next ()     {if (fence <= listSize) fence++;}
		int leftLength() const  {return fence;}
		int rightLength() const {return listSize - fence;}
		int findName(Elem& item);
		int findXY(Elem& item);
		bool setPos(int pos)
		{
			if ((pos>=0) && (pos<=listSize)) fence = pos;
			return (pos>=0) && (pos<=listSize);
		}
		bool getValue(Elem& it)const
		{
			if(rigthLength()==0)return false;
			else { it = listArray[fence]; return true;}
		}
};

template <class Elem>
bool AList<Elem>::insert (const Elem& item)
{
	if(listSize == maxSize)return false;
	for (int i=fence; i<listSize; i++)  //记录后移
		listArray[i] = listArray[i-1];
	listArray[fence] = item;   //插入新记录到指定位置
	listSize++;     //拥有记录数增加
	return true;
}
template <class Elem>
bool AList<Elem>::append (const Elem& item)
{
	if(listSize == maxSize)return false;
	listArray[listSize++] = item;
	return true;
}
template <class Elem>
bool AList<Elem>::remove ()
{
	if (rightLength() == 0) return false;
	for (int i=fence; i<listSize-1; i++)
		listArray[i] = listArray[i+1];
	listSize--;
	return true;
}

template <class Elem>
int AList<Elem>::findName(Elem& item)
{
	for(int i=0; i<listSize; i++)
		if(listArray[i].name == item.name)  
			return i;
	return -1;
}

template <class Elem>
int AList<Elem>::findXY(Elem& item)
{
	for(int i=0; i<listSize; i++)
		if(listArray[i].x==item.x && listArray[i].y==item.y)  
			return i;
	return -1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -