作者: jacken00 (~準備暨大資工考~) 看板: C
標題: 利用中續轉後續模擬計算機.....求救中 stack.h標頭檔
時間: Mon Jun 10 00:02:24 2002
#include <iostream.h>
#include <stdlib.h>
class stack {
private:
int top, sz;
int *elem;
public:
stack(int n=10);
~stack() { delete elem; }
bool pop(int &n);
bool push(int n);
int size () { return sz; }
bool empty();
bool full();
bool peek(int idx, int &n);
void print (ostream &os=cout);
} ;
stack::stack(int n=10)
{
top = -1;
sz = n;
elem = new int[sz];
}
bool stack::empty()
{
return (top==-1)? true:false;
}
bool stack::full()
{
return (top==(sz-1))? true:false;
}
bool stack::pop(int &n)
{
if (empty())
{
cout<<"The stack is empty, nothing to pop out !\n" ;
return false;
}
n = elem[top--];
return true;
}
bool stack::push(int n)
{
if(full())
{
cout<<"The stack is full, nothing can be push into !\n";
return false;
}
elem[++top] = n;
return true;
}
bool stack::peek(int idx, int &n)
{
if((idx>=0)&&(idx<sz))
{
n = elem[idx];
return true;
}
else
return false;
}
void stack::print(ostream &os=cout)
{
if(!empty())
for (int i=0;i<=top;i++)
os<<elem[i]<<' ';
}
ostream & operator <<(ostream &os, stack &s)
{
s.print();
return os;
}
int main()
{
stack a;
int n;
cout<<"The stack size is :"<<a.size()<<endl;
a.push(3);
a.push(5);
a.push(9);
cout<<"The stack contains :";
a.print();
cout<<endl;
a.pop(n);
cout<<"The poped element is :";
cout<<n<<endl;
cout<<"The stack contains :";
cout<<a;
cout<<endl;
if(a.pop(n))cout<<n<<" is poped out !\n";
if(a.pop(n))cout<<n<<" is poped out !\n";
if(a.pop(n))cout<<n<<" is poped out !\n";
cout<<a;
system("PAUSE");
return 0;
}
--
寒假沒事何處去~ 朋友家裡來打牌~ 紅中青發加白板~ 槓上開花大三元~
再來一付系狼牌~ 單調東風大四喜~ 別怪我沒跟你說~ 此人恐怖又機掰~
無緣無故青一色~ 梅蘭竹菊四君子~ 生平最愛海底撈~ 三家烤肉一家香~
無聲無息來搶槓~ 再來門清一摸三~ 生死決戰北風北~ 莊家連七殺四方~
--
[1;31mO [1;32mr [1;33mi [1;34mg [1;35mi [1;36mn [1;31m: [1;36m<不良牛牧場> [1;33mzoo.ee.ntu.edu.tw [1;32m(140.112.18.36) [m
[1;32mWelcome to SimFarm BBS [1;36m-- [m [1;31mFrom : [ [madsl-nk-A2379.hitron.net [1;31m] [m