Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<stdbool.h>
- #define CAPACITY 5
- int menu (){
- int opt;
- printf("\n");
- printf(" STACK MENU \n");
- printf(" 0. PUSH \n");
- printf(" 1. POP \n");
- printf(" 2. Full \n");
- printf(" 3. Empty \n");
- printf(" 4. Print Stack \n");
- printf(" 5. Exit \n ");
- printf("\n");
- printf("Enter the option:");
- scanf("%d",&opt);
- return opt;
- }
- int arr[CAPACITY];
- int front =-1;
- void init()
- {
- for(int i=0;i<CAPACITY;i++)
- {
- arr[i]=-1;
- }
- }
- bool isFull()
- {
- if(front==CAPACITY-1)
- return true;
- else
- return false;
- }
- bool isEmpty()
- {
- if(front==-1)
- return true;
- else
- return false;
- }
- void push(int el)
- {
- if(isFull()==true)
- {
- printf("Stack is full\n");
- }
- else
- {
- front++;
- arr[front]=el;
- printf("New element %d was introduced in location %d\n",el,front);
- }
- }
- int pop()
- {
- int del_el;
- if(isEmpty()==true)
- {
- return true;
- }
- else
- {
- del_el=arr[front];
- arr[front]=-1;
- front--;
- }
- return del_el;
- }
- void print_stack()
- {
- for(int i=CAPACITY-1;i>=0;i--)
- {
- if(arr[i]!=-1)
- {
- printf("| %d |",arr[i]);
- }
- else {printf("| -- |");}
- printf("\n");
- printf("|_______| ");
- printf("\n");
- }
- }
- int main()
- {
- int new,option;
- init();
- do{
- option = menu();
- switch(option){
- case 0:
- printf("Enter the element to introduce: ");
- scanf("%d",&new);
- push(new);
- break;
- case 1:
- printf("pop-->%d",pop());
- break;
- case 2:
- if(isFull()==true)
- {
- printf("Stack is full\n");
- }
- else printf("Stacks is not full\n");
- break;
- case 3:
- if(isEmpty()==true)
- {
- printf("Stack is empty\n");
- }
- else printf("Stacks is not empty\n");
- break;
- case 4:
- print_stack();
- break;
- case 5:
- printf("Program terminated\n");
- exit(0);
- break;
- }
- }while(option<=5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement