/*
The stack itself is a linear data structure, and plainly he and the container linear table is a kind of datatype, don't think of him as tall.
In real time he has no linear table complex, the following simple implementation of the stack.
In fact, the entire core operation is a pointer to the top element of the stack
*/
Template <class t>
Class Basestack {
Out of the stack
virtual bool Pop () = 0;
Into the stack
virtual BOOL Push (T x) = 0;
Get top of stack element
Virtual T Top () const = 0;
Empty stack
virtual void clear () = 0;
};
Template <class t>
Class Seqstack:p ublic basestack<t> {
Public
int _maxsize,_index;//determines the capacity and determines the index of the head pointer
T *_stack;
Seqstack (int maxSize) {
_maxsize = maxSize;
_index =-1;
_stack = new T (maxSize-1);
}
void Clear () {
_index =-1;
}
T Top () {
return _stack[_index];
}
BOOL Push (T x) {
if (Isfull ()) {
printf ("over flow");
return false;
}
else {
_index++;//let the top index of the stack move an element up
_stack[_index] = x;
return true;
}
}
BOOL Pop () {
if (IsEmpty ()) {
return false;
}
_index--;//let the top index of the stack move down one element
return true;
}
Judge is the "queue full"?
BOOL Isfull () {
if (_index < _maxsize) {
return false;
}
else {
return true;
}
}
Judge is the queue empty?
BOOL IsEmpty () {
if (_index = =-1) {
return true;
}
else {
return false;
}
}
};
int main ()
{
Seqstack<int > *test = new seqstack<int> (8);
Test->push (1);
printf ("%d", test->top ());
while (1);
return 0;
}
Stack C + + version of data structure