Queue Using Template Class
Queue Using Template Class
CONCEPT :
Templates are a feature of the C++ programming language that allows functions and classes to
operate with generic types. This allows a function or class to work on many different data types
without being rewritten for each one.
There are two kinds of templates: function templates and class templates.
A function template behaves like a function except that the template can have arguments of many
different types (see example). In other words, a function template represents a family of
functions.
A class template provides a specification for generating classes based on parameters. Class
templates are commonly used to implement containers or data storage classes. A class template
is instantiated by passing a given set of types to it as template arguments.
PROBLEM STATEMENT :
THEORY :
A queue is a particular kind of 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. This makes the queue
a First-In-First-Out (FIFO) data structure. A queue is an example of a linear data structure.
To incorporate the design, a structure that represents a node in the linked list is at first created. It
contains a data element having a data type that is specified only when a node is actually created;
and a pointer to the next element in the list. It can be shown diagrammatically as follows :
In the class design, the data members of the class are front and rear both pointers of node type.
The first node’s address is stored in front variable and the last is kept in the rear variable. An
insert operation adds nodes to the end of the list. Each node added is marked as rear. A delete
operation removes the first node and marks the following node as front.
CLASS DIAGRAM :
Qtype
Queue
Insert ( QType )
Delete ( ): Qtype
PROGRAM IN C++ :
#include<iostream.h>
#include<conio.h>
#define nil 0
public :
queue()
{
front=NULL;
rear=NULL;
}
Qtype delet()
{
node<Qtype> *t;
Qtype retval;
if(front==NULL)
{
cout<<"Queue Empty";
return(nil);
}
t=front;
front=front->next;
retval=t->data;
delete t;
return(retval);
}
};
void main()
{
clrscr();
queue<int> q1;
queue<float> q2;
char ans;
int x;
float y;
cout<<"QUEUE USING LINKED LIST\n";
cout<<"=======================\n\n\n";
cout<<"QUEUE STORING INTEGERS\n\n";
cout<<"INSERTION IN THE QUEUE"<<endl;
do
{
cout<<"\nEnter number to insert :";
cin>>x;
q1.insert(x);
cout<<"\nWant to insert another ( Y / N ) ?";
ans=getche();
}while(ans=='Y'||ans=='y');
OUTPUT :
DISCUSSIONS
In this program, a template argument Qtype is used to represent the data type of the element
stored in the queue. Whenever an object is created from the class the data type is specified, i.e.
the statements :
queue<int> q1;
queue<float> q2;
creates a queue, q1, that stores integer values and another queue, q2, that stores floats
respectively.
< Include the structure of the queue after all the elements are inserted and after at least one node
is deleted>