Stack in C

#include<stdio.h>
#define MAX 4
int mystack[MAX], top=-1;
void push(int value)
{
    if(top>=MAX-1)
    {
        printf("Your stack is full!you can't push");
    }
    else
    {
        mystack[++top]=value;  //push value
        printf("%d\n",value);
    }
}
void pop()
{
    if(top<0)
    {
        printf("Your stack is empty");
    }
    else
    {
        top--;      //decrease top value 1
    }
}
void display()
{
    int i;
    for(i=0;i<MAX;i++)
    {
        printf("\n%d\n",mystack[i]); //print the array of the stack......
    }
}
void peek()
{
    if(top<0)               //check the stack is empty
    {
        printf("Your stack is empty");
    }
    else
    {
        printf("The peek value is %d\n",mystack[++top]); //print the last top data
    }
}
int main()
{
    push(1);
    push(2);
    push(3);
    push(4);
    push(5);
    pop();
    display();
    peek();
    return 0;
}

2 comments:

Theme images by enot-poloskun. Powered by Blogger.