64,682
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <vector>
using namespace std;
class iStack
{
public:
iStack( int capacity ) : _stack( capacity ), _top( 0 ){}
bool pop( int &value );
bool push( int value );
bool full(){ return _top < _stack.size() - 1 ? false : true; }
bool empty(){ return _top ? false : true; }
void display();
int size(){ return _top; }
private:
int _top;
vector< int > _stack;
};
bool iStack::pop( int &value )
{
if( empty() )
return false;
value = _stack[ --_top ];
return true;
}
bool iStack::push( int value )
{
if( full() )
return false;
_stack[ _top++ ] = value;
return true;
}
void iStack::display()
{
if( !size() )
cout << "stack is empty!" << endl;
for( int i = 0; i < _top; ++i ){
cout << _stack[ i ] << " ";
}
cout << endl;
}
//以下是我在VS2005 C#下做的一个栈类.你可以参考一下.
class mStack
{
private ArrayList m_List = new ArrayList();
private int m_Index = 0;
public void Clear() //置空栈
{ m_List.Clear(); m_Index = 0;}
public bool isEmpty
{
get{ return m_List.Count == 0;}
}
public void Push(string Data)
{
m_Index = m_Index + 1;
m_List.Add(Data);
}
public string Pop()
{
string x;
x = m_List[m_List.Count - 1].ToString();
m_List.Remove(x);
m_Index = m_Index - 1;
return x;
}
public string GetTop
{
get { return m_List[m_List.Count - 1].ToString();}
}
public int Count
{
get { return m_Index; }
}
}