Adsense 728 * 15

Search

Custom Search

Program to Implement Stack Push and Pop Operation Using an Array


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 this post, i'll show how to implement stack push (Insertion) and pop (Deletion) operation. While inserting and deleting an element in a stack we need to check whether memory is available for insertion, if memory is not available then  this state is called stack overflow. Similarly while deleting (pop operation) an element, if no element is present in a stack, then this state is called stack underflow.

Reverse a String using Stack 

Recommended Books for Data Structure

 

 Data Structure Books on Amazon

Data Structure Available on Flipkart


Program to Implement Stack Push and Pop Operation


// Program
 
#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);

    // Deleting value from stack

    pop(stackop);
    pop(stackop);

} 
 
Linked List Questions
 

No comments:

Post a Comment