Tuesday, 7 February 2012

Queues implementaion using Array in c++

Queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.
is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position.

 
/******************************************************************************
               Author: Sunil Kumar
               Website: www.techdoon.com
               Description: Queues implementaion using link list in c++
*******************************************************************************/
#include<iostream>
#include<cstdlib>
using namespace std;
void delet(int*);
void inser(int*);
class queue{
      private: 
               int *a;//array pointer
              int n,p,front, rear;/*n-total size allocated, 
              p-number of items inseted by user*/
      public:
             queue()//constructor to initialize the variables
             {
                  front=0;
                  rear=0;
              }
              ~queue();
             void delet();//for deletion of elements from que
             void inser(); //inserting elements to the que
             void getElemets(); //menu for user and memory allocation
      };
queue::~queue()
{
               delete a;
               }
int main() //program main function
{    
     queue obj;
     obj.getElemets();
    cin.get(); //pause screen
    return 0;
}
void queue::inser() //inserting item to the que
{
     //scan item
     int item,r;
     cout<<"\nITEM: ";
     cin>>item;
     //find rear to be insert
     
      if(front==1&&rear==n)//queuse is full
      {
           cout<<"OVERFLOW";
       }
       else{
     if(front==0) //queuse is empty
     {
                  front=1;
                  rear=1;
                 }
     else if(rear==n&&front!=1)
     {
          rear=1;
      }
      else
      {
          rear++;
      }
      a[rear]=item;
      }
     
   }
void queue::delet()
{
     if(front==0)cout<<"UNDERFLOW.";// queuse is enpty
     else
     {
         cout<<"ITEM="<<a[front];
     if(front==rear)
     {
                    front=0;
                    rear=0;
     }
     else if(front==n)
          {
          front=1;
      }
      else
      {
          front++;
      }
      }
 } 
void queue::getElemets()
{
     int ch; //except choice from menu
    cout<<"Enter number of item to be insert:";
    cin>>p;//number item user wants to enter
    n=p*2; //double size of number od elements
    a=new int(p*2); //arrar memory allocation
    cout<<"\nEnter Items:\n"; 
    for(int i=1;i<=p;i++)cin>>a[i]; //scanning elements from user
           front=1;
           rear=p;
    while(1)//infinite loop
    {
            cout<<"\n1.INSERT.\n2.DELETE.\n0.EXIT.";
            cin>>ch;
            switch(ch)
            {
                      case 1: inser();
                           break;
                      case 2: delet();
                           break;
                      case 0: exit(0);
                      default:
                              cout<<"INVALID";
                    }
           
           }
 }
  
    

Related Posts:



0 comments:

Post a Comment