Tuesday, October 26, 2010

Queue Template

The queue template I made. Overflow conditions have been skipped as they practically dont occur on modern PC's.

Please specify class T when you declare the object, for example for 'int',
queue type_name > queue_object_name;

template class T >
class queue
{

   struct node{
   T data;
   struct node *next;
   }*frnt,*rear;

public:
queue()
{
                  frnt=rear=NULL;
}

void insert(const T & value)
{
   struct node *ptr;
   ptr=new node;
   ptr->data=value;
   ptr->next=NULL;
   if(frnt==NULL)
                   frnt=ptr;
   else
                   rear->next=ptr;
   rear=ptr;
   cout<<"\nNew item is inserted to the Queue!!!";
   getch();
}

T del()
{
   if(frnt==NULL)
   {
                   cout<<"\nQueue is empty!!";
                   getch();
                   return;
   }
   struct node *temp;
   temp=frnt;
   frnt=frnt->next;
   T t=temp->data;
   delete temp;
   return t;
}

void show()
{
   struct node *ptr1=frnt;
   if(frnt==NULL)
   {
                   cout<<"The Queue is empty!!";
                   getch();
                   return;
   }
   cout<<"\nThe Queue is\n";
   while(ptr1!=NULL)
   {
                    cout<data<<" ->";
                    ptr1=ptr1->next;
   }
}
};