// 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]);}}
We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.
Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.
Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.
A simple “thank you” goes a long way—express your gratitude below in the comments!
Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.
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!