ex3-1-6g.h

来自「一些学习c++的例题」· C头文件 代码 · 共 60 行

H
60
字号
#include <iostream>

using namespace std;

class CStack
{
private:
   int SP, Depth;
   int *Cells;

public:
   enum OPCODE { OP_OVERFLOW, OP_ISEMPTY, OP_SUCCESSFUL };

   CStack(int d = 100)
   {
      if ( d < 0 || d > 100 ) d = 100;
      Depth = d;
      SP = 0;
      Cells = new int[d];
   }

   ~CStack() { delete []Cells; }

   OPCODE Push(int value)
   {
      if ( SP == Depth )
      {
	 cout << "Stack overflow!\n";
	 return OP_OVERFLOW;
      }

      Cells[SP++] = value;
      return OP_SUCCESSFUL;
   }

   OPCODE Pop(int& value)
   {
      if ( SP == 0 )
      {
	 cout << "Stack is empty!\n";
	 return OP_ISEMPTY;
      }

      value = Cells[--SP];
      return OP_SUCCESSFUL;
   }

   bool IsEmpty(void) { return SP == 0; }

   void Empty(void) { SP = 0; }

   void StackWalk(void)
   {
      if ( !IsEmpty() )
	 for ( int i = 0; i < SP; i++ )
	    cout << "Stack walking: cell[" << i << "]=" << Cells[i] << endl;
      else
	 cout << "Stack is empty now.\n";
   }
};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?