IT HOME Programming care

IT HOME Programming Care

Monday, January 30, 2017

Push Pop in Data Structure by C Programming

Push Pop in Data Structure by C Programming



//Push Pop in C programming
#include<stdio.h>
void push();
int pop();
int stack[2],top=-1,max=2,empty=-1;
main()
{
    int n;
    while(1)
    {
        printf("\nPress [1] for Inter a value.\nPress [2] for Delete top value.\n");
        scanf("%d",&n);
        //check user want to insert(push) or delete(pop) item
    if(n==1)
        push();
    else if(n==2)
        pop();
    else
        printf("You Enter a Invalid Number\n");
    }
}
//function for insert(push) an item
void push(){
    int item;
    //check stack is full
    if(top==max)
           printf("Stack Overflow.");
    //insert an item
    else{
        top++;
        printf("Enter your Value: ");
        scanf("%d",&item);
        stack[top]=item;
        printf("You Enter: %d\n",stack[top]);
    }
}
//function for delete(pop) an item
int pop(){
    //check whether the stack is empty
    if(top==empty)
        printf("Stack is Underflow.\n");
    else{
        printf("You have Deleted the top value of: %d\n",stack[top]);
        //delete the last inserted item
        --top;
    }
}

0 comments:

Post a Comment

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();

IT HOME Freelancing Care