Chinaunix首页 | 论坛 | 博客
  • 博客访问: 88278
  • 博文数量: 34
  • 博客积分: 2000
  • 博客等级: 大尉
  • 技术积分: 395
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-23 10:22
文章分类

全部博文(34)

文章存档

2011年(1)

2010年(4)

2009年(29)

我的朋友

分类: C/C++

2009-05-09 10:16:54

#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) |
0

上一篇:一些面试技巧

下一篇:用链表实现堆栈

给主人留下些什么吧!~~