C program to convert an array to stack data structure

PROGRAM:

#include<stdio.h>
int s[5],size=5,top=-1;
void push()
{
    if(top==size-1)
        printf("Push not possible, Stack Full\n");
    else
    {
        top++;
        printf("Enter an element\n");
        scanf("%d",&s[top]);
    }
}
void pop()
{
    if(top==-1)
        printf("Pop not possible, Empty stack\n");
    else
    {
        printf("Deleted element is %d\n",s[top]);
        top--;
    }
}
void disp()
{
    int i;
    if(top==-1)
        printf("Display not possible, Empty stack\n");
    else
    {
        printf("Displaying Stack Contents:\n");
        for(i=top; i>-1; i--)
            printf("%d\n",s[i]);
    }
}
int main()
{
    int choice;
    while(1){
    printf("Enter your choice\n");
    printf("'1' to push\n");
    printf("'2' to pop\n");
    printf("'3' to display stack contents\n");
    printf("'0' to exit\n");
    scanf("%d",&choice);
    switch(choice)
    {
        case 1: push();
                break;
        case 2: pop();
                break;
        case 3: disp();
                break;
        case 0: return 0;
                break;
        default:printf("Invalid Entry\n");
    }
    }
}


OUTPUT:

Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
2
Pop not possible, Empty stack
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
3
Display not possible, Empty stack
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
5
Invalid Entry
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
1
Enter an element
10
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
1
Enter an element
20
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
1
Enter an element
30
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
1
Enter an element
40
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
1
Enter an element
50
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
3
Displaying Stack Contents:
50
40
30
20
10
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
1
Push not possible, Stack Full
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
2
Deleted element is 50
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
2
Deleted element is 40
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
2
Deleted element is 30
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
3
Displaying Stack Contents:
20
10
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
2
Deleted element is 20
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
2
Deleted element is 10
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
2
Pop not possible, Empty stack
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit
3
Display not possible, Empty stack
Enter your choice
'1' to push
'2' to pop
'3' to display stack contents
'0' to exit

Popular Posts