C Foundation--Static and dynamic generation of stacks

Source: Internet
Author: User

MAIN.C file


#include <stdio.h>#include"stack.h"intMainvoid){ Char*str ="ABCDEFGHIJKLMN"; Init_stack (3);//Create a dynamic stack while(*str! =' /') {push (*str); STR++; } while(Is_empty () = =0) Putchar (pop ()); Putchar ('\ n'); Destory_stack (); //destroying dynamic stacks return 0;}

STACK.C file

#include <stdio.h>#include<stdlib.h>/*static char stack[512] = {0};        data stack static int top = 0; stack pointer void push (char ch) {stack[top++] = ch;} Char pop (void) {return stack[--top];} int Is_empty (void) {return top = = 0;} int Is_full (void) {return top = =;}*/Static intSZ = +;Static Char*stack;//Data StacksStatic inttop =0;//stack PointervoidInit_stack (intsize) {    if(Size = =0) Size=sz; Elsesz=size; Stack= (Char*)malloc(SZ);}voidDestory_stack (void){     Free(stack);}voidPushCharch) {    if(top = =SZ) {SZ+=sz; Stack=realloc(Stack, SZ); } stack[top++] =ch;}CharPopvoid){    returnstack[--top];}intIs_empty (void){    returntop = =0;}intIs_full (void){    returntop = =sz;}

Stack.h file

extern voidPushCharch);extern CharPopvoid);extern intIs_empty (void);extern intIs_full (void);extern voidInit_stack (intsize);extern voidDestory_stack (void);

C Foundation--Static and dynamic generation of stacks

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.