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.


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 : Data Pointer to the next element

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.

}.h> #include<conio. public : queue() { front=NULL. rear Insert ( QType ) Delete ( ): Qtype << bind >> (integer) << bind >> ( float ) q1 q2 PROGRAM IN C++ : #include<iostream.h> #define nil 0 template <class Qtype> struct node { Qtype data.CLASS DIAGRAM : Qtype Queue Node : front. struct node<Qtype> *next. rear=NULL. } void insert(Qtype item) { . template <class Qtype> class queue { node<Qtype> *front. *rear.

node<Qtype> *t. void main() { clrscr(). cout<<"INSERTION IN THE QUEUE"<<endl. t->data=item. cout<<"=======================\n\n\n". if(front==NULL) front=t. return(nil). t->next=NULL. delete t. float y. q1.insert(x). } t=front. retval=t->data. cin>>x. t=new node<Qtype>. int x. } Qtype delet() { node<Qtype> *t. if(front==NULL) { cout<<"Queue Empty". queue<float> q2. queue<int> q1. else rear->next=t. return(retval). cout<<"QUEUE USING LINKED LIST\n". . } }. do { cout<<"\nEnter number to insert :". Qtype retval. front=front->next. cout<<"\nWant to insert another ( Y / N ) ?". rear=t. char ans. cout<<"QUEUE STORING INTEGERS\n\n".

cout<<"\n\n\nQUEUE STORING FLOATING POINT NUMBERS\n\n". cout<<"\nWant to delete another ( Y / N ) ?".. cout<<"INSERTION IN THE QUEUE"<<endl. do { y=q2.. cout<<"\nWant to delete another ( Y / N ) ?".. do { x=q1. do { cout<<"\nEnter floating point number to insert :". ans=getche(). }while(ans=='Y'||ans=='y').. getch().. cout<<"\nDeleted item :"<<x.. }while(ans=='Y'||ans=='y'). cout<<"\nWant to insert another ( Y / N ) ?"... q2.delet().... }while(ans=='Y'||ans=='y'). cout<<"\n\n\t\t\tPress any key to exit. if(y==nil) break. cin>>y... cout<<"\nDeleted item :"<<y. }while(ans=='Y'||ans=='y').. cout<<"\n\n\nDELETION FROM THE QUEUE"... if(x==nil) break..". } OUTPUT : QUEUE USING LINKED LIST ======================= QUEUE STORING INTEGERS .insert(y)..ans=getche(). cout<<"\n\n\nDELETION FROM THE QUEUE".delet().. ans=getche(). ans=getche().

..87 Want to insert another ( Y / N ) ?n DELETION FROM THE QUEUE Deleted item :12............76 Want to insert another ( Y / N ) ?y Enter floating point number to insert :90. DISCUSSIONS ......76 Want to delete another ( Y / N ) ?n Press any key to exit..INSERTION IN THE QUEUE Enter number to insert :12 Want to insert another ( Y / N ) ?y Enter number to insert :23 Want to insert another ( Y / N ) ?y Enter number to insert :90 Want to insert another ( Y / N ) ?n DELETION FROM THE QUEUE Deleted item :12 Want to delete another ( Y / N ) ?y Deleted item :23 Want to delete another ( Y / N ) ?y Deleted item :90 Want to delete another ( Y / N ) ?yQueue Empty QUEUE STORING FLOATING POINT NUMBERS INSERTION IN THE QUEUE Enter floating point number to insert :12.

e. q2. queue<float> q2. the statements : queue<int> q1. i. creates a queue. that stores floats respectively. < Include the structure of the queue after all the elements are inserted and after at least one node is deleted> . Whenever an object is created from the class the data type is specified. q1. that stores integer values and another queue.In this program. a template argument Qtype is used to represent the data type of the element stored in the queue.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.