Monday, June 20, 2011

Queue's in C++

1. Linear Queue

#include<iostream>
#include<cstdio>
using namespace std;
# define maxsize 10
int front=0,rear=-1;


class queu
{
    private:
       char item[maxsize];

    public:
       void getqueue()
       {
           if(rear>=maxsize)
              {cout<<"\nQueue Full";}
            else
            {
          cout<<"\nEnter Element to queue\n";
          cin>>item[++rear];

            }
       }
       void dequeue()
       {
         if(front>rear)
         {cout<<"\nQueue is empty";}
         else
           {
            item[front++]=' ';
           }
       }
       void showqueue()
       {
         cout<<"\n Queue has following elements\n";
         for(int i=0;i<=rear;i++)
         {cout<<item[i]<<" ";}
       }
};
int main()
{
    queu qu;
    char response;int choice;
    while(response!='n')
    {
        cout<<"\n\nStarting queue operation";
        cout<<"\n\nEnter your choice";
        cout<<"\n\n1.Queue\n2.Dequeue\n3.View elements";
        cin>>choice;
        switch (choice)
          {
            case 1:
                  qu.getqueue();
                  break;

           case 2:
                  qu.dequeue();
                  break;

            case 3:
                  qu.showqueue();
                  break;
            default:
              break;
          }
          cout<<"\nWant to perform more??(y or n)";
          cin>>response;
    }
}



2.Circular Queue

#include<iostream>
#include<cstdio>
using namespace std;
# define maxsize 5
int head=0,tail=0;
class queu
{
    private:
       char item[maxsize];

    public:
       void getqueue()
       {
           if((tail+1)%maxsize==head)
              {cout<<"\nQueue Full";}
            else
            {
                cout<<"\nEnter Element to queue\n";
                cin>>item[tail];
                tail=(tail+1)%maxsize;

            }
       }
       void dequeue()
       {
         if(head==tail)
         {cout<<"\nQueue is empty";}
         else
           {
            item[head]=' ';
            head=(head+1)%maxsize;
           }
       }
       void showqueue()
       {
         cout<<"\n Queue has following elements\n";
         for(int i=0;i<=maxsize;i++)
         {cout<<item[i]<<" ";}
       }
};
int main()
{
    queu qu;
    char response;int choice;
    while(response!='n')
    {
        cout<<"\n\nStarting queue operation";
        cout<<"\n\nEnter your choice";
        cout<<"\n\n1.Queue\n2.Dequeue\n3.View elements";
        cin>>choice;
        switch (choice)
          {
            case 1:
                  qu.getqueue();
                  break;

           case 2:
                  qu.dequeue();
                  break;

            case 3:
                  qu.showqueue();
                  break;
            default:
              break;
          }
          cout<<"\nWant to perform more??(y or n)";
          cin>>response;
    }
}



3.Linear Queue using head pointer fixed and tail pointer  varying

#include<iostream>
#include<cstdio>
using namespace std;
# define maxsize 10
int front=0,rear=-1;
class queu
{
    private:
       char item[maxsize];

    public:
       void getqueue()
       {
           if(rear>=maxsize-1)
              {cout<<"\nQueue Full";}
            else
            {
          cout<<"\nEnter Element to queue\n";
          cin>>item[++rear];

            }
       }
       void dequeue()
       {
         if(rear==-1)
         {cout<<"\nQueue is empty";}
         else
           {
            item[front]=' ';
            for(int i=0;i<=rear;i++)
            {
              item[i]=item[i+1];

            }
            item[rear]=' ';
            rear--;
           }
       }
       void showqueue()
       {
         cout<<"\n Queue has following elements\n";
         for(int i=0;i<rear+1;i++)
         {cout<<item[i]<<" ";}
       }
};
int main()
{
    queu qu;
    char response;int choice;
    while(response!='n')
    {
        cout<<"\n\nStarting queue operation";
        cout<<"\n\nEnter your choice";
        cout<<"\n\n1.Queue\n2.Dequeue\n3.View elements";
        cin>>choice;
        switch (choice)
          {
            case 1:
                  qu.getqueue();
                  break;

           case 2:
                  qu.dequeue();
                  break;

            case 3:
                  qu.showqueue();
                  break;
            default:
              break;
          }
          cout<<"\nWant to perform more??(y or n)";
          cin>>response;
    }
}

No comments:

Post a Comment