Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Created April 3, 2019 09:32
Show Gist options
  • Save jatinsharrma/4bdd922c2f17cc5076561dcbfe3a2c43 to your computer and use it in GitHub Desktop.
Save jatinsharrma/4bdd922c2f17cc5076561dcbfe3a2c43 to your computer and use it in GitHub Desktop.
Stack implantation
# include<stdio.h>
# define CAPACITY 5
int stack[CAPACITY], top = -1 ;
//Prototyping
void push(int);
int isFull(void);
void pop(void);
int isEmpty(void);
void peek(void);
void traverse(void);
//----------------------------------------------------------------------------------
void main(){
int option, item;
while(1){
printf("\n 1. Push\n 2. Pop \n 3. Peek \n 4. Traverse \n 5. Quit \n");
printf("Enter your option : ");
scanf("%d",&option);
switch(option){
case 1 : printf("Ente the value you want to push : ");
scanf("%d",&item);
push(item);
break;
case 2 : pop();break;
case 3 : peek();break;
case 4 : traverse();break;
case 5 : exit(0);break;
default : printf("You entered a invalid option\n");
}
}
}
//------------------------------------------------------------------------------
void push(int num){
if(isFull()){
printf("Stack overflow\n");
}
else{
top++;
stack[top] = num;
printf("Pushed : %d\n",num);
}
}
//--------------------------------------------------------------------------------
int isFull(){
if (top == CAPACITY-1){
return 1;
}
else{
return 0;
}
}
//--------------------------------------------------------------------------------
void pop(){
if(isEmpty()){
printf("Stack empty\n");
}
else{
printf("Poped element : %d\n",stack[top]);
top--;
}
}
//--------------------------------------------------------------------------------
int isEmpty(){
if(top == -1){
return 1;
}
else{
return 0;
}
}
//--------------------------------------------------------------------------------
void peek(){
if(isEmpty()){
printf("Stack is empty\n");
}
else{
printf("Top element : %d\n", stack[top]);
}
}
//--------------------------------------------------------------------------------
void traverse(){
if (isEmpty()){
printf("Stack empty\n");
}
else{
printf("Elements of Stack are : \n");
for (int i = 0 ; i<=top ; i++){
printf("%d ",stack[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment