#ifndef _CSTACK_H_
#define _CSTACK_H_
class CStack //堆栈类
{
public:
//一般构造函数、拷贝构造函数、赋值运算符、析构函数
CStack();
CStack(const CStack &rhs);
CStack& operator=(const CStack &rhs);
~CStack();
//入栈
void Push(double element);
//出栈
void Pop(void);
//查看栈顶元素
double Top(void);
//清空堆栈
void Clear(void);
//判断堆栈是否为空
bool Empty(void);
//当前堆栈中的元素个数
int Size(void);
private:
struct Node //堆栈元素结构
{
struct Node *last; //指向前一个堆栈元素的指针
double element; //堆栈元素的值
struct Node *next; //指向后一个堆栈元素的指针
};
struct Node *m_head; //保存指向堆栈首元素的指针
struct Node *m_top; //指向栈顶元素的指针
int m_size; //堆栈中的元素个数
};
#endif
///
//堆栈类
#include <iostream>
#include "CStack.h"
using namespace std;
//一般构造函数
CStack::CStack()
:m_head(NULL), m_top(NULL), m_size(0)
{
m_head = new Node[1];
if (m_head != NULL)
{
m_head->last = NULL;
m_head->element = 0;
m_head->next = NULL;
}
}
//拷贝构造函数
CStack::CStack(const CStack &rhs)
:m_head(NULL), m_top(NULL), m_size(0)
{
m_head = new Node[1];
if (m_head != NULL)
{
m_head->last = NULL;
m_head->element = 0;
m_head->next = NULL;
//定义指针变量
Node *tempNode = NULL;
Node *backNode = NULL;
tempNode = rhs.m_head->next;
backNode = m_head;
while (tempNode != NULL)
{
Node *newNode = NULL; //该指针变量指向新创建的节点
newNode = new Node[1]; //创建新节点
if (newNode == NULL) //创建失败
{
return;
}
backNode->next = newNode;
newNode->last = backNode;
newNode->element = tempNode->element;
newNode->next = NULL;
backNode = newNode;
m_top = newNode; //m_top指向栈顶元素
m_size++; //堆栈的元素个数增1
tempNode = tempNode->next;
} //while (tempNode != NULL)
}
}
//赋值运算符
CStack& CStack::operator=(const CStack &rhs)
{
if (this != &rhs) //检查是否自赋值
{
//定义指针变量
Node *tempNode = NULL;
Node *backNode = NULL;
//删除原有的堆栈元素
if (m_head != NULL)
{
backNode = m_head->next;
while (backNode != NULL)
{
tempNode = backNode->next;
delete [] backNode;
backNode = tempNode;
}
m_top = NULL;
m_size = 0;
m_head->next = NULL;
}
else
{
m_head = new Node[1];
if (m_head == NULL)
{
return*this; //返回本对象的引用
}
m_head->last = NULL;
m_head->element = 0;
m_head->next = NULL;
}
//从rhs对象中复制堆栈元素
tempNode = rhs.m_head->next;
backNode = m_head;
while (tempNode != NULL)
{
Node *newNode = NULL; //该指针变量指向新创建的节点
newNode = new Node[1]; //创建新节点
if (newNode == NULL) //创建失败
{
return*this; //返回本对象的引用
}
backNode->next = newNode;
newNode->last = backNode;
newNode->element = tempNode->element;
newNode->next = NULL;
backNode = newNode;
m_top = newNode; //m_top指向栈顶元素
m_size++; //堆栈的元素个数增1
tempNode = tempNode->next;
} //while (tempNode != NULL)
}
return *this; //返回本对象的引用
}
//析构函数
CStack::~CStack()
{
if (m_head != NULL)
{
//定义指针变量
Node *tempNode = NULL;
Node *backNode = NULL;
//释放堆栈元素占用的内存空间
backNode = m_head;
while (backNode != NULL)
{
tempNode = backNode->next;
delete [] backNode;
backNode = tempNode;
}
}
}
//入栈
void CStack::Push(double element)
{
Node *newNode = NULL;
newNode = new Node[1]; //创建新节点
if (newNode != NULL)
{
if (m_top == NULL) //堆栈中还没有元素
{
m_head->next = newNode;
newNode->last = m_head;
}
else
{
m_top->next = newNode;
newNode->last = m_top;
}
newNode->element = element;
newNode->next = NULL;
m_top = newNode; //newNode为新的栈顶元素
m_size++; //堆栈中的元素个数增1
}
}
//出栈
void CStack::Pop(void)
{
//判断堆栈是否为空
if (m_top != NULL)
{
Node *tempNode = NULL;
tempNode = m_top->last;
delete [] m_top; //删除栈顶元素
m_top = tempNode;
if (m_top == m_head) //堆栈中已没有元素
{
m_top = NULL;
m_head->next = NULL;
}
m_size--; //堆栈中的元素个数减1
}
}
//查看栈顶元素
double CStack::Top(void)
{
if (m_top != NULL)
{
return m_top->element;
}
return 0;
}
//清空堆栈
void CStack::Clear(void)
{
if (m_head != NULL)
{
//定义指针变量
Node *tempNode = NULL;
Node *backNode = NULL;
backNode = m_head->next;
while (backNode != NULL)
{
tempNode = backNode->next;
delete [] backNode;
backNode = tempNode;
}
m_head->next = NULL;
m_top = NULL;
m_size = 0;
}
}
//判断堆栈是否为空
bool CStack::Empty(void)
{
if (0 == m_size)
{
return true; //空
}
return false; //不为空
}
//当前堆栈中的元素个数
int CStack::Size(void)
{
return m_size;
}