NAV BAR

Monday 8 August 2016

Working With normal Stack in C

/*All operation in stack are performed in top order
For a stack top element has most priority*/
/*It is last in first out */
/*when you press back button in browser then this is what we call it as stack*/
/*PUSH is for insert
POP is for delete
PEEK is to display*/
/*There is one error when i enter 1 or 0 for repeat it simply goes to end *******************************************ERROR***********   */
#include<stdio.h>
#include<conio.h>
#define MAX 5
int top,num,temp,stack[];
void push();
void pop();
void peek();
void status();
void main()
{
    int choice,n,num,temp;
    int rpt=1;
    int stack[MAX];
    top = -1;
    printf("Enter your choice from given below\n");
    do
    {
        printf("1.Push\n2.Pop\n3.Peek\n4.Current status\n");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:push();
                   break;
            case 2:pop();
                   break;
            case 3:peek();
                   break;
            //case 4:status();
                 //  break;
        }
        printf("do you wish to continue?(Type Y or N)\n");
        scanf("%c",&rpt);
    }
    while(rpt==1);//||rpt=='y');
    getch();
}
void push()
{
    if (top==MAX-1)
    {
        printf("Sorry! Stack is full");
    }
    else
    {
        printf("Enter a number to be inserted\n");
        scanf("%d",&num);
        stack[top++]=num;
        printf("Number %d has been inserted\n",num);
    }
}
void pop()
{
    if(top==-1)
    {
        printf("Stack is empty\n");
    }
    else
    {
        temp = stack[top];
        printf("your number has %d has been removed\n",temp);
    }
}
void peek()
{
    if(top==-1)
    {
        printf("Stack is empty\n");
    }
    else
    {
        printf("Your top most element is %d\n",stack[top]);
    }
}

No comments:

Post a Comment