Tuesday, 18 March 2014

implementation of stack using array .
#include<stdlib.h>
#include<stdio.h>
int push(int);
int display();
int pop(int);
int stack[10];
static int pos=0;
int main()
{
    int a;
    char ch='n';
    do
    {
        printf("1->push\n");
        printf("2->pop\n");
        printf("3->display\n");
        scanf("%d",&a);
        switch(a)
        {
            case 1:
            pos=push(pos);
            break;
            case 2:
            pos=pop(pos);
            break;
            case 3:
            display();
            break;
            default:
            printf("wrong choice");
        }
        printf("\n press any key to continue and press n to quit->");
        scanf("%s",&ch);
    }while (ch!='n');
    return 0;
}

int push(pos)
{
    int limit,i;
    printf("enter how many elements you want to push->");
    scanf("%d",&limit);
    printf("enter %d elements\n",limit);
    for(i=1;i<=limit;i++)
    {
        if(pos<10)
        {
            scanf("%d",&stack[pos]);
            printf(" %d is pushed \n",stack[pos]);
            pos++;
        }
        else
        printf("\n stack overflow...\n");
    }
    return pos;
}
int pop(pos)
{
    int limit,i;
    printf("enter how many elements you want to pop->");
    scanf("%d",&limit);
    for(i=1;i<=limit;i++)
    {
        if(pos>0)
        {
            pos--;
            printf(" %d is pushed \n",stack[pos]);
            stack[pos]=0;
        }
        else
        printf("\n stack underflow...\n");
    }
    return pos;
}
int display()
{
    int i,j=9;
    for(i=9;i>=0;i--)
    {
        printf(" %d\t%d\n",j--,stack[i]);
    }
    return 0;
}




No comments:

Post a Comment