Advertisement
pkbagchi

stack implementation in c

Nov 27th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3. #define SIZE 10
  4.  
  5. void push(int);
  6. void pop();
  7. void display();
  8.  
  9. int stack[SIZE], top = -1;
  10.  
  11. void main()
  12. {
  13.    int value, choice;
  14.    while(1){
  15.       printf("1. Push\n2. Pop\n3. Display\n4. Exit");
  16.       printf("\nEnter your choice: ");
  17.       scanf("%d",&choice);
  18.       switch(choice){
  19.      case 1: printf("Enter the value to be insert: ");
  20.          scanf("%d",&value);
  21.          push(value);
  22.          break;
  23.      case 2: pop();
  24.          break;
  25.      case 3: display();
  26.          break;
  27.      case 4: exit(0);
  28.      default: printf("\nWrong selection, Try again\n");
  29.       }
  30.    }
  31. }
  32.  
  33. void push(int value){
  34.    if(top == SIZE-1)
  35.       printf("\nStack is Full, Insertion is not possible!!!\n");
  36.    else{
  37.       top++;
  38.       stack[top] = value;
  39.       printf("\nInsertion success!!!\n");
  40.    }
  41. }
  42. void pop(){
  43.    if(top == -1)
  44.       printf("\nStack is Empty, Deletion is not possible!!!\n");
  45.    else{
  46.       printf("\nDeleted : %d", stack[top]);
  47.       top--;
  48.    }
  49. }
  50. void display(){
  51.    if(top == -1)
  52.       printf("\nStack is Empty!\n");
  53.    else{
  54.       int i;
  55.       printf("\nStack elements are:\n");
  56.       for(i=top; i>=0; i--)
  57.      printf("%d\n",stack[i]);
  58.    }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement