Data structure stack C + + source code implementation

Source: Internet
Author: User

#include <malloc.h>

#include <stdlib.h>

#define STACK_INIT_SIZE 10

#define STACK_INCREMENT_SIZE 10

#define STATUS BOOL

#define OK True

#define ERROR False

typedef int SELEMTYPE;


typedef struct sqstack{

selemtype* top;

selemtype* Base;

int stackSize;

}sqstack,*psqstack;


Class stack{

Public

Stack ();

~stack ();

Status Initstack (Sqstack &s);

Status Destroystack (Sqstack &s);

Status Clearstack (Sqstack &s);

Status stackisempty (Sqstack &s);

Status stacklength (Sqstack &S,int& len);

Status GetTop (Sqstack &S,SElemType& e);

Status push (Sqstack &S,SElemType& e);

Status Pop (Sqstack &S,SElemType& e);

};

Stack::stack () {


}

Stack::~stack () {


}


/*

Initialize stack

Assigning Default size space

*/

Status Stack::initstack (Sqstack &s) {

S.base = (selemtype*) malloc (stack_init_size*sizeof (Selemtype));

if (! S.base)

return ERROR;

S.top = S.base;

S.stacksize = stack_increment_size;

return OK;

}

/*

Destroying stacks

The default allocated space is also recycled

*/

Status Stack::D estroystack (Sqstack &s) {

if (s.base)

Free (s.base);

S.base = S.top = NULL;

return true;

}

/*

Empty stack

The logical emptying

*/

Status Stack::clearstack (Sqstack &s) {

if (! S.base)

return ERROR;

S.top = S.base;

}

/*

Whether the stack is empty

*/

Status stack::stackisempty (Sqstack &s) {

if (s.top = = s.base) return OK;

return ERROR;

}

/*

The length of the stack

*/

Status stack::stacklength (sqstack &S,int& len) {

if (s.top = = s.base) {

len = 0;

return ERROR;

}

Len = (s.top-s.base)% (s.stacksize+1);

return OK;

}

/*

Stack Top Value

*/

Status stack::gettop (Sqstack &S,SElemType& e) {

if (s.top = = s.base) return ERROR;

E = * (s.top-1);

return OK;

}

/*

Press Stack

*/

Status Stack::p ush (Sqstack &S,SElemType& e) {

if (s.top-s.base >= s.stacksize)

{

Need to increase space

S.base = (selemtype*) realloc (S.base, (s.stacksize+stack_increment_size) *sizeof (Selemtype));

if (! S.base) return ERROR;

S.top = s.base+s.stacksize;

S.stacksize+=stack_increment_size;

}

*s.top++ = e;

return OK;

}

/*

Out of the stack

*/

Status Stack::p op (sqstack &S,SElemType& e) {

if (s.top = = s.base) return ERROR;

E=*--s.top;

return OK;

}


/*

Test


*/

int _tmain (int argc, _tchar* argv[])

{

Sqstack sq;

stack* stack = new stack ();


Stack->initstack (SQ);

if (stack->stackisempty (SQ))

printf ("Stack is empty, please push\n");

Selemtype i = 0;

while (I++<30)

Stack->push (Sq,i);

if (stack->stackisempty (SQ))

printf ("stack is not empty \ n");

int Len;

if (Stack->stacklength (Sq,len))

printf ("Stack length =%d\n", Len);

Selemtype e;

if (Stack->gettop (sq,e))

printf ("Stack top element =%d\n", e);

i=0;

while (I++<len)

{

if (Stack->pop (sq,e))

{

printf ("Stack top element%d=%d\n", i,e);

}

}

if (stack->stackisempty (SQ))

printf ("Stack is empty \ n");

Stack->clearstack (SQ);

Stack->destroystack (SQ);

if (NULL = = sq.base)

{

printf ("stack has been destroyed \ n");

}

if (stack)

{

Delete stack;

}

GetChar ();

return 0;

}



Data structure stack C + + source code implementation

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.