C Program to Implement Stack Operations push and pop Using Array

Implement Stack Operations push and pop Using Array

C Program has to implement all Stack Operations including push and pop using Array. It is clear that, the stack operations contains 5 options (menu) namely PUSH, POP, DISPLAY, Stack Empty and Stack Full. Through the PUSH operation we can add new data item in the stack. Through the POP operation we can remove the recently (last) stored data item from the stack. Through the DISPLAY operation we can print (display) all the data items from the stack. If there is no data item in the stack, the maessage like "Empty Stack" will be displayed. If there is stack full, the maessage like "the Stack is full" will be displayed. The below given C program will implement all Stack Operations including push and pop using Array. Kindly check out the program to Implement all Stack Operations including push and pop using Array. The problem to  Implement the Stack Operation such as Push and Pop using Array can be asked as, 

1). C Program to Implement Stack Operations Using Array
2). Implementation of Stack Operations in C Using Array
3). C Program for Stack PUSH & POP Implementation using Arrays
4). C Program to Implement Stack Operations using Dynamic Memory
5). Implement the Push and Pop Stack Operation using Array


Program to implement all Stack Operations including push and pop 


    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #define size 10
    struct stack{
                     int stac[size];
                     int tops;
                     }sta;

    int stacfull(){
                     if (sta.tops >= size - 1)
                     return 1;
                     else
                     return 0;
                     }


    void push(int item){
                     sta.tops++;
                     sta.stac[sta.tops] = item;
                     }

    int stacempty(){
                    if (sta.tops == -1)
                    return 1;
                    else
                    return 0;
                    }

    int pop(){
                    int item;
                    item = sta.stac[sta.tops];
                    sta.tops--;
                    return (item);
                    }

    void display(){
                    int a;
                    if(stacempty())
                    printf("\nStack Is Empty!");
                    else {
                    for (a = sta.tops; a >= 0; --a)
                    printf("\n%d", sta.stac[a]);
                    }
                    }

    int main(){
                   int data, selection;
                   char reply;
                   sta.tops = -1;
                   printf("\n   Implementations of Stack Operations using Array");
                   do {
                   printf("\n  Main Menu for Implementations of Stack Operations");
                   printf("\n 1.Push \n 2.Pop \n 3.Display \n 4.exit");
                   printf("\n Give Your Choice : ");
                   scanf("%d", &selection);
                   switch(selection){
                   case 1:  printf("\n Enter a data item to be pushed : ");
                                scanf("%d", &data);
                                if (stacfull())
                                printf("\n The Stack is Full!");
                                else
                                push(data);
                                break;
                   case 2:  if (stacempty())
                                printf("\nEmpty stack!");
                                else {
                                data = pop();
                                printf("\nThe popped element is %d", data);
                                }
                                break;
                   case 3:  display();
                                break;
                   case 4:  exit(0);
                   }
                   printf("\n Do You want To Continue ?   ");
                   reply=getche();
                   }while((reply == 'Y') || (reply == 'y'));
    return 0;
    }


Sample Output:
                 Implementations of Stack Operations using Array
              Main Menu for Implementations of Stack Operations
                    1.Push 

                    2.Pop 

                    3.Display

                     4.exit

                     Give Your Choice :  1

                      Enter a data item to be pushed :  46

                      Do You want To Continue ?    y

                      

      Main Menu for Implementations of Stack Operations
                    1.Push 

                    2.Pop 

                    3.Display

                     4.exit

                     Give Your Choice :  4


Related Popular Posts:

Other Related C Programs :


* C Program to find whether the given number is Odd or Even

* C Program to find the Sum of Odd Digits

* C Program to find the Sum of Even Digits

* C Program to find the Sum of Even and Odd Digits,

* C Program to Display Odd digits

* C Program to Display even digits

* C Program to Display Odd Numbers,

* C Program to Display Even Numbers,

* C Program to Check whether the given number is Armstrong number

* C Program to display the Armstrong numbers in Range

* C Program to display the Armstrong numbers between 1 and 1000

* C Program to check perfect number, perfect numbers in range

* C Program to Check Prime number, Prime numbers between 1 and 100

* C Program to display the Prime numbers in a Range

* C Program to display the Sum of Prime numbers

* C Program to Check Strong number

* C Program to display the Strong number in Range

* C Program to Check whether the given number is polindrome number or not

* C Program to Check polindrome number using for loop

* C Program to Check polindrome number using Recursion

* C Program to  check whether the given string is polindrome or not

* C Program to find the Roots of Quadratic equation

* C Program to display Fibonacci Series

* C Program to display the Fibonacci Series using an array

* C Program to display the Fibonacci Series using while loop

* C Program to find the Sum of Fibonacci Series, Factorial of Number

* C Program to find the Factorial without using Recursion

* C Program to find the Factorial using Pointers

* C Program to find the Factorial using Function

Bookmark and Share