Whatsapp

Stack implementation using C language

Hey cool developers the code is about how you can easily implement stack operations in an array using C language.





#include<stdio.h>
int push_item(int arr[],int max,int* top,int item){if(max==*top){return 0;}else{arr[*top]=item;*top+=1;return 1;}}
int pop_item(int arr[],int max,int* top){if(*top==0){return -1;}else{*top-=1;int pop=arr[*top];return pop;}}
void stack(int arr[],int top){
     int i=0;
     printf("\n[");
     for(i=0;i<top;i++){
                        printf("%d ",arr[i]);
                        printf(",");
                        }
     printf("]\n");

}
int linear_search(int arr[],int item,int* top){
     int i=0;
     for(i;i<*top;i++){
                       if(arr[i]==item){
                                        return i;
                                        }
                       }
                       return -1;
     }
void peek(int arr[],int top){if(top>0){printf("Item at top is : %d",arr[top-1]);}}
int main(){
printf("THIS IS THE CODE CREATED BY ONKAR JHA : )");
int arr[20]={},ch;
int item,max=20,top=0,a,terminate=1,search;
while(terminate){
                 printf("\n<=================================>\n1. Push\n2. Pop\n3. Peek\n4. Linear Search\n5. Reset Stack\n6. Exit : ");
                 scanf("%d",&ch);
                 switch(ch){
                            case (1):
                                 printf("Enter the element you want to push : ");
                                 scanf("%d",&item);
                                 if(push_item(arr,max,&top,item)){
                                                                  printf("\nData inserted successfully !");
                                                                  }
                                 else{
                                      printf("\nOverflow !");
                                      }
                                 stack(arr,top);
                                 break;
                            case (2):
                                 a=pop_item(arr,max,&top);
                                 if(a==-1){
                                      printf("\nUnderflow!");
                                           }
                                 else{
                                       printf("\n%d Popped Successfully !",a);
                                      }
                                  stack(arr,top);
                                  break;
                            case (3):
                                 peek(arr,top);
                                 break;
                            case (4):
                                 printf("\nEnter the item you want to search : ");
                                 scanf("%d",&search);
                                 int res=linear_search(arr,search,&top);
                                 if(res!=-1){
                                             printf("\nFound at index : %d",res);
                                             }
                                 else{
                                      printf("\nNot Found !");
                                      }
                                 break;
                             case (5):
                                  main();
                                  break;
                             case (6):
                                  terminate=0;
                                  break;
                            }
                 }
return 0;
}

Post a Comment

0 Comments

Ad Code

Responsive Advertisement