Stack Program #1

15. Implement a program for stack that performs following operations using array: PUSH, POP, PEEP, CHANGE & DISPLAY

#include<stdio.h>
#define N 500
int top=-1;
int s[N];

//Function prototypes
void push(int x);
int pop();
int peep(int i);
void change(int i,int x);
void display();

void main()
{
    int ch,i,x,a;
    printf ("STACK OPERATION\n");
    while (1)
    {
        printf ("------------------------------------------\n");
        printf ("      1    -->    PUSH               \n");
        printf ("      2    -->    POP               \n");
        printf ("      3    -->    PEEP               \n");
        printf ("      4    -->    CHANGE               \n");
        printf ("      5    -->    DISPLAY               \n");
        printf ("      6    -->    EXIT           \n");
        printf ("------------------------------------------\n");
        printf ("Enter your choice\n");
        scanf    ("%d", &ch);
        switch (ch)
        {
        case 1:
            printf ("Enter the element to be pushed:\n");
            scanf ("%d", &x);
            push(x);
            break;
        case 2:
            a=pop();
            if(a!=0)
            printf("Delete element is %d\n",a);
            break;
        case 3:
              display();   
            printf ("Enter index number of the element witch you want peep from stack:\n");
            scanf ("%d", &i);
            a=peep(i);
            if(a!=0)
            printf("Element is %d\n",a);
            break;
        case 4:
            printf ("Enter the element to be pushed\n");
            scanf ("%d", &x);
            printf ("Enter index number of the element witch you want change from stack:\n");
            scanf ("%d", &i);
            change(i,x);
            break;  
        case 5:
            display();
            break;
        case 6:
            return;
        default :
            printf("Unvalid entered choice, please enter choice between 1 to 6.\n");
        }
     }
}

//Function for push operation
/*  Function to add an element to the stack */
void push(int x)
{
     if(top>=N-1)
     {
         
          printf("STACK OVERFLOW\n");
     }
     else
     {
          top=top+1;
          s[top]=x;
     }
}
//Function for pop operation
/*  Function to delete an element from the stack */
int pop()
{
     if(top==-1)
     {
          printf("STACK UNDERFLOW\n");
          return(0);
     }
     else
     {
          top=top-1;
          return(s[top+1]);
     }
}

//Function for peep operation
/*  Function to delete an element from the stack */
int peep(int i)
{
     if(top-i+1<=-1)
     {
          printf("STACK UNDERFLOW\n");
          return(0);
     }
     else
     {
          return(s[top-i+1]);
     }
}

//Function for change operation
void change(int i,int x)
{
     if(top-i+1<=-1)
     {
          printf("STACK UNDERFLOW\n");
     }
     else
     {
          s[top-i+1]=x;
     }
}

/*  Function to display the status of the stack */
void display()
{
    int j;
    if (top== -1)
    {
        printf ("Stack is empty\n");
    }
    else
    {
        printf ("\n The status of the stack is \n");
        for (j=top;j>=0;j--)
        {
            printf ("%d\n", s[j]);
        }
    }
    printf ("\n");
}

Output

  • STACK OPERATION
    ------------------------------------------
          1    -->    PUSH              
          2    -->    POP              
          3    -->    PEEP              
          4    -->    CHANGE              
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter the element to be pushed:
    12
    ------------------------------------------
          1    -->    PUSH              
          2    -->    POP              
          3    -->    PEEP              
          4    -->    CHANGE              
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter the element to be pushed:
    13
    ------------------------------------------
          1    -->    PUSH              
          2    -->    POP              
          3    -->    PEEP              
          4    -->    CHANGE              
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter the element to be pushed:
    14
    ------------------------------------------
          1    -->    PUSH              
          2    -->    POP              
          3    -->    PEEP              
          4    -->    CHANGE              
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter the element to be pushed:
    15
    ------------------------------------------
          1    -->    PUSH              
          2    -->    POP              
          3    -->    PEEP              
          4    -->    CHANGE              
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter the element to be pushed:
    16
    ------------------------------------------
          1    -->    PUSH              
          2    -->    POP              
          3    -->    PEEP              
          4    -->    CHANGE              
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    5

     The status of the stack is
    16
    15
    14
    13
    12

    ------------------------------------------
          1    -->    PUSH              
          2    -->    POP              
          3    -->    PEEP              
          4    -->    CHANGE              
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    2
    Delete element is 16
    ------------------------------------------
          1    -->    PUSH              
          2    -->    POP              
          3    -->    PEEP              
          4    -->    CHANGE              
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    3

     The status of the stack is
    15
    14
    13
    12

    Enter index number of the element witch you want peep from stack:
    4
    Element is 12
    ------------------------------------------
          1    -->    PUSH              
          2    -->    POP              
          3    -->    PEEP              
          4    -->    CHANGE              
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    4
    Enter the element to be pushed
    34
    Enter index number of the element witch you want change from stack:
    3
    ------------------------------------------
          1    -->    PUSH              
          2    -->    POP              
          3    -->    PEEP              
          4    -->    CHANGE              
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    5

     The status of the stack is
    15
    14
    34
    12

    ------------------------------------------
          1    -->    PUSH              
          2    -->    POP              
          3    -->    PEEP              
          4    -->    CHANGE              
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    6










Note:


1. Ask the user for the operation like push, pop,peep,change,display and exit. Use the variable top to represent the top of the stack.
2. According to the option entered, access its respective function using switch statement.
3. In the function push(), firstly check if the stack is full. If it is, then print the output as “Stack is Full”. Otherwise take the number to be inserted as input and store it in the variable x. Copy the number to the array s[] and increment the variable top by 1.
4. In the function pop(), firstly check if the stack is empty. If it is, then print the output as “Stack is Empty”. Otherwise print the top most element of the array s[] and decrement the variable top by 1.
5. In the function peep(),firstly check if the stack is full. If it is, then print the output as “Stack is Full”. Otherwise display that particular number from index number which you given.
6. In the function change(),firstly check if the stack is full. If it is, then print the output as “Stack is Full”. Otherwise change the number in stack with which you want to change at particular index number.
7. In the function display(), using for loop print all the elements of the array.
8. Exit.

For download this program please check the link ➡➡ Download


Comments

Popular

Basic Program #3

Pointer Program #1

Array Program #4

Singly Linked List program #1

Basic program #2