stack.h

来自「实现对输入的算术表达式进行计算,用栈完成此项功能」· C头文件 代码 · 共 81 行

H
81
字号
// Stack.h: interface for the Stack class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STACK_H__BB8389C1_689E_4D28_A65F_A330A28A510C__INCLUDED_)
#define AFX_STACK_H__BB8389C1_689E_4D28_A65F_A330A28A510C__INCLUDED_
#include "iostream.h"
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include<iostream.h>
#include<assert.h>

/*class Stack  
{
public:
	Stack();
	virtual ~Stack();

};*/
template<class Type>
class Stack
{
    private:
		int top;
		Type *elems;
		int Maxsize;
    public:
		Stack(int=10);
		~Stack(){ delete []elems; }
		void Push( Type  );//入栈
		Type Pop();//出栈
		Type Gettop();//取栈顶
		void MakeEmpty() { top=-1; }//置空
		int IsEmpty()  const{ return top==-1;}
		int IsFull()  const { return top==Maxsize-1; }
        friend ostream & operator << (ostream &out,Stack<Type> &ob);
  };
template<class Type>
Stack<Type>::Stack(int s):top(-1),Maxsize(s)
{
    elems=new Type[Maxsize];
    assert(elems!=0);
}
///////////////////////////////////////////////////////////
template<class Type>
void Stack<Type>::Push( Type item)
{
    assert(!IsFull());
    elems[++top]=item;
}
///////////////////////////////////////////////////////////
template<class Type>
Type Stack<Type>::Pop()
{
    assert(!IsEmpty());
    return elems[top--];
}
///////////////////////////////////////////////////////////
template<class Type>
Type Stack<Type>::Gettop()
{
    assert(!IsEmpty());
    return elems[top];
}
///////////////////////////////////////////////////////////
template<class Type>
ostream & operator << (ostream &out,Stack<Type> &ob)
{
    if(ob.IsEmpty())  out<<"NULL";
    for(int i=0;i<=ob.top;i++)
    {
		out<<"  "<<ob.elems[i]<<"  ";
    }
    out<<endl;
	return out;
}
///////////////////////////////////////////////////////////
#endif // !defined(AFX_STACK_H__BB8389C1_689E_4D28_A65F_A330A28A510C__INCLUDED_)

⌨️ 快捷键说明

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