//Header file:linkedStackType.h 链堆栈头文件
#ifndef H_linkedStackType
#define H_linkedStackType
#include
using namespace std;
template
struct nodeType
{
Type info;
nodeType *link;
};
template
class linkedStackType
{
public:
const linkedStackType& operator=(const linkedStackType&);
void initializeStack();
bool isEmptyStack();
bool isFullStack();
void push(const Type& newItem);
void pop(Type& poppedElement);
void destroyStack();
linkedStackType();
linkedStackType(const linkedStackType& otherstack);
~linkedStackType();
private:
nodeType *top;
};
template
linkedStackType::linkedStackType()
{
top=NULL;
}
template
void linkedStackType::destroyStack()
{
nodeType *temp;
while(top!=NULL)
{
temp=top;
top=top->link;
delete temp;
}
}
template
void linkedStackType::initializeStack()
{
destroyStack();
}
template
bool linkedStackType::isEmptyStack()
{
return(top==NULL);
}
template
bool linkedStackType::isFullStack()
{
return false;
}
template
void linkedStackType::push(const Type& newElement)
{
nodeType *newNode;
newNode=new nodeType;
newNode->info=newElement;
newNode->link=top;
top=newNode;
}
template
void linkedStackType::pop(Type& poppedElement)
{
nodeType *temp;
poppedElement=top->info;
temp=top;
top=top->link;
delete temp;
}
template
linkedStackType::linkedStackType(const linkedStackType& otherStack)
{
nodeType *newNode,*current,*last;
if(otherStack.top==NULL)
top=NULL;
else
{
current=otherStack.top;
top=new nodeType;
top->info=current->info;
top->link=NULL;
last=top;
current=current->link;
while(current!=NULL)
{
newNode=new nodeType;
newNode->info=current->info;
newNode->link=NULL;
last->link=newNode;
last=newNode;
current=current->link;
}
}
}
template
linkedStackType::~linkedStackType()
{
nodeType *temp;
while(top!=NULL)
{
temp=top;
top=top->link;
delete temp;
}
}
template
const linkedStackType& linkedStackType::operator=(const linkedStackType& otherStack)
{
nodeType *newNode,*current,*last;
if(this!=&otherStack)
{
if(top!=NULL)
destroyStack();
if(otherStack.top==NULL)
top=NULL;
else
{
current=otherStack.top;
top=new nodeType;
top->info=current->info;
top->link=NULL;
last=top;
current=current->link;
while(current!=NULL)
{
newNode=new nodeType;
newNode->info=current->info;
newNode->link=NULL;
last->link=newNode;
last=newNode;
current=current->link;
}
}
}
return *this;
}
#endif
|