**Implementation of Stack using Array in C Language**

*Stacks are the special data structures where insertion and deletion operations are performed only at specific ends rather than at intermediate or any other random positions. Stacks are special case of ordered lists and are also called controlled linear or linked lists. There is a wide variety of software applications where we need these restricted data structure operations.*

*In our day to day life, we come across many examples of stacks, i.e, a stack of books, a stack of dishes or a stack of chairs. The data structure stack is very similar to these practical examples.*

*Now, consider a stack of books on a table. We can easily put a new book on the top of the stack, and similarly, we can easily remove the topmost book as compared to the books lying in-between or at the bottom position. In the same way, only the topmost element of a stack can be accessed while direct access of other intermediate positions is not feasible. Elements may be added to or removed from only one end, called the top of a stack.*

*There are three basic stack operations which are performed on it are push, pop and getTop.*

*Let us quickly recall all these stack operations:*

*1. push operation: inserts an element on the top of the stack*

*2.*

*pop operation: deletes an element from the top of the stack*

*3. getTop operation: reads an element from the top of the stack*

*4. Empty operation: checks whether the stack is empty*

*5. Full operation: checks whether the stack is full.*

*Applications of Stack:*

*1. Converting infix expression to postfix and prefix expression*

*2. Evaluating the postfix*

*expression*

*3. Checking well- formed parenthesis*

*4. Reversing a string data type*

*5. processing function calls*

*6. Parsing of computer programs*

*7. Simulating recusion*

*8. In computation such as decimal to binary conversion*

*9. In backtracking algorithms*

*Implementation of Stack using Array in C Language:**// included header files and function declaration*

*#include<stdio.h>*

*#define SIZE 10*

*void push(int);*

*void pop();*

*void display();*

*int stack[SIZE],top=-1;*

*// push function definition*

*for stack element insertion*

*void push(int value)*

*{*

*if(top==SIZE-1)*

*printf("\n Stack is full!!!!insertion is not possible");*

*else*

*{*

*top++;*

*stack[top]=value;*

*printf("\n Insertion success!!!");*

*}*

*}*

*// pop function definition for stack element deletion*

*void pop()*

*{*

*if(top==-1)*

*printf("\n Stack is empty!deletion is not possible");*

*else*

*{*

*printf("\n Deleted:%d",stack[top]);*

*top--;*

*}*

*}*

*// display function definition*

*void display()*

*{*

*if(top == -1)*

*printf("\n Stack is empty!!!");*

*else*

*{*

*int i;*

*printf("\n Stack elements are:\n");*

*for(i=top;i>=0;i--)*

*{*

*printf("%d\n",stack[i]);*

*}*

*}*

*}*

*// main function with different stack function calls*

*int main()*

*{*

*int i;*

*for(i=1;i<=5;i++)*

*{*

*push(i);*

*}*

*display();*

*pop();*

*pop();*

*display();*

*}*

*Thanks*

*Mukesh Rajput*
## No comments:

## Post a Comment

Thanks

Mukesh Rajput