HOME

Monday, March 6, 2017

Various operation in Stack !

#include<stdio.h>
#include<conio.h>
#define max 5
void push(int);
int pop(void);
void traverse(void);
   int val,ch,value;
    int stack[max];
    int top=-1;
void main()
{


    while(1)
    {
    printf("\n1.Push data");
    printf("\n2. pop Data");
    printf("\n3. Traverse");
    printf("\n4. Exit");
    printf("\nEnter your choice\n");
    scanf("%d",&ch);
switch(ch)
{
case 1:
    if(top==max-1)
    {
        printf("\Stack overflow");
    }
    else{
    printf("\nEnter Data to Push in stack\n");
    scanf("%d",&value);
    top++;
    push(value);
    }
    break;
case 2:
    if(top==-1)
    {
        printf("\n stack underflow");
    }
    else
        {
    val=pop();
    printf("\n%d is Removed",val);
    top--;
    }
    break;
case 3:
    if(top==-1)
    {
        printf("\nNO Record Found");
    }
    else{
    traverse();}
    break;
case 4:
    exit(1);
    break;
default:
    printf("\nWrong Input\n");
}
    }
}
void push(int value)
{
    stack[top]=value;
}
int pop()
{
    return(stack[top]);
}
void traverse()
{
    int i;
    for(i=top;i>=0;i--)
    {
        printf("\n%d",stack[i]);
    }
}

No comments:

Post a Comment