C Programming – HackerRank Solution | STACKS OPERATIONS |

(HackerRank) Write a Modular C Programming code to solve STACKS – BASIC OPERATIONS, Write a menu driven modular C program to simulate the basic stack operations like push, pop, peek.

Write a menu driven modular C program to simulate the basic stack operations like push, pop, peek.

Menu options

  1. Push
  2. POP
  3. Peek

-1. Exit

Input Format

The input contains the option number followed by an element only in case of push operation.

Constraints

NA

Output Format

For each case display the output in a new line.

Sample Input 0

1 10
1 20
1 30
2
3
2
1 40
3
-1

Sample Output 0

30
20
20
40

Explanation 0

 1 10, push 10 on top of the stack.
 1 20, push 20 on top of the stack.
 1 30, push 30 on top of the stack.
 2 is pop, pop an element from top of stack which is 30 and print it. 
 3 is peek, get top of stack which is 20, peek and print 20.
 2 is pop, pop an element from top of stack 20 and print it
 1 40, push 40 on top of stack
 3 is peek, get top of stack which is 40 and print it.
 -1 is stop. 

Sample Input 1

1 10
1 20
1 30
2
2
2
2
1 40
3
-1

Sample Output 1

30
20
10
stack underflow
40

Explanation 1

1 10, push 10
1 20, push 20
1 30, push 30
2, pop an element from top of stack which is 30 and print it.
2, pop an element from top of stack which is 20 and print it.
2, pop an element from top of stack which is 10 and print it.
2, pop, but the stack is empty therefore print stack underflow
1 40, push 40
3 peek, get the top, which is 40 and print it
-1 stop.

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings | Data Structures | Linked lists | Stacks | Queues | Binary Trees

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define SIZE 100
struct stack
{
    int data[SIZE];
    int top;
};
void push(struct stack *sptr, int num);
void pop(struct stack *sptr);
void display(struct stack *sptr);
void peek(struct stack *sptr);
int main()

{
    struct stack * sptr;
    struct stack s;
    sptr=&s;
    sptr->top=-1;
    int num,ch;
    while(1)
    {
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
            scanf("%d",&num);
            push(sptr,num);
            break;
            case 2:pop(sptr);
            break;
            case 3:peek(sptr);
            printf("\n");
            break;
            case -1:exit(0);
            break;
            default:printf("Enter valid choice\n");
            break;
        }
    }
    return 0;
}
void push(struct stack *sptr, int num)
{
    if(sptr->top==SIZE-1)
    {
        printf("stack Overflow\n");
    }
    else
    {
        sptr->top++;
        sptr->data[sptr->top]=num;
    }
}
void pop(struct stack *sptr)
{
    int num;
    if(sptr->top==-1)
    {
        printf("stack underflow\n");
    }
    else
    {
        num=sptr->data[sptr->top];
        sptr->top--;
        printf("%d \n",num);
    }
    
}

void peek(struct stack *sptr)
{
    if(sptr->top==-1)
    {
        printf("Stack empty\n");
    }
    else
    {
        printf("%d",sptr->data[sptr->top]);
    }
}


OUTPUT

Congratulations, you passed the sample test case.
Click the Submit Code button to run your code against all the test cases.

Input (stdin)

1 10
1 20
1 30
2
3
2
1 40
3
-1
Your Output (stdout)

30 
20
20 
40
Expected Output

30
20
20
40

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!