Created
September 18, 2019 18:22
-
-
Save Ajay-Raj-S/4fe80857cf053798d9bb38c9df96a6d4 to your computer and use it in GitHub Desktop.
Stack using Array C Program.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#define MAX 10 | |
int top = -1; | |
int stack[MAX]; | |
void push(int val) { | |
stack[++top] = val; | |
printf("Value Pushed!\n"); | |
} | |
int pop() { | |
int temp = stack[top]; | |
stack[top--] = 0; | |
return temp; | |
} | |
void display() { | |
int i=0; | |
for(i=0;i<=top;i++) { | |
printf("> %d\n", stack[i]); | |
} | |
} | |
int checkFull() { | |
if(top == MAX-1) { | |
printf("Stack is Full!!\n"); | |
return 0; | |
} | |
return 1; | |
} | |
int checkEmpty() { | |
if(top == -1) { | |
printf("Stack is Empty!!\n"); | |
return 0; | |
} | |
return 1; | |
} | |
int main() { | |
int choice = 0; | |
int value = 0; | |
while(1) { | |
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n >>> "); | |
scanf("%d", &choice); | |
switch(choice) { | |
case 1: | |
if(checkFull()) { | |
printf("Enter the Number to push >> "); | |
scanf("%d", &value); | |
push(value); | |
} | |
break; | |
case 2: | |
if(checkEmpty()) { | |
printf("Popped element is %d\n", pop()); | |
} | |
break; | |
case 3: | |
if(top != -1) { | |
display(); | |
} | |
break; | |
case 4: | |
exit(0); | |
break; | |
default: | |
printf("Wrong Choice!"); | |
exit(0); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment