C++创建队列和栈的类.docVIP

  • 2
  • 0
  • 约1.8千字
  • 约 3页
  • 2019-10-11 发布于江西
  • 举报
C++栈的类的实现和队列的实现 由于最近面试老是面这么些题,索性自己再写了一遍 下面是栈的类的实现的代码。 typedef char* ELEM; class Stack { private: const int size; int top; ELEM* arraylist; public: Stack(const int sz):size(sz),top(-1){ arraylist = new ELEM[size]; } ~Stack() {delete [] arraylist;} bool isEmpty(){ return (top==-1);} void push(const ELEM item); ELEM pop(); }; void Stack::push(const ELEM item){ assert(top!=size-1);//not full arraylist[++top]=item; } ELEM Stack::pop(){ assert(!isEmpty()); return arraylist[top--]; } 测试的例子: #include assert.h #include stack.h using namespace std; int main(){ Stack sa(5); ELEM ex[5];

文档评论(0)

1亿VIP精品文档

相关文档