Home > Back-end >  C stack with array implementation problems run
C stack with array implementation problems run

Time:09-30

Compile no problem but it cannot run, technology small white for a great god!!!!!

#include
#include

The class stack {
Public:
Virtual ~ stack () {
}
Virtual bool empty () const=0;
Virtual int size (const)=0;
Virtual int& Top (const)=0;
Virtual void pop ()=0;
Virtual void push (const int& Theelement)=0;
};
The class arraystack: public stack {
Public:
Arraystack (int initialstack);
~ arraystack ();
Bool empty () const;
Int size () const;
Int& Top () const;
Void the pop ();
Void push (const int&);
Private:
Int stackTop;
Int * stack;
Int arraylength;
};
Void changeLength1D (int * a, int oldlength, int newlength)
{
Int * b=new int [newlength];
STD: : copy (a, a + oldlength, b);
The delete [] a;
A=b;
}
Arraystack: : arraystack (int initialstack)
{
If (initialstack<1)
STD: : cerr<" An empty stack!" Stack=new int [initialstack];
Arraylength=initialstack;
StackTop=1;
}
Arraystack: : ~ arraystack ()
{
The delete [] stack;
}
Bool arraystack: : empty () const
{
Return (stackTop<0);
}
Const int arraystack: : size ()
{
Return (stackTop + 1);
}
Int& Arraystack: : top (const)
{
If (stackTop==1)
STD: : cerr<" An empty stack!" The else
The return stack [stackTop];
}
Void arraystack: : pop ()
{
If (stackTop==1)
STD: : cerr<" An empty stack!" The else
{
Delete the stack;
- stackTop;
- arraylength;
}
}
Void arraystack: : push (const int& Theelement)
{
If (stackTop==arraylength - 1)
{
ChangeLength1D (stack, arraylength, 2 * arraylength);
Arraylength=2 * arraylength;
Stack [+ + stackTop]=theelement;
}
The else
{
Stack [+ + stackTop]=theelement;

}
}
Int main ()
{
Arraystack a1 (10);
for(int i=0; i<19. + + I)
A1. Push (I);
A1. Push (9);
A1. Pop ();
STD: : coutreturn 0;
}
  • Related