Friday, April 29, 2016

Stack (Push & PoP)



stack operations


Stack is abstract data type that serves as a collection of elements , with two principal operation push and pop .



Push operation : The process of inserting an element into stack.In order to insert an element into stack first we have to check weather free space is available in the stack or not.

Pop operation : The process of deleting an element form stack.In order to delete an element from stack  first we have to check weather stack is empty or not.If stack is empty then we can not delete an element.In pop operation we delete topmost element form the stack.


Example : 

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define maxsize 5

int str[maxsize];
int i=0;

void push (){
    if(i<5){
    printf("Please Enter an Integer Number\n");
    scanf("%d",&str[i]);
    i++;
    }
    else
        printf("The Stuck is Full !!\n");
}

void pop() {
    if (i==0){
        printf("The Stack Is Empty!!\n");
    }
    else
        i--;
        printf("%d\n",str[i]);

}
void printall (){
      int j ;
      for(j=i-1;j>=0;j--){
           printf("\t%d\n",str[j]);
          }
}

int main()
{
    int choice;
    do {
      printf("\nPlease Enter Your Choice :\n 1-Push\n 2-Pop\n 3-Display\n");
      scanf("%d",&choice);
      switch( choice){
  case 1 :
      push();
      break;
  case 2 :
      pop();
      break;
  case 3 :
     printall();
     break;
     }
     printf("Do You Want To Do SomeThing Else ? (Y/N)\n");
  }while(getch()=='y');

    return 0;
}



  

Share this

0 Comment to "Stack (Push & PoP)"

Post a Comment