Code Scrappers


Stack Using Array C Program


                    #include<stdio.h>
                    #include<stdlib.h>
                    #define SIZE 10

                    int stack[SIZE];

                    int top = -1, i, ele;

                    void main()

                    {
                        int choice;

                        printf("stack implementation\n");

                        while(1)
                        {

                            printf("1. push \n 2. pop \n 3. display \n 4. exit \n");

                            printf("enter your choice : ");

                            scanf("%d",&choice);

                            switch(choice)

                            {
                                case 1: push();

                                break;

                                case 2: pop();

                                break;

                                case 3: display();

                                break;

                                case 4: exit(0);

                                break;

                                default: printf("\nInvalid Choice\n");

                              }
                        }
                    }

                    void push()

                    {
                        if(top >= SIZE - 1)

                        {
                            printf("stack is full\n");

                        }

                        else

                        {
                            printf("enter the element to be pushed : ");

                            scanf("%d",&ele);

                            top++;

                            stack[top] = ele;
                        }

                    }

                    void pop()

                    {
                        if(top <= -1)

                        {
                            printf("stack is underflow \n");

                        }

                        else

                        {
                            printf("The popped element is %d\n",stack[top]);

                            top--;
                        }

                    }

                    void display()

                    {
                        if(top <= -1)

                        {
                            printf("stack is empty\n");

                        }

                        else

                        {
                            for(i = top; i >= 0; i--)

                            printf("\n %d\n",stack[i]);

                        }

                    }


                



Output

              stack implementation
               1. push
               2. pop
               3. display
               4. exit
              enter your choice : 1

              enter the element to be pushed : 10
               1. push
               2. pop
               3. display
               4. exit

              enter your choice : 1

              enter the element to be pushed : 20
               1. push
               2. pop
               3. display
               4. exit

              enter your choice : 1

              enter the element to be pushed : 30
               1. push
               2. pop
               3. display
               4. exit

              enter your choice : 1

              enter the element to be pushed : 40
               1. push
               2. pop
               3. display
               4. exit

                enter your choice : 3

                40

                30

                20

                10

                1. push
                2. pop
                3. display
                4. exit

                enter your choice : 2

                The popped element is 40

                1. push
                2. pop
                3. display
                4. exit

                enter your choice : 2

                The popped element is 30

                1. push
                2. pop
                3. display
                4. exit

                enter your choice : 3

                20

                10

                1. push
                2. pop
                3. display
                4. exit