DS – Stack using and Functions

#include<stdio.h>
#define SIZE 5
int stack[SIZE], top=-1;
void push(int);
int pop(void);
int peek(void);
int isFull(void);
int isEmpty(void);
void display(void);
int main()
{
            int choice, ele;
            while(1)
            {
                        printf(“Stack operations : \n”);
                        printf(“1. Push \n”);
                        printf(“2. Pop \n”);
                        printf(“3. Peek \n”);
                        printf(“4. Display \n”);
                        printf(“5. Quit \n”);
                        printf(“Enter your choice : “);
                        scanf(“%d”, &choice);
                        switch(choice)
                        {
                                    case 1  :           printf(“Enter element to push : “);
                                                            scanf(“%d”, &ele);
                                                            push(ele);
                                                            break;
                                                                       
                                    case 2  :           ele = pop();
                                                            if(ele==-1)
                                                                        printf(“Empty Stack \n\n”);
                                                            else
                                                                        printf(“Popped : %d \n\n”, ele);
                                                            break;
                                   
                                    case 3  :           ele = peek();
                                                            if(ele==-1)
                                                                        printf(“Empty Stack \n\n”);
                                                            else
                                                                        printf(“Peek element is : %d \n\n”, ele);
                                                            break;
                                   
                                    case 4  :           display();
                                                            break;
                       
                                    case 5  :           exit(1);
                                    default :           printf(“Invalid choice \n\n”);                            
                        }
            }
            return 0;
}
void push(int ele)
{
            if(isFull())
                        printf(“Stack is Full \n\n”);
            else
            {
                        ++top;
                        stack[top] = ele;
                        printf(“Element pushed on to the Stack \n\n”);
            }
}
int isFull()
{
            if(top==SIZE-1)
                        return 1;
            else
                        return 0;
}
int pop(void)
{
            if(isEmpty())
                        return -1;
            else
            {
                        int ele = stack[top];
                        –top;
                        return ele;
            }
}
int isEmpty()
{
            if(top==-1)
                        return 1;
            else
                        return 0;
}
int peek(void)
{
            if(isEmpty())
                        return -1;
            else
                        return stack[top];
}
void display()
{
            if(isEmpty())
                        printf(“Stack is Empty \n\n”);
            else
            {
                        int i;
                        printf(“Stack elements are : \n”);
                        for(i=top ; i>=0 ; i–)
                        {
                                    printf(“%d \n”, stack[i]);
                        }
            }
}
Scroll to Top