Skip to content

Instantly share code, notes, and snippets.

@salayhin
Created December 19, 2015 13:57
Show Gist options
  • Save salayhin/3adb087a6ab0f1d32e83 to your computer and use it in GitHub Desktop.
Save salayhin/3adb087a6ab0f1d32e83 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int choice = 0;
int stackBag[MAX] = {};
int pos = 0;
int value = 0;
// Function for Push an Element to Stack
void addToStack(value){
int i;
if(pos == (MAX - 1)){
printf("\n Stack Overflow\n");
}else{
stackBag[pos] = value;
pos = pos + 1;
}
}
// Function for Pop an Element from Stack
int popToStack(){
int i;
if(pos == 0){
printf("Stack is empty \n");
}else{
pos = pos - 1;
}
}
// Function for Print Elements from Stack
void printStackItems(){
int i;
if(pos == 0){
printf("Stack is empty! \n");
}else{
for(i= 0; i < pos ; i++){
printf("\n %d \n", stackBag[i]);
}
}
}
int main(){
while(1){
printf("1. Push. \n");
printf("2. Pop \n");
printf("3. Print Stack. \n");
printf("4. Exit \n");
printf("Please Enter Your Choice: \n");
printf("------------------------------------");
printf("\n \n");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Push \n");
printf("Please enter your value you want to push: ");
scanf("%d", &value);
addToStack(value);
break;
case 2:
printf("You Choose 2 for Pop \n");
popToStack();
break;
case 3:
printf("You Choose 3 for Print \n");
printStackItems();
break;
case 4:
printf("You Choose 4 For Exit ... Bye Bye! \n");
return 0;
default :
printf("Please Select Option 1, 2 or 3 \n");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment