stack_cd.cpp

来自「本代码是《C/C++程序员实用大全》的配套代码。网络转载」· C++ 代码 · 共 63 行

CPP
63
字号
#include <iostream.h>
#define ARR_SIZE 100

class stack 
{
   int stck[ARR_SIZE];
   int stack_top;
 public:
 	stack();
   ~stack();
   void push(int i);
   int pop();
 };

stack::stack(void)
 {
 	stack_top = 0;
   cout << "Stack Initialized" << endl;
 }

stack::~stack(void)
 {
 	cout << "Stack Destroyed" << endl;
 }

void stack::push(int i)
 {
 	if (stack_top==ARR_SIZE)
    {
    	cout << "Stack is full." << endl;
      return;
    }
   stck[stack_top] = i;
   stack_top++;
 }

int stack::pop(void)
 {
 	if (stack_top==0)
    {
    	cout << "Stack underflow." << endl;
      return 0;
    }
   stack_top--;
   return stck[stack_top];
 }

void main(void)
 {
 	stack obj1, obj2;

   obj1.push(1);
   obj2.push(2);
   obj1.push(3);
   obj2.push(4);
   cout << obj1.pop() << endl;
   cout << obj1.pop() << endl;
   cout << obj2.pop() << endl;
   cout << obj2.pop() << endl;
 }
 

⌨️ 快捷键说明

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