Tuesday, 7 February 2012

Queues implementaion using link list in c++

Queue is a list of elements in which insertions are at one end of the list called rear end and the deletions are at the other end of the list called FRONT end.
Insertion is also known as Enqueue,deletion is also known as Dequeue.
  
    /******************************************************************************
Author: Sunil Kumar
Website: www.techdoon.com
Description: Queues implementaion using link list in c++
*******************************************************************************/
#include<iostream>
#include<cstdlib>
#include<memory>
using namespace std;
void menu();
struct node{
       int info;
       struct node *link;
       };
class queue{
      private:
                 struct node *front,*rear;
      public: 
             queue(); //constructor
             void insert(); //inserting element to the que
             void delet(); //removing elements from the que
             int isEmpty(); //Checking for empty
             void display(); //to fatch the data
       };
queue::queue() //constructor initializing variables
{
             front=rear=NULL;
              }
int queue::isEmpty()
{
               if(front==NULL)                                                   
                return 1;
                else
                return 0;
    }

void queue::insert()
{
     struct node *temp;
     int item;
     cout<<"\nITEM: "; cin>>item;
     temp=new node;
     temp->info=item;
     temp->link=NULL;
     if(front==NULL)
     {
                     front=temp;
                     }
                     else
                     {
                         rear->link=temp;
                         }
                         rear=temp;
     cout<<"\nNew item is inserted to the Queue!!!";
 }
 void queue::delet()
 {
      struct node *temp;
      int item,i;
      if((i=isEmpty())==1) cout<<"UNDERFLOW";
      else 
      {
           item=front->info;
           temp=front;
           front=temp->link;
           cout<<"\n "<<item<<"is Deleted.";
       }
  }
 int main()
 {
     menu();
     cin.get();
     return 0;
 }
void menu()
{
     queue obj;
     int ch;
     while(1)
     {
             cout<<"\n1.INSERT.\n2.DELETE.\n3.DISPLAY.\n0.EXIT.";
             cin>>ch;
             switch(ch)
             {
                       case 1: obj.insert();
                            break;
                       case 2: obj.delet();
                            break;
                       case 3:obj.display();
                       case 0:exit(0);
                       default:
                               cout<<"INVALID.";
                     }
             }
 
 }
void queue:: display()
{
    
     if(front==NULL){
                      cout<<"QUEUE is Empty.";
                      menu();
                      }
     else{
     while(front!=NULL){
     cout<<front->info<<"  ";
     front=front->link;
     }
     menu();
     }
 }
  
    

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";
                    }
           
           }
 }