Hi friends,
Here i will explain how to create a queue in c++ program.
First let take a short look at queue's concept.Queue is special type of data structure available, which we can say that one step improvement over stack.
Where stack follow's LIFO structure that's Last in First Out type of mechanism in which front side is available for input and output both operations. it is implemented using integer type "top" variable and an array.
For tutorial on stack function click below link to find out about stack operation in c++:
Here we can see From rear value is inserted in the queue and from front side value outputed .
For downloading queue.cpp click here:
queue.cpp
Now let's have a look at code that how queue is implemented
class queue
{
private:
int *data;
int size;
int front,rare;
public:
queue()
{
size = 0;
data = NULL;
front = -1;
rare = -1;
}
queue(int q_size);
void insertq(int);
int deleteq();
void display();
};
As you can see here all function needed for queue simulation like insert,delete,display and creation of queue are declared and all member variable are declared, also default constructor is also defined.
Now let look at all function's definition one by one:
Queue creation:
queue::queue(int q_size)
{
size = q_size;
data = new int[q_size];
front = -1;
rare = -1;
}
Here as you can see argument is passed for size of queue and array of that size is created.
and front and rear are initialized to default value -1.
QUeue Insert:
void queue::insertq(int q_data)
{
if((rare+1)==size)
{
cout<<"Overflow"<<endl;
}
else if(front == -1)
{
front=rare=0;
data[rare]=q_data;
}
else
{
rare++;
data[rare]=q_data;
}
}
Here for insertion condition is checked for if there is overflow and if it is detected then error message is given
else particular value is inserted at rear.
Queue delete element:
int queue::deleteq()
{
if(front == rare+1)
{
cout<<"Underflow"<<endl;
return -1;
}
else
return data[front++];
}
Delete function is quite is to understand here also as in above function condition is checked but in this case checking is for Underflow if detected then -1 value returned indicating queue is empty.
So After writing main function and utilising this queue function output that we get is look like:
For downloading queue.cpp click here:
queue.cpp
So that's it hope you enjoy it!
Visit my site for more tutorials:
Here i will explain how to create a queue in c++ program.
First let take a short look at queue's concept.Queue is special type of data structure available, which we can say that one step improvement over stack.
Where stack follow's LIFO structure that's Last in First Out type of mechanism in which front side is available for input and output both operations. it is implemented using integer type "top" variable and an array.
For tutorial on stack function click below link to find out about stack operation in c++:
Visit my site
Here we talk about queue which follows FIFO kind of mechanism in which front and rear both side are open for output and input respectivly.
QUeue structure look like follow:
| ->Rear | Front-> |
Here we can see From rear value is inserted in the queue and from front side value outputed .
For downloading queue.cpp click here:
queue.cpp
Now let's have a look at code that how queue is implemented
class queue
{
private:
int *data;
int size;
int front,rare;
public:
queue()
{
size = 0;
data = NULL;
front = -1;
rare = -1;
}
queue(int q_size);
void insertq(int);
int deleteq();
void display();
};
As you can see here all function needed for queue simulation like insert,delete,display and creation of queue are declared and all member variable are declared, also default constructor is also defined.
Now let look at all function's definition one by one:
Queue creation:
queue::queue(int q_size)
{
size = q_size;
data = new int[q_size];
front = -1;
rare = -1;
}
Here as you can see argument is passed for size of queue and array of that size is created.
and front and rear are initialized to default value -1.
QUeue Insert:
void queue::insertq(int q_data)
{
if((rare+1)==size)
{
cout<<"Overflow"<<endl;
}
else if(front == -1)
{
front=rare=0;
data[rare]=q_data;
}
else
{
rare++;
data[rare]=q_data;
}
}
Here for insertion condition is checked for if there is overflow and if it is detected then error message is given
else particular value is inserted at rear.
Queue delete element:
int queue::deleteq()
{
if(front == rare+1)
{
cout<<"Underflow"<<endl;
return -1;
}
else
return data[front++];
}
Delete function is quite is to understand here also as in above function condition is checked but in this case checking is for Underflow if detected then -1 value returned indicating queue is empty.
So After writing main function and utilising this queue function output that we get is look like:
For downloading queue.cpp click here:
queue.cpp
So that's it hope you enjoy it!
Visit my site for more tutorials:

No comments:
Post a Comment