Saturday, 5 October 2013

Implementing 2 stack in single link list

   We can Implement 2 stack in single array . Take TOP1 at beginning of array. Take TOP2 at end of array.

                                                                 C code :



 // implementing 2 stack in single array;  
 #include<stdio.h>  
 #include<malloc.h>  
 main()  
 {  
      int *stack;  
      int size,k=1,n,m;  
      printf("\n enter the size of arr :");  
      scanf("%d",&size);  
      stack=(int *) malloc(size * sizeof(int)); // creating array dynamicaly  
      int top1=-1,top2=size; //top1=beginning of array.. top2=end of array  
      while(k)  
      {  
           printf("\n \t input ::\n\t 1 for stack1\n\t 2 for stack2\n\t\t::" );  
           scanf("%d",&n);  
           printf("\n input...\n\t 1 for push\n\t 2 for pop \n\t\t ::");  
           scanf("%d",&m);  
           switch(n)  
           {  
                case 1:  
                     if(m==1)  
                      push_1(stack,&top1,top2);  
                     else  
                      pop_1(stack,&top1);  
                      break;  
                case 2:  
                     if(m==1)  
                      push_2(stack,&top2,top1,size);  
                     else  
                      pop_2(stack,&top2,size);  
                      break;  
                default:  
                     printf("\n u enter wrong keyword \n");  
                     break;  
           }  
      }  
 }  
 int push_1(int *stack,int *top1,int top2) //insertion at first stack;   
  {  
       int i;  
       if(*top1==top2-1) //cheacking array is full or not  
        printf("\n overflow condition\n");  
       else  
        *top1=*top1+1;  
        printf("\n inter the no u wan't to insert :");  
        scanf("%d",(stack+*top1));  
        printf("\n data of stack 1 \n");  
        for(i=0;i<=*top1;i++)  
          printf(" %d ",*(stack+i));  
  }  
  int push_2(int *stack,int *top2,int top1,int size) //insertion at 2nd stack;   
  {  
       int i;  
       if(*top2==top1+1) //cheacking array is full or not  
        {   
         printf("\n overflow condition\n");  
        }  
        else  
        *top2=*top2-1;  
        printf("\n enetr no to be inserted in stack 2 :");  
        scanf("%d",(stack+*top2));  
        printf("\n data of stack 2 :\n");  
        for(i=size-1;i>=*top2;i--)  
         printf(" %d  ",*(stack+i));  
  }  
  int pop_2(int *stack,int *top2,int size) //deletion from first stack;   
  {  
       int i;  
       if(*top2==size)  
       {  
            printf("\n undeflow condition \n");  
       }  
       else  
       *top2=*top2+1;  
       printf("\n element of stack 2 \n");  
       for(i=size-1;i>=*top2;i--)  
        printf(" %d  ",*(stack+i));  
  }  
  int pop_1(int *stack,int *top1) //deletion from first stack;  
  {  
       int i;  
       if(*top1==-1)  
       printf("\n underflow condition \n");  
       else  
       *top1=*top1-1;  
       printf("\n data element of stack 1 :");  
        for(i=0;i<=*top1;i++)  
          printf(" %d ",*(stack+i));  
  }  

OUTPUT :-

1 comment:

  1. thanks dude it is very good thanks for it

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets