- 说明
- 启发式规则为转置的自组织线性表
一、说明
1、自组织线性表(self-organizing list):根据实际记录访问模式修改记录的顺序的线性表。
2、管理自组织线性表的三个传统的启发式规则:计数、移至前端和转置。下例采用的是转置。
3、相比较于实验一 线性表的物理实现(顺序表+单向链表),除了修改相应的命名外,增添了转置(transpose)和查找(find)操作。
4、下列的查找操作为顺序查找,且找到第一个符合条件的就返回。
5、以下代码仅供参考。
二、启发式规则为转置的自组织线性表
1、SOList.h
#include <iostream>
using namespace std;
#ifndef _SOList
#define _SOList
namespace wangzhe
{
template <typename E>
class SOList
{
private:
void operator =(const SOList&) {}
SOList(const SOList&) {}
public:
SOList() {}
virtual ~SOList() {}
virtual void clear() =0;
virtual void insert(const E& item) =0;
virtual void append(const E& item) =0;
virtual E remove() =0;
virtual void moveToStart() =0;
virtual void moveToEnd() =0;
virtual void prev() =0;
virtual void next() =0;
virtual int length() const =0;
virtual int currPos() const =0;
virtual void moveToPos(int pos) =0;
virtual const E& getValue() const =0;
virtual void transpose(int pos)=0;//pos处的元素与前一条交换位置
virtual int find(const E& item)=0;//顺序查找元素item
};
}
#endif
2、SOAList.h
#include<iostream>
using namespace std;
#include"SOList.h"
#ifndef _SOAList
#define _SOAList
namespace wangzhe
{
template <typename E>
class SOAList:public SOList<E>
{
private:
int maxSize;
int listSize;
int curr;
E* listArray;
public:
SOAList(int size);
~SOAList();
void clear();
void insert(const E& item);
void append(const E& item);
E remove();
void moveToStart();
void moveToEnd();
void prev();
void next();
int length() const;
int currPos() const;
void moveToPos(int pos) ;
const E& getValue() const ;
void transpose(int pos);
int find(const E& item);
};
}
#endif
3、OLAList.cpp(未给出的部分可参考实验一 线性表的物理实现(顺序表+单向链表))
#include<iostream>
using namespace std;
#include"SOAList.h"
namespace wangzhe
{
template <typename E>
void SOAList<E>::transpose(int pos)
{
if(pos<=0) //首位就别转置了吧
{
//cout<<"Illegal operation!\n";
return;
}
E temp=listArray[pos];
listArray[pos]=listArray[pos-1];
listArray[pos-1]=temp;
}
template <typename E>
int SOAList<E>::find(const E& item)
{
for(moveToStart();currPos()<length();next())
if(getValue()==item) return currPos();
return length();
}
}
4、main.cpp
#include<iostream>
#include<fstream>
using namespace std;
#include"SOList.h"
#include"SOAList.h"
#include"SOAList.cpp"
using namespace wangzhe;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv)
{
for(int i=1;i<=50;i++) cout<<'-';
cout<<"\n"<<" 启发式规则为转置的自组织线性表示例\n";
for(int i=1;i<=50;i++) cout<<'-';
cout<<endl;
SOAList<string> solist(1111111);
ifstream infile1("input.txt",ios::in);//输入
ifstream infile2("find.txt",ios::in);//要查找的数据
ofstream outfile("output.txt",ios::out);//输出
if(!infile1||!infile2)
{
cerr<<"open error!\n";
exit(-1);
}
string word;
while(infile1.peek()!=EOF)//初始化
{
infile1>>word;
solist.append(word);
}
int count=0;//第count次查找
while(infile2.peek()!=EOF)//查找
{
infile2>>word;
outfile<<"第"<<++count<<"个要查找的汉字是:"<<word<<endl;
int index=solist.find(word);
if(index<solist.length())//查找成功
{
outfile<<"查找成功!共查找了"<<index+1<<"次;\n";
solist.transpose(index);//查找成功一次转置一次
}
else //查找失败
{
outfile<<"查找失败!共查找了"<<index<<"次;\n";
}
outfile<<endl;
/*for(solist.moveToStart();solist.currPos()<solist.length();solist.next())
cout<<solist.getValue()<<' ';*/
}
cout<<"查找结束,结果在文件output.txt中可查看。\n";
return 0;
}
5、input.txt
(文出自仓央嘉措《那一世》)文末不要出现多余的空行。
6、find.txt
7、output.txt