// constants#define MAX 5 // Max capacity of a stack
#define ERR "\x1B[31m" // Error Color
#define SUCCESS "\x1B[32m" // Success Color
#define RESET "\033[0m" // Reset Color
Constants defined in above lines of code such as ERR,SUCCESS and RESET will be used to color the text of printf(). MAX defines the maximum capacity of the stack
Now we'll declare a stack...
//declaration of stacktypedefstructstack{intentry[MAX];inttop;}stack;
//checking stack is empty or not.intisEmpty(stack*sptr){if(sptr->top==0)return(1);elsereturn(0);}
A function to check if stack is overflowed...
//checking stack is full or not.intisFull(stack*sptr){if(sptr->top==MAX)return(1);elsereturn(0);}
A function to push/insert a new element into the stack...
//Inserting element into stackvoidpush(stack*sptr){intitem;if(isFull(sptr))printf(ERR"\tSTACK IS FULL"RESET);else{printf("\n\tEnter the element: ");scanf("%d",&item);sptr->entry[sptr->top]=item;sptr->top++;}}
A function to pop/remove elements from the stack...
//Deleting element from the stackvoidpop(stack*sptr){intitem;if(isEmpty(sptr))printf(ERR"\tSTACK IS EMPTY"RESET);else{sptr->top--;item=(sptr->entry[sptr->top]);printf(SUCCESS"\t%d HAS BEEN REMOVED"RESET,item);}}
A function to traverse though the stack or print all the elements of the stack
//Traversing the elements of stackvoidtraverse(stack*sptr){if(isEmpty(sptr))printf(ERR"\tSTACK IS EMPTY"RESET);else{inti;for(i=sptr->top-1;i>=0;i--)printf("\t-> %d\n",sptr->entry[i]);}}
Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.
Top comments (2)
You can do this btw
I think you shouldn't do traverse in stack.
Yes, I do agree with you. Thanks for correction!