Friday, April 22, 2016

Stack in C - using array implementation

 //Stack implementation using arrays  
 #include<stdio.h>  
 #include<conio.h>  
 int max=10;  
 int stack[10];  
 int top=-1;  
 int i;  
 void push(int ele){  
   if(top<max){  
      stack[++top]=ele;  
      printf("\n The element is added to stack %d",ele);  
   }else{  
      printf("\n Stack is overflow");  
   }  
 }  
 void pop(){  
      if(top==-1){  
           printf("\n Stack is underflow");  
      }else{  
           printf("\n Element %d is removed ",stack[top]);  
           top--;  
      }  
 }  
 void display(){  
     if(top==-1){  
           printf("\n Stack is empty");  
     }else{  
       printf("[");  
       for(i=0;i<=top;i++){  
           printf(" %d ",stack[i]);  
           if(i<top)  
           printf(",");  
       }  
       printf("]");  
     }  
 }  
 void main(){  
    int ch,ele;  
    int choice=1;  
    clrscr();  
    while(choice){  
       printf("\n1.PUSH 2.POP 3.DISPLAY\n ");  
       scanf("%d",&ch);  
       switch(ch){  
           case 1:printf("\n Enter the element to push \n");  
               scanf("%d",&ele);  
               push(ele);  
               break;  
           case 2:pop();  
               break;  
           case 3:display();  
               break;  
           default: printf("\n Enter valid number (1 to 3)\n");  
       }  
       printf("\n Do you want to continue (yes = 1 no = 0) \n");  
       scanf("%d",&choice);  
    }  
 }  

No comments:

Post a Comment

Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"...