Skip to content

Instantly share code, notes, and snippets.

@milon
Last active April 24, 2018 21:47
Show Gist options
  • Save milon/29cf63227c4863dc1c4ff46787302702 to your computer and use it in GitHub Desktop.
Save milon/29cf63227c4863dc1c4ff46787302702 to your computer and use it in GitHub Desktop.
stack.cpp
//Stack using array
//Author: Milon
#include<stdio.h>
//#include<conio.h>
#define max 50
void push();
void pop();
void display();
int top=0;
int stack[max];
void main(){
int c=0;
char ch;
do{
//clrscr();
printf("This program is for Push & Pop operation.\n");
printf("Enter your choice:\n");
printf("# (P)ush operatino.\n");
printf("# Pop (O)peration.\n");
printf("# (D)isplay stack.\n");
printf("# (E)xit.\n\n");
printf("Enter your choice: ");
ch=getchar();
switch(ch){
case 'P':
case 'p':
push();
break;
case 'O':
case 'o':
pop();
break;
case 'D':
case 'd':
display();
break;
case 'E':
case 'e':
c=1;
break;
default:
printf("\nInvalid input.");
//getch();
break;
}
}while(c!=1);
}
void push(){
int data;
if(top==max){
printf("\nStack is overflow.");
//getch();
return;
}
printf("\nEnter data for Push: ");
scanf("%d",&data);
top=top+1;
stack[top]=data;
printf("\nPush operation complete successfully.");
//getch();
}
void pop(){
if(top==0){
printf("\nStack is underflow.");
//getch();
return;
}
printf("\nData %d is deleted.",stack[top]);
top=top-1;
printf("\nPop operation complete successfully.");
//getch();
}
void display(){
if(top==0){
printf("\nStack is empty.");
//getch();
return;
}
printf("\nStack: ");
for(int i=1;i<=top;i++)
printf("%d -->",stack[i]);
//getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment