Skip to main content

Queue and its Implementation using C++

Queue:
 A queue is a container that implements the first in first out protocol. That means that the only accessible object in the container is the one among them that was inserted first. A good analogy is a group of people waiting in line of a ticket. The next one admitted is the person in the line who got here ahead of everyone else.  


Common operations of queue on C++ are
bool empty()
Returns True if the queue is empty, and False otherwise.
T & front()
Returns a reference to the value at the front of a non-empty queue. There is also a constant version of this function, const T & front().
void pop()
Removes the item at the front of a non-empty queue.
void push(const T &foo)
Inserts the argument foo at the back of the queue.
size_type size()
Returns the total number of elements in the queue.


Example: Stuck Implementation on C++ 

#include<iostream>

#include<queue>
using namespace std;
template <class T> void print (const queue <T> &);

int main()

{

queue<string>q;

q.push("Jakir");

q.push("Ruble");

q.pop();

q.push("Mehedi");

q.push("Rimon");

q.pop();
}
template<class T> void print (const queue <T>&q)
{
queue<T> qq=q;

cout <<"size="<< qq.size();

if (qq.empty()) cout<< ";

the queue is empty.";

else    {
cout <<";

front=" << qq.front()        << ", back=" << qq.front ();

<< ": (" << qq.front();

qq.pop();

while(!qq.empty())

{
cout << "," << qq.front();

qq.pop();
}        cout <<").";

}

cout <<"\n";

}

Thank you.

Comments

Popular posts from this blog

Object-Oriented Programming and JAVA Programming Language

  Object-Oriented Programming and:   Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, messaging, modularity, olymorphism, and inheritance. Many modern programming languages now support OOP, at least as an option. JAVA Programming Language: Object-oriented programming is at the core of Java. In fact, all Java programs are object-oriented, this isn’t an option the way that it is in C++, for example. OOP is so integral to Java that you must understand its basic principles before you can write even simple Java programs. Therefore, this chapter begins with a discussion of the theoretical aspects of OOP.

Absolute value and Floating-point absolute value function in C Programming Language

Absolute value function in C Programming Language is abs(i), Its return the absolute valu of i. where  i  is any positive or negative  number. It is integer type function. The include file of abs(i) is stslib.h Observe under program: #include<stdio.h> #include<stdlib.h> int main(void) { int a; scanf("%d",&a); printf("%d",abs(a)); } Here I use a integer number. Input it by scanf and print the Absolute value by abs() function. Floating-point Absolute value function in C Programming Language is fabs(i), Its return the absolute valu of i. where  i  is any positive or negative  Floating-point number. It is double integer type function. The include file of fabs(i) is math.h Observe under program: #include<stdio.h> #include<stdlib.h>  int main(void) { float a; scanf("%f",&a); printf("%.2f",fabs(a)); } Here I use a integer number. Input it by scanf and print the Floating-point Absolute value b...

DIFFERENCE BETWEEN IDENTIFIER AND KEYWORD

Identifier keyword Identifiers are names that are given to various program elements, such as variable, function and arrays. C take some reserved word called keyword, they have predefine meaning in C. Identifiers consist of letters and digits. Keyword consist only letter. Identifier’s first character must be a letter. Keyword’s all character is letter. Identifiers Upper and lowercase letter is use. Keywords are all lowercase. Upper and lowercase are not equivalent. Upper and lowercase are also not equivalent. Like: X, sum_5, _weather etc. But 4 th is not identifier cause identifier first character must a letter. Like: auto, short, long etc.