Sie sind auf Seite 1von 6

CLASS TEMPLATES

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 :

TO DESIGN A GENERIC CLASS IN C++ TO IMPLEMENT


A QUEUE USING LINKED LIST.

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 :

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.
CLASS DIAGRAM :

Qtype
Queue

Node : front, rear

Insert ( QType )
Delete ( ): Qtype

<< bind >> (integer)


q1

<< bind >> ( float )


q2

PROGRAM IN C++ :

#include<iostream.h>
#include<conio.h>
#define nil 0

template <class Qtype>


struct node
{
Qtype data;
struct node<Qtype> *next;
};

template <class Qtype>


class queue
{
node<Qtype> *front, *rear;

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

void insert(Qtype item)


{
node<Qtype> *t;
t=new node<Qtype>;
t->data=item;
t->next=NULL;
if(front==NULL)
front=t;
else
rear->next=t;
rear=t;
}

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');

cout<<"\n\n\nDELETION FROM THE QUEUE";


do
{
x=q1.delet();
if(x==nil)
break;
cout<<"\nDeleted item :"<<x;
cout<<"\nWant to delete another ( Y / N ) ?";
ans=getche();
}while(ans=='Y'||ans=='y');

cout<<"\n\n\nQUEUE STORING FLOATING POINT NUMBERS\n\n";


cout<<"INSERTION IN THE QUEUE"<<endl;
do
{
cout<<"\nEnter floating point number to insert :";
cin>>y;
q2.insert(y);
cout<<"\nWant to insert another ( Y / N ) ?";
ans=getche();
}while(ans=='Y'||ans=='y');

cout<<"\n\n\nDELETION FROM THE QUEUE";


do
{
y=q2.delet();
if(y==nil)
break;
cout<<"\nDeleted item :"<<y;
cout<<"\nWant to delete another ( Y / N ) ?";
ans=getche();
}while(ans=='Y'||ans=='y');
cout<<"\n\n\t\t\tPress any key to exit....................";
getch();
}

OUTPUT :

QUEUE USING LINKED LIST


=======================

QUEUE STORING INTEGERS


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.76

Want to insert another ( Y / N ) ?y


Enter floating point number to insert :90.87

Want to insert another ( Y / N ) ?n

DELETION FROM THE QUEUE


Deleted item :12.76
Want to delete another ( Y / N ) ?n

Press any key to exit....................

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>

Das könnte Ihnen auch gefallen