C Program to Implement Stack using Array | codeitwise

Here we are going to write a c program to implement stack using array.

Stack is a data structure that stores elements in an ordered manner. Stack follows LIFO rule i.e., Last In First Out rule as the element that was inserted last is the first to be removed.


Stack Representation



Stack Operations - 

We can perform three stack operations namely, push, pop and peek.

• Push operation adds one element to the top of the stack.

• Pop operation removes one element from the top of the stack.

• Peek operation returns the value stored in the top of the stack.

Now lets write c program to implement stack using array i.e., a program to perform push pop and peek operation on a stack.


Stack implementation using array in c -


#include <stdio.h>

#define max 10

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

void push();
void pop();
void peek();
void display();

int main()
{
    int ch;
    do
    {
        printf("----------Main Menu----------\n");
        printf("1. Push operation\n");
        printf("2. Pop operation\n");
        printf("3. Peek operation\n");
        printf("4. Display operation\n");
        printf("5. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            push();
            break;
        case 2:
            pop();
            break;
        case 3:
            peek();
            break;

        case 4:
            display();
            break;
        case 5:
            printf("Exited!");
            break;
            
        default:
            printf("Invalid Input!!!!!\n");
            break;
        }
    } while (ch != 5);

    return 0;
}

void push()
{
    int n;
    printf("Enter number to be pushed: ");
    scanf("%d", &n);
    if(top==max-1)
    {
        printf("Stack Overflow!!!!!\n");
    }
    else{
        top++;
        stack[top]=n;
    }
}

void pop()
{
    if(top==-1)
    {
        printf("Stack Undeflow!!!!!\n");
    }
    else{
        printf("Elememt %d popped!\n",stack[top]);
        top--;
    }
}

void peek()
{
    if(top==-1)
    {
        printf("Empty Stack!!!!!\n");
    }
    else
    {
        printf("%d\n",stack[top]);
    }
}

void display()
{
    if(top==-1)
    {
        printf("Empty Stack!!!!!\n");
    }
    else{
        for(int i=top;i>=0;i--)
        {
            printf("%d\n",stack[i]);
        }
    }
}



Output - 


----------Main Menu----------
1. Push operation   
2. Pop operation    
3. Peek operation   
4. Display operation
5. Exit
Enter your choice: 2
Stack Undeflow!!!!!
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 3
Empty Stack!!!!!
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 4
Empty Stack!!!!!
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 1
Enter number to be pushed: 1
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 4
1
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 1
Enter number to be pushed: 2
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 1
Enter number to be pushed: 3
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 3
3
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 4
3
2
1
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 1
Enter number to be pushed: 4
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 1
Enter number to be pushed: 5
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 1
Enter number to be pushed: 6
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 4
6
5
4
3
2
1
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 2
Elememt 6 popped!
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 4
5
4
3
2
1
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 3
5
----------Main Menu----------
1. Push operation
2. Pop operation
3. Peek operation
4. Display operation
5. Exit
Enter your choice: 5
Exited!



Explanation of Implementation of Stack Using Array Program in C - 


Here top variable is used to store the topmost element of the stack. Variable max is used to store the maximum number of elements that the stack can hold. We have defined the value of max as 10 which can be altered if desired (e.g #define max 5).

top = -1 indicates that stack is empty and top = max-1 indicates that stack is full (remember indexing starts from 0).

We have declared top and stack[max] outside main() function that means these are declared as global variables. 

In functions push(), pop() and peek() we will do insertion of elements in stack, deletion of elements from stack and view the topmost element of the stack respectively. 

We know that program starts executing from main() function so inside that we are creating a menu that will be displayed to the user asking him/her to do operations like push, pop, peek and display according to his/her choice. Then we are asking the user to input his/her choice, storing it in ch variable and passing the value of ch to switch statement and accordingly the functions will get executed.

Now, let us understand what exactly is happening inside each functions.


Push Operation in Stack -

In push() function, we are first checking if top = max-1, as that means stack is full and no more insertion can be done. So if that is the case we are displaying a message stating Stack Overflow!. 

And if the condition is false then we are increasing the value of top and are storing the new element at position given by stack[top].


Pop Operation in Stack - 

In pop() function, we are first checking if top = -1, as that means stack is empty and no more element is there to be removed. So if that is the case we are displaying a message stating Stack Underflow!.

And if the condition is false then we are printing the topmost value stored in stack which indeed is indicating popped value (say top index is 3 then stack[3] = 4 is popped) and are decreasing the value of top. That means if before popping top index was 3 then after popping it has become 2.


Peek Operation in Stack -

In peek() function, we are first checking if top = -1, as that means stack is empty and we are peeking in an empty stack. So if that is the case we are displaying a message stating Empty Stack!. 

And if the condition is false then we are printing the topmost value stored in stack. 


In display() function, we are first checking if top = -1, as that means stack is empty and there is nothing to display. So if that is the case we are displaying a message stating Empty Stack!. 

And if the condition is false, we are displaying the stack at current state.


Post a Comment

If you have any doubts, please let me know

Previous Post Next Post