• Recent Uploads

    C PROGRAM TO IMPLEMENT STACK [ C PROGRAM FOR STACK WITH ALGORITHM AND FULL EXPLANATION]



    What is Stack?

    1. It is type of linear data structure.
    2. It follows LIFO (Last In First Out) property.
    3. It has only one pointer TOP that points the last or top most element of Stack.
    4. Insertion and Deletion in stack can only be done from top only.
    5. Insertion in stack is also known as a PUSH operation.
    6. Deletion from stack is also known as POP operation in stack.

    Applications of Stack

    • Conversion ofpolish notations
      There are three types of notations:
      Infix notation - Operator is between the operands : x + y
      Prefix notation - Operator is before the operands : + xy
      Postfix notation - Operator is after the operands : xy +
    • To reverse a string
      A string can be reversed by using stack. The characters of string pushed on to the stack till the end of the string. The characters are popped and displays. Since the end character of string is pushed at the last, it will be printed first.
    • When function  is called
    When a function is called, the function is called last will be completed first. It is the property of stack. There is a memory area, specially reserved for this stack.

    Algorithm for PUSH Operation




    begin STACK(push: stack, data)

       if stack is full
          return null
       endif
       
       top ? top + 1
       stack[top] ? data

    end PUSH

    Algorithm for PUSH Operation


    begin STACK_POP(pop: stack)

       if stack is empty
          return null
       endif
       
       data ? stack[top]
       top ? top - 1
       return data

    end STACK_POP

    C Program to Implement a Stack


    PROGRAM INTRODUCTION

    This program implements the stack operation . here we will implement stack using arrays .
    Problem Solution
    1. Use three functions for three operations like push, pop and display by pressing the respective buttons assign to it.
    2. Use switch statement to access these functions , if you will press the respective key it will automatically call that function at the function will perform the given task.
    3. Exit.
    Source Code
    Here is source code of the C program to implement a stack with the help of the  arrays.
    1. #include<stdio.h>
      int stack[100],choice,n,top,x,i;
      void push(void);
      void pop(void);
      void display(void);
      int main()
      {
          //clrscr();
          top=-1;
          printf("\n Enter the NO OF ELEMENTS YOU WANT IN STACK");
          scanf("%d",&n);
        
          do
          {
             printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
              printf("\n Enter the Choice:");
              scanf("%d",&choice);
              switch(choice)
              {
                  case 1:
                  {
                      push();
                      break;
                  }
                  case 2:
                  {
                      pop();
                      break;
                  }
                  case 3:
                  {
                      display();
                      break;
                  }
                  case 4:
                  {
                      printf("\n\t EXIT ");
                      break;
                  }
                  default:
                  {
                      printf ("\n\t  WRONG INPUT");
                  }
                      
              }
          }
          while(choice!=4);
          return 0;
      }
      void push()
      {
          if(top>=n-1)
          {
              printf("\n\tSTACK is over flow");
              
          }
          else
          {
              printf(" Enter a value to BE INSERT");
              scanf("%d",&x);
              top++;
              stack[top]=x;
          }
      }
      void pop()
      {
          if(top<=-1)
          {
              printf("\n\t Stack is under flow");
          }
          else
          {
              printf("\n\t The DELETED elements is %d",stack[top]);
              top--;
          }
      }
      void display()
      {
          if(top>=0)
          {
              printf("\n The elements in STACK \n");
              for(i=top; i>=0; i--)
                  printf("\n%d",stack[i]);
          }
          else
          {
              printf("\n The STACK is empty");
          }
         
      }
    OUTPUT:-


    Program Explanation shortly..
    here you have the stack program where you can , perform push, pop display as well as the exit this program if you will choose 1 it will call the push function and if  you will choose the 2. option it will can direct the pop . function will delete one element,

    this progaran is running through the codechef ide

    PLZ... Share This Site To Your Contacts.....




    With regards



      Team EveTech