65,211
社区成员
发帖
与我相关
我的任务
分享
头文件SimChain.h
/*
* -------Copyright (c)--------
* 2009, Hunan University Software School OOPDA The Third Team 3TminiDB Project
* All rights reserved.
*
* -------File Info-------
* File Name: SimChain.h
* File Description: 主要实现了静态链表中链表类的声明和定义
* Creat Time: 2009年3月31日
* Author: 王洋
* Change History:
* 1.
* 2.
* 3.
*
* -----------Todo List-----------
* 1.添加注释
* 2.统一命名规则
* 3.
*/
#pragma once
template <class T>
class SimNode
{
public:
SimNode(void);
~SimNode(void);
//friend SimSpace<T>;
//private:
T data;
int link;
};
template <class T>
class SimSpace
{
public:
SimSpace(int MaxSpaceSize = /*DefaultSize*/100);//
~SimSpace() { delete [] node; }
int Allocate();
void Deallocate(int& i);
private:
int NumberOfNodes, first;
SimNode<T>* node;
};
template <class T>
SimSpace<T>::SimSpace(int MaxSpaceSize)
{
NumberOfNodes = MaxSpaceSize;
node = new SimNode<T>[NumberOfNodes];
for(int i = 0; i < NumberOfNodes - 1; i++)
node[i].link = i + 1;
node[NumberOfNodes - 1].link = -1;//node[i].link = -1;
first = 0;
}
template <class T>
int SimSpace<T>::Allocate()
{
if(first == -1) throw NoMem();
int i = first;
first = node[i].link;
return i;
}
template <class T>
void SimSpace<T>::Deallocate(int& i)//
{
node[i].link = first;
first = i;
i = -1;
}
//用Record类来套用模板成行,用Stringc类来套用模板成列
template <class T>
class SimChain
{
public:
SimChain() { first = -1; }
~SimChain() { Destory(); }
void Destory();
int Length() const;
bool Delete(int k, T& x);
bool Insert(int k, const T& x);
//bool Find(int k, T& x) const;
//int Search(const T& x) const;
//void Output(ostream& out) const;
private:
int first;
static SimSpace<T> S;
};
template<class T>
void SimChain<T>::Destory()
{
int next;
while(first != -1)
{
next = S.node[first].link;
S.Deallocate(first);
first = next;
}
}
template<class T>
int SimChain<T>::Length() const
{
int current = first;
len = 0;
while(current != -1)
{
current = S.node[current].link;
len++;
}
return len;
}
template<class T>
bool SimChain<T>::Delete(int k, T &x)
{
if (k < 1 || first == -1)
return false;
int p = first;
if (k == 1)
first = S.node[first].link;
else
{
int q = first;
for (int index = 1; index < k - 1 && q != -1; index++)
q = S.node[q].link;
if (q == -1 || S.node[q].link == -1)
return false;
p = S.node[q].link;
S.node[q].link = S.node[p].link;
}
x = S.node[p].data;
S.Deallocate(p);
return true;
}
template<class T>
bool SimChain<T>::Insert(int k, const T &x)
{
if(k < 0) return false;
int p = first;
for (int index = 1; index < k && p != -1; index ++)
p =S.node[p].link;
if(k > 0 && p == -1)
return false;
int y = S.Allocate();
S.node[y].data = x;
if (k)
{
S.node[y].link = S.node[p].link;
S.node[p].link = y;
}
else
{
S.node[y].link = first;
first = y;
}
return true;
}
//template<class T>
//bool SimChain<T>::Find(int k, T &x) const
//{
// if (k < 1) return false;
// int current = first;
// index = 1;
// while(index < k && current != -1)
// {
// current = S.node[current].link;
// index++;
// }
// if(current != -1)
// {
// x = S.node[current].data;
// return ture;
// }
// return false;
//}
cpp文件 测试静态指针.cpp
/*
* -------Copyright (c)--------
* 2009, Hunan University Software School OOPDA The Third Team 3TminiDB Project
* All rights reserved.
*
* -------File Info-------
* File Name: 测试静态指针.cpp
* File Description:
* Creat Time: 2009年3月31日
* Author: 王洋
* Change History:
*
* -----------Todo List-----------
*/
#include "c:\Users\WL\Documents\Visual Studio 2008\Projects\TTTminiDB\TTTminiDB\SimChain.h"
int main()
{
SimChain<int> a;
return 0;
}
template <class T>
class SimChain
{
public:
SimChain() { first = -1; cout << "SimChain" << endl; }
~SimChain() { Destory(); cout << "~SimChain" << endl;}
void Destory();
int Length() const;
bool Delete(int k);
bool Insert(int k, const T& x);
//bool Find(int k, T& x) const;
//int Search(const T& x) const;
//void Output(ostream& out) const;
static SimSpace<T> S;
private:
int first;
};
//关键是这一句
template<class T>
SimSpace<T>
SimChain<T>::S;
粗略的看了一下,指出楼主的几个关键点问题:
一、SimChain类中到处可见类似S.node[first].link;这种写法,node虽是类SimSpace的对象,但
对private属性成员的访问需要在本类中进行(这是访问的作用域问题),解决的办法有两种,其一提供
访问的成员函数,当然函数本身的属性为public。假设为GetValue;
SimNode& Getvalue(int index)const { return node[i];}
那S.node[first].link;的写法就改为了:S.GetValue(first).link;
其二是直接定义SimNode* node;为public属性,这个一楼已经说了;
二、类SimChain中的静态成员static SimSpace<T> S;必须初始化
SimChain::SimSpace<T> s=SimSpace<T>(12); //这个初始化必须在类外,就算在main函数前也行!
其算法的逻辑性没有时间看了,楼主自行检查吧。
template <class T>
class SimSpace
{
public:
SimSpace(int MaxSpaceSize = /*DefaultSize*/100);//
~SimSpace() { delete [] node; }
int Allocate();
void Deallocate(int& i);
SimNode<T>* node;//move to here
private:
int NumberOfNodes, first;
//SimNode<T>* node;
};