Answer to Question #246398 in C for Ram

Question #246398
Write a program to implement the stack using an array.
Note: The code should have the modularity and should include following function apart from
main ():
 Push() This function inserts an element to top of the Stack.
 Pop() This function deletes an element from top of the Stack.
 Display() This function displays all the elements of the Stack by popping them one by
one.
1
Expert's answer
2021-10-04T07:51:24-0400
#include<stdio.h>
int stack[100];
int opt,q,top,p,j;
void push(void);
void pop(void);
void display(void);
int main()
{
    top=-1;
    printf("\n Enter stack :");
    scanf("%d",&q);
    printf("\n\t Stack using array:");
    printf("\n\t 1.Push\n\t 2.Pop\n\t 3.Display\n\t 4.Exit");
    do
    {
        printf("\n Input the option:");
        scanf("%d",&opt);
        switch(opt)
        {
            case 1:
            {
                push();
                break;
            }
            case 2:
            {
                pop();
                break;
            }
            case 3:
            {
                display();
                break;
            }
            case 4:
            {
                printf("\n\t Exit ");
                break;
            }
            default:
            {
                printf ("\n\t Invalid option");
            }
                
        }
    }
    while(opt!=4);
    return 0;
}
void push()
{
    if(top>=q-1)
    {
        printf("\n\tStack is over flow");
        
    }
    else
    {
        printf(" Enter a value to be pushed:");
        scanf("%d",&p);
        top++;
        stack[top]=p;
    }
}
void pop()
{
    if(top<=-1)
    {
        printf("\n\t Stack is under flow");
    }
    else
    {
        printf("\n\t The popped elements is %d",stack[top]);
        top--;
    }
}
void display()
{
    if(top>=0)
    {
        printf("\n The elements in Stack \n");
        for(j=top; j>=0; j--)
            printf("\n%d",stack[j]);
        printf("\n Press Next Choice");
    }
    else
    {
        printf("\n Stack is empty");
    }
   
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS