#include "stdafx.h"
#include
#include
using namespace std;
/*******************
*使用数组实现堆栈
*******************/
template
class stackType
{
friend ostream& operator<< <>(ostream& os,const stackType& otherStack)
{
assert(otherStack.stackTop>0 && otherStack.stackTop for(int i=0;i os< return os;
}
public:
const stackType& operator=(const stackType&);
//overload the assignment operator
void initStack();
//function to initialize the stack using stackTop=0
bool isEmpty();
//funtion to determine whether the stack is empty
bool isFull();
//funtion to determine whether the stack is full
void destroyStack();
//remove all the elements of the stack
void push(const Type& newItem);
//funtion to add new element to the stack
Type top();
//function to return the top element of the stack
void pop();
//function to remove the top element of the stack
stackType(int stackSize=100);
stackType(const stackType& otherStack);
//copy constructor
~stackType();
//remove all the elements from the stack
private:
int maxSize;
int stackTop; //variable to point the top of the stack
Type *list;
void copyStack(const stackType& otherStack);
//private:only use this funtion to implement the copy constructor and to overload
//the assignment operator
};
/*template
ostream& operator<<(ostream& os,const stackType& otherStack)
{
assert(otherStack.stackTop>0 && otherStack.stackTop for(int i=0;i os< return os;
}*/
//initialize the stack
template
void stackType::initStack ()
{
stackTop=0;
}
//destroy the stack
template
void stackType::destroyStack()
{
stackTop=0;
}
//is the stack empty?
template
bool stackType::isEmpty ()
{
return (0==stackTop);
}
//is the stack full?
template
bool stackType::isFull ()
{
return (stackTop==maxSize);
}
//push element into stack
template
void stackType::push (const Type& newItem)
{
if(!isFull())
{
list[stackTop++]=newItem;
}
else
cerr<<"cannot add to a full stack"<}
//return the top element
template
Type stackType::top ()
{
assert(stackTop>0 && stackTop<=maxSize);
return list[stackTop-1];
}
//pop
template
void stackType::pop ()
{
if(!isEmpty())
{
stackTop--;
}
else
cerr<<"cannot remove from an empty stack"<}
//copy a stack
template
void stackType::copyStack (const stackType& otherStack)
{
delete [] list;//非法:list!=NULL 且list未分配空间 (野指针)
list=NULL;
maxSize=otherStack.maxSize ;
stackTop=otherStack.stackTop ;
list=new Type[stackTop];
assert(list!=NULL);
//copy the elements of the stack
for(int i=0;i list[i]=otherStack.list [i];
}
/*****************************
*constructor & destructor
*****************************/
template
stackType::stackType(int stackSize)
{
if(stackSize<=0)
{
maxSize=100;
}
else
maxSize=stackSize;
stackTop=0;
list=new Type[maxSize];
assert(list!=NULL);
}
template
stackType::~stackType ()
{
delete [] list;
//free the memory occupied by the array
}
//copy constructor
template
stackType::stackType (const stackType& otherStack)
{
list=NULL;//it's not safe to delete [] list if list!=NULL at the beginning of copyStack()
copyStack(otherStack);
}
//overload the assignment operator
template
const stackType& stackType::operator =(const stackType& otherStack)
{
//avoid self-copy
if(this!=&otherStack)
{
copyStack(otherStack);
}
return *this;
}
//program to test varios operations of the stack
int main()
{
stackType st1(50);
stackType tempst;
st1.push (23);
st1.push (45);
st1.push (38);
st1.push (67);
tempst=st1;
cout<<"The top element of tempst:"< cout<
stackType st2(st1);//copy constructor
cout< return 0;
}
运行结果:
阅读(955) | 评论(0) | 转发(0) |