Skip to content

Instantly share code, notes, and snippets.

@Ajay-Raj-S
Created September 18, 2019 18:22
Show Gist options
  • Save Ajay-Raj-S/4fe80857cf053798d9bb38c9df96a6d4 to your computer and use it in GitHub Desktop.
Save Ajay-Raj-S/4fe80857cf053798d9bb38c9df96a6d4 to your computer and use it in GitHub Desktop.
Stack using Array C Program.
#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