Adsense 728 * 15

Search

Custom Search

C Program to Implement a Stack Using an Array

Write a C program to implement a stack using an array. In this tutorial, We'll implement a stack data structure using an array.

What is a Stack Data Structure?

Stack is a data structure in which insertion and deletion can be done from one end only. That's why it is also called LIFO(Last In First Out).

In stack, Insertion and deletion operation is known as push (Insertion) and pop (Deletion). While inserting and deleting an element in a stack we need to check some conditions. For insertion, we need to check whether a memory is available, if memory is not available then it is known as stack overflow. Similarly, while deleting (pop operation) an element, if no element is present in a stack, then it is known as stack underflow.

Reverse a String using Stack

C Program to Implement a Stack using an Array


#include <stdio.h>

#define MAX 50
int top=-1;

void push(int stackop[],int item){
     
    if(top==MAX-1){
        printf("Stack Overflow");
        return;
    }else{
        // Insert the element in a stack
        stackop[++top]=item;
        printf("Value inserted in stack is %d\n",item);
    }
}

void pop(int stackop[]){

    int val;

    if(top==-1){
        printf("Stack underflow");
        return;
    }else{
        val = stackop[top];
        --top;
    }

    printf( "Value deleted from stack is %d\n",val);
}

main()
{
    int stackop[MAX];

    // Inserting value in stack

    push(stackop,5);
    push(stackop,7);
    push(stackop,2);
    // Deleting value from stack

    pop(stackop);
    pop(stackop);

} 

C Program to Implement a Stack using an Array

No comments:

Post a Comment