Untitled

 avatar
unknown
c_cpp
2 years ago
1.6 kB
4
Indexable
#include<stdio.h>
#define MAX_SIZE 10

void pushStack(int);
void popStack();
void printStack();
void peek();

int stack[MAX_SIZE], top = -1;

 // main driver code
void main(){
int value, choice;

do{
printf("\n\n*** MENU ***\n");
printf("1. Push\n2. Pop\n3. Display\n4.Peek\n5.Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
    case 1: printf("Enter the value to be insert: ");
        scanf("%d", &value);
        pushStack(value);
        break;
    case 2: popStack();
        break;
    case 3: printStack();
        break;
    case 4: peek();
        break;
    default: printf("\nInvalid  choice. Enter from 1-4");
        break;
}
} while(choice != 5);

}

// end of driver code

// push function 
void pushStack(int value){
if(top == MAX_SIZE-1){
printf("\nStack is FULL!!!\n");
}
else{
top++;
stack[top]=value;
printf("\nValue insertion is succesfull!");
}
}
// end of push function
// pop function
void popStack(){
if(top == -1){
printf("\nStack is EMPTY!!!\n");
}
else{
printf("\nStack Value is DELETED!!! %d\n", stack[top]);
top--;
}
}
// end of pop function

void printStack(){
if(top == -1){
printf("\nStack is EMPTY!!!\n");
}
else {
for(int i=0;i<=stack[MAX_SIZE]-1;i++){
printf("\nThe stack value is : %d\n", stack[top]);
}
}
}
//end of Stack Display
// function to display top value of stack

void peek(){
if(top == -1){
printf("Stack is EMPTY!!!\n");
}
else {
printf("The top value in the Stack is: %d", stack[top]);
}

}
Editor is loading...