C language implementation of basic data structure (iii) stack

Source: Internet
Author: User

#include <stdio.h>#include<tchar.h>#include<stdlib.h>#defineStackSize 5#defineIncrementsize 5//TODO: Refer to other header files required by the program herestructstack{int*Base; int*top; intstacksize;};//Initialize StackBOOLInit_stack (stack*s) {s-Base= (int*)malloc(stacksize*sizeof(int)); if(s->Base= = NULL)return false; S->top = s->Base; S->stacksize =StackSize; return true;}//Destroying StacksBOOLDestroy_stack (stack*s) {     Free(s->Base); S-Base=NULL; S->top =NULL; S->stacksize =0; return true;}//Empty StackBOOLClear_stack (stack*s) {s->top = s->Base; return true;}//inserting elementsBOOLPush (stack* S,intvalue) {    if(s->top-s->Base>= s->stacksize) {s-Base= (int*)realloc(s->Base, (StackSize + incrementsize) *sizeof(int)); S->top = s->Base+ s->stacksize;//Reset The top of the stack, it doesn't feel very necessary.S->stacksize + =incrementsize; }    *++s->top = value;//stack top points to top of stack elementss->stacksize++; return true;}//Popup ElementsintPop (stack*s) {    if(S->top = = s->Base)return false; intp = *s->top--; S->stacksize--; returnp;}//Reverse PrintvoidPrint_stack (Stack s) {if(S.top = = S.)Base) printf ("this stack is empty \ n");  while(S.top!=s.Base) {printf ("%d->", Pop (&s)); } printf ("\ n");}//Positive Order PrintingvoidStack_traverse (Stack s) {if(S.top = = S.)Base) printf ("this stack is empty \ n");  while(S.top! = S.)Base) {printf ("%d->", *++s.Base); } printf ("\ n");}

C language implementation of basic data structure (iii) stack

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.