Header file #include <stdio.h>/*==================== stack data structure using arrays to implement ====================*/#define MAX 100#define OK Define error 0typedef struct stack{int Data[max]; int top; stack top int bottom;//stack}stack,*stack;//initialization stack int initstack (stack stack);//Destroy int destroystack (stack stack);//Empty int Clearstack (Stack stack),//stack empty int stackempty (stack stack),//get stack top element int GetTop (stack stack,int* elem);//stack int Push (stack stack,int* elem);//stack int Pop (stack stack,int* elem);//returns the element length int stacklength (stack stack,int* len) in the stack;//the element int in the print stack Printstack (Stack stack);//stack full int stackfull (stack stack);
The main function implements the # include "Stack.h" int main () {int num=10, NUM1, i=0,j=0; Stack stack;int Initflag=initstack (&stack); if (!initflag) return 0;for (num1=0;num1<10;num1++) Push (&stack , &NUM1);/*int Pushflag=push (&stack,&num); if (!pushflag) return 0;*///clearstack (&stack); for (;i< 10;i++) {int Flag=pop (&STACK,&NUM1), if (flag) printf ("%d", NUM1); elseprintf ("Pop error");} int lenflag=stacklength (&STACK,&NUM1); if (Lenflag) printf ("len=%d", NUM1); elseprintf ("Stacklength error"); *int getflag=gettop (&STACK,&NUM1); if (Getflag) printf ("%d", NUM1); */printstack (&stack); return 0;} Initialize stack int initstack (stack stack) {if (stack==null) return error;stack->bottom=stack->top=0;return OK;} stack int Push (stack stack,int* elem) {int flag=stackfull (stack), if (!flag) return error;stack->data[stack->top]=* Elem;++stack->top;return OK;} All elements in the print stack int printstack (stack stack) {int i=0;int flag=stackempty (stack), if (!flag) return error;for (i=0;i<stack- >top;i++) {printf ("%d", StAck->data[i]);} printf ("\ n"); return OK;} stack empty int stackempty (stack stack) {if (Stack==null | | stack->top==stack->bottom) return Error;elsereturn OK; Out stack int Pop (stack stack,int* elem) {int flag=stackempty (stack); if (!flag) return error;*elem=stack->data[stack-> Top-1];--stack->top;return OK;} Stack full int stackfull (stack stack) {if (Stack==null | | stack->top>max) return Error;elsereturn OK; destroys the int destroystack (stack stack) {if (stack==null) return Ok;else{stack->top=stack->bottom;stack=null;return OK;}} empties the int clearstack (stack stack) {int flag=stackempty (stack); if (!flag) return error;else{stack->top=stack-> Bottom;return OK;}} Gets the top element of the stack int GetTop (stack stack,int* elem) {int flag=stackempty (stack); if (!flag) return error;*elem=stack->data[ Stack->top-1];return OK;} Returns the length of the element in the stack int stacklength (stack stack,int* len) {if (stack==null) return error;if (stack->top==stack->bottom) {* Len=0;return OK;} Else{*len=stack->top;return OK;}}
Implementation of the Stack (C language Implementation)