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