在DEV-CPP中通过的代码:
#include
#include
using namespace std;
typedef char* ELEM;
class Link
{
public:
ELEM element;
Link* next;
Link(const ELEM& elemval,Link* nextp=NULL)
{element=elemval;next=nextp;}
Link(Link* nextp=NULL)
{next=nextp;}
~Link(){}
};
class Stack
{
private:
Link *top;
public:
Stack(const int sz=1000)
{top=NULL;}
~Stack(){clear();}
void clear();
void push(const ELEM& item)
{top=new Link(item,top);}
ELEM pop();
ELEM topValue() const
{assert(!isEmpty());return top->element;}
bool isEmpty() const
{return top==NULL;}
};
void Stack::clear()
{
while(top!=NULL)
{Link*temp=top;top=top->next;delete temp;}
}
ELEM Stack::pop()
{
assert(!isEmpty());
ELEM temp=top->element;
Link* ltemp=top->next;
delete top; top=ltemp;
return temp;
}
int main()
{
Stack stack;
ELEM aa[5];
aa[0]="QQLove1";
aa[1]="QQLove2";
aa[2]="QQLove3";
aa[3]="QQLove4";
aa[4]="QQLove5";
for(int i=0;i<5;i++)
{
cout<<"压入栈:"< stack.push(aa[i]);
}
cout<<"顺序出栈:"< for(int j=0;j<5;j++)
cout<