合肥工业大学-数据结构二叉树实验报告.docx
文本预览下载声明
合肥工业大学数据结构二叉树实验代码
李亚鸣
#includeiostream
#includefstream
#includestring
#includestdio.h
#includestdlib.h
#includeWindows.h
#includecmath
using namespace std;
enum errorcode{ success, underflow, overflow };
const int maxlen = 100;
//栈类---------------------
templateclass T
class stack
{
public:
stack();
bool empty();
bool full();
errorcode get_top(T x);
errorcode push(T x);
errorcode pop(){
if (empty()) return underflow;
else count--;
return success;
}
private:
T data[maxlen];
int count;
};
//-------------------------
templateclass T
errorcode stackT::get_top(T x){
if (empty()) return underflow;
else x = data[count - 1];
return success;
}
templateclass T
errorcode stackT::push(T x)
{
if (full()) return overflow;
else data[count++] = x;
return success;
}
templateclass T
stackT::stack()
{
count = 0;
}
templateclass T
bool stackT::empty()
{
return count == 0;
}
templateclass T
bool stackT::full()
{
return count == maxlen;
}
//队列类-------------------
templateclass T
class queue{
public:
queue();
bool empty()const;
bool full()const;
errorcode get_front(T x);
errorcode append(T x);
errorcode serve();
private:
int flag;
int front, count, rear;
T data[maxlen];
};
//-------------------------
templateclass T
queueT::queue()
{
flag = 0;
front = rear = 0;
}
templateclass T
bool queueT::empty()const
{
if (front == rearflag == 0) return true;
else return false;
}
templateclass T
bool queueT::full()const
{
if (front == rearflag == 1) return true;
else return false;
}
templateclass T
errorcode queueT::get_front(T x)
{
if (empty()) return underflow;
x = data[front%maxlen];
return success;
}
templateclass T
errorcode queueT::append(T x)
{
if (full()) return overflow;
data[rear % maxlen] = x;
rear++;
flag = 1;
return success;
}
templateclass T
errorcode queueT::serve()
{
if (empty()) return underflow;
front++;
flag = 0;
return success;
}
//类节点
templateclass T//------------------------
class bnode
{
public:
T data;
bnode *rchild, *lch
显示全部