Thursday 21 March 2013

two stack queue c code


#include<stdio.h>
#include<conio.h>
#define MAX 10
int i=0;

void push(int *a,int v)
{
     if(i==MAX-1)
     {
         printf("\nOverflow\n");
     }
     else
     {
         i++;
         a[i]=v;
     }
   
}

int pop(int a[])
{
    int v;
    if(i==0)
            printf("\nUnderflow\n");
    else
    {
        v=a[i];
        i--;
    }
    return v;
}

int popq(int a[],int b[])
{
     int j;
     for(j=0;j<i;j++)
     {
            push(b,pop(a));
     }
     return pop(b);
     for(j=0;j<i;j++)
     {
            push(a,pop(b));
     }
   
}

void display(int a[])
{
     int j;
     for(j=0;j<i;j++)
     {
          printf("%d\n",a[j]);
     }
}

int main()
{
    int a[MAX],b[MAX];
    printf("\n2s Queue\n");
    int choice,v;
    do
    {
        printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\nEnter choice:");
        scanf("%d",&choice);
        switch(choice)
        {
                      case 1:printf("\nenter value:");
                           scanf("%d",&v);                
                           push(a,v);
                           break;
                      case 2:v=popq(a,b);
                           printf("\n%d dequeued",v);
                           break;
                      case 3:display(a);
                           break;
                      case 4:break;
                      default:printf("\nEnter correct option..!");
        }      
    }while(choice!=4);
    getch();
}

No comments:

Post a Comment

Have some problem with this code? or Request the code you want if you cant find it in Blog Archive.