queue c++ code code example

Example 1: c++ coding structure

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!" << endl;
  return 0;
}

Example 2: c++ stack and queue

#include <iostream>
#include <stack> 
#include <queue>
using namespace std;

void printStack(stack<int> custstack)
{
    for(int i = 0; i < 3; i++)
    {
        int y = custstack.top();
        cout << y << endl;
        custstack.pop();
    }
}

void printQueue(queue<int> custqueue)
{
    for(int i = 0; i < 3; i++)
    {
        int y = custqueue.front();
        cout << y << endl;
        custqueue.pop();
    }
}

int main ()
{
    cout << "Stack:" << endl;
    // this stack stacks three elements one by one and displays each element before its removal
    stack<int> MY_STACK; // define stack and initialize to 3 elements
    MY_STACK.push(69); // last element popped / displayed
    MY_STACK.push(68); // second element popped / displayed
    MY_STACK.push(67); // third popped/displayed
    printStack(MY_STACK);

    cout << endl << "Switching to queue" << endl;

    queue<int> MY_QUEUE;
    MY_QUEUE.push(69); // first out
    MY_QUEUE.push(68); // second out 
    MY_QUEUE.push(67); // third out
    printQueue(MY_QUEUE);
    return 0;
}

Example 3: queue c++

//Queue is a data structure designed to operate in FIFO(First in First out) context. 
//In queue elements are inserted from rear end and get removed from front end.
 The functions supported by queue:
 ---------------------------------
 empty()   | Tests whether queue is empty or not.
 size()    | Returns the total number of elements present in the queue.
 push()    | Inserts new element at the end of queue.
 emplace() | Constructs and inserts new element at the end of queue.
 pop()     | Removes front element of the queue.
 swap()    | Exchanges the contents of queue with contents of another queue.
 front()   | Returns a reference to the first element of the queue.
 back()    | Returns a reference to the last element of queue.

Tags:

Misc Example