Home > Back-end >  For c stack
For c stack

Time:09-21

The standard output the following
Now, intStack is full.
9 September 9th September 9th September 9th September 9 9
9 8 7 6 5 4 3 2 1 0
Now, intStack is empty.
There are five elements in stringStack.
Now, there are no elements in stringStack

My question code below
# include
using namespace std;


TemplateThe class Stack {
Private:
T array [SIZE];//array, used to hold the stack elements
int top;//stack location (array subscript)
Public:
The Stack ();//the constructor to initialize the stack
Void push (const T & amp; );//element into the stack
T pop ();//top element out stack
Void the clear ();//will stack empty
Const T & amp; Top () const;//access stack element
Bool empty () const;//test whether the stack is empty
Bool full () const;//test whether stack full
Int size ();//returns the current number of elements in the stack
};

Template
Stack : : Stack () {
Top=0;

}

Template
Void Stack : : push (const T & amp; A)
{
Top=top + 1;
Array [top]=a;
}

Template
T Stack : : pop ()
{
T T=array (top),
Top=top - 1;
Coutreturn t;
}

Template
Void Stack : : clear ()
{
The delete [] array.
}

Template
Const T & amp; Stack : : Top (const)
{
T T=array (top),
return t;
}

Template
Bool Stack : : empty () const
{
If (top==0) return true;
else return false;
}

Template
Bool Stack : : full (const)
{
If (top==SIZE) return true;
else return false;
}

Template
Int Stack : : size ()
{
Return the top;
}
Int main ()
{
Stackfor(int i=0; I<10; I++)
IntStack. Push (I);
Cout< if (intStack. Full ()); & lt;" Now, intStack is full. "& lt; for(int i=0; I<10; I++)
CoutCoutfor(int i=0; I<10; I++)
CoutCoutIf (intStack. Empty ())
Cout<& lt;" Now, intStack is empty. "& lt;
StackStringStack. Push (" One ");
StringStack. Push (" Two ");
StringStack. Push (" Three ");
StringStack. Push (" Four ");
StringStack. Push (" Five ");
Cout<& lt;" There are "& lt; StringStack. The clear ();
If (stringStack. Empty ())
Cout<& lt;" Now, there are no elements in stringStack "& lt;
return 0;
}
  • Related