Stack C + + version of data structure

Source: Internet
Author: User

/*

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.