# Implementation of Stack Using Array in C

- Link: https://technotaught.com/implementation-of-stack-using-array-in-c/
- Published: 2018-11-09T23:44:22+05:30
- Author: Ashishkumar Vishwakarma

In this we will learn the Implementation of Stack Using Array in C

### **Stack.**

1. What is a stack? 
ANS.: An ordered collection of elements using the LIFO(Last
in First Out) mechanism.

2. Operations performed on the stack? 
ANS.: Push (insertion), pop (deletion), peek(
Topmost element) & display.

3. Applications of the stack? 
ANS.: Infix, Postfix, and Prefix notations, Recursive
functions, Function cell, Expression evaluation etc

### Graphical Representation.

![Implementation of Stack Using Array in C](https://technotaught.com/wp-content/
uploads/2018/11/stack-1.jpg)

## Implementation of Stack Using Array in C

##### Source code.

    ```c
    #include<conio.h>
    #include<stdio.h>
    #define SIZE 4

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

    int empty();
    int full();
    void display();
    void pop();
    void push(int);

    int main()
    {

            printf("push elements\n");
    	push(10);//10
    	push(20);//10 20
    	push(30);//10 20 30
            push(40);// 10 20 30 40
    	push(15);//error full
            printf("pop elements.\n ");
    	pop();//10 20 30
    	pop();//10 20
    	pop();// 10
    	pop();//error empty
            getch();
    }

    int empty()
    {
    	if(top==-1)
    		return 1;
    	else
    		return 0;
    }

    int full()
    {
    	if(top==SIZE-1)
    		return 1;
    	else
    		return 0;
    }

    void display()
    {
    	int i;
    	printf("Stack:");
    	for(i=0; i<=top; i++)
            printf("%d ",stack[i]);
    	printf("\n");
    }

    void push(int val)
    {
    	if(full())
    	{
    		printf("Error : 
                    Stack full\n");
    	}
    	else
    	{
    		stack[++top]=val;
    		display();
    	}
    }

    void pop()
    {
    	int i,loc;
    	if(empty())
    	{
    	      printf("Error : 
                 Stack empty\n");
    	}
    	else
    	{    top--;
                 display();
    	}
    }
    ```

Also, you can use the C [compiler](https://www.onlinegdb.com/online_c_compiler) 
to compile a program.

### Output.

> push stack.
> Stack: 10 Stack: 10 20 Stack: 10 20 3O Stack: 10 20 3O 40 Error : 
> Stack full pop stack.Stack: 10 20 3O Stack: 10 20 Stack: 10 Stack: 

Also View:– [Queue using linked list.](https://technotaught.com/queue-using-linked-list/)

### Share this:

 * [  X ](https://technotaught.com/implementation-of-stack-using-array-in-c/?share=twitter)
 * [  Facebook ](https://technotaught.com/implementation-of-stack-using-array-in-c/?share=facebook)
 * [More](https://technotaught.com/implementation-of-stack-using-array-in-c/#)

 * [  WhatsApp ](https://technotaught.com/implementation-of-stack-using-array-in-c/?share=jetpack-whatsapp)
 * [  LinkedIn ](https://technotaught.com/implementation-of-stack-using-array-in-c/?share=linkedin)
 * [  Email ](https://technotaught.com/implementation-of-stack-using-array-in-c/?subject=%5BShared%20Post%5D%20Implementation%20of%20Stack%20Using%20Array%20in%20C&body=https%3A%2F%2Ftechnotaught.com%2Fimplementation-of-stack-using-array-in-c%2F&share=email)
 * [  Reddit ](https://technotaught.com/implementation-of-stack-using-array-in-c/?share=reddit)
 * [  Tumblr ](https://technotaught.com/implementation-of-stack-using-array-in-c/?share=tumblr)
 * [  Pinterest ](https://technotaught.com/implementation-of-stack-using-array-in-c/?share=pinterest)
 * [  Telegram ](https://technotaught.com/implementation-of-stack-using-array-in-c/?share=telegram)
 * [  Print ](https://technotaught.com/implementation-of-stack-using-array-in-c/#print?share=print)

### _Related_

## Comments

### By: Anessa B

- Link: https://technotaught.com/implementation-of-stack-using-array-in-c/#comment-146
- Published: 2020-04-27T00:28:45+05:30

helpful post
