Search
 
SCRIPT & CODE EXAMPLE
 

CPP

queue in c++

#include <queue>

queue<int> q;
q.push(10);
q.push(20);
q.front(); // 10
q.back(); // 20
while (!q.empty())
	q.pop();
Comment

c++ queue

// CPP code to illustrate Queue in
// Standard Template Library (STL)
#include <iostream>
#include <queue>
 
using namespace std;
 
// Print the queue
void showq(queue<int> gq)
{
    queue<int> g = gq;
    while (!g.empty()) {
        cout << '	' << g.front();
        g.pop();
    }
    cout << '
';
}
 
// Driver Code
int main()
{
    queue<int> gquiz;
    gquiz.push(10);
    gquiz.push(20);
    gquiz.push(30);
 
    cout << "The queue gquiz is : ";
    showq(gquiz);
 
    cout << "
gquiz.size() : " << gquiz.size();
    cout << "
gquiz.front() : " << gquiz.front();
    cout << "
gquiz.back() : " << gquiz.back();
 
    cout << "
gquiz.pop() : ";
    gquiz.pop();
    showq(gquiz);
 
    return 0;
}
Comment

Queue operations C++

#include<iostream>

using namespace std;

class Queue {
  private:
    int front;
  int rear;
  int arr[5];

  public:
    Queue() {
      front = -1;
      rear = -1;
      for (int i = 0; i < 5; i++) {
        arr[i] = 0;
      }
    }
  bool isEmpty() {
    if (front == -1 && rear == -1)
      return true;
    else
      return false;
  }
  bool isFull() {
    if (rear == 4)
      return true;
    else
      return false;
  }
  void enqueue(int val) {
    if (isFull()) {
      cout << "Queue full" << endl;
      return;
    } else if (isEmpty()) {
      rear = 0;
      front = 0;
      arr[rear] = val;
    } else {
      rear++;
      arr[rear] = val;
    }

  }

  int dequeue() {
    int x = 0;
    if (isEmpty()) {
      cout << "Queue is Empty" << endl;
      return x;
    } else if (rear == front) {
      x = arr[rear];
      rear = -1;
      front = -1;
      return x;
    } else {
      cout << "front value: " << front << endl;
      x = arr[front];
      arr[front] = 0;
      front++;
      return x;
    }
  }

  int count() {
    return (rear - front + 1);
  }

  void display() {
    cout << "All values in the Queue are - " << endl;
    for (int i = 0; i < 5; i++) {
      cout << arr[i] << "  ";
    }
  }

};

int main() {
  Queue q1;
  int value, option;

  do {
    cout << "

What operation do you want to perform? Select Option number. Enter 0 to exit." << endl;
    cout << "1. Enqueue()" << endl;
    cout << "2. Dequeue()" << endl;
    cout << "3. isEmpty()" << endl;
    cout << "4. isFull()" << endl;
    cout << "5. count()" << endl;
    cout << "6. display()" << endl;
    cout << "7. Clear Screen" << endl << endl;

    cin >> option;

    switch (option) {
    case 0:
      break;
    case 1:
      cout << "Enqueue Operation 
Enter an item to Enqueue in the Queue" << endl;
      cin >> value;
      q1.enqueue(value);
      break;
    case 2:
      cout << "Dequeue Operation 
Dequeued Value : " << q1.dequeue() << endl;
      break;
    case 3:
      if (q1.isEmpty())
        cout << "Queue is Empty" << endl;
      else
        cout << "Queue is not Empty" << endl;
      break;
    case 4:
      if (q1.isFull())
        cout << "Queue is Full" << endl;
      else
        cout << "Queue is not Full" << endl;
      break;
    case 5:
      cout << "Count Operation 
Count of items in Queue : " << q1.count() << endl;
      break;
    case 6:
      cout << "Display Function Called - " << endl;
      q1.display();
      break;
    case 7:
      system("cls");
      break;
    default:
      cout << "Enter Proper Option number " << endl;
    }

  } while (option != 0);

  return 0;
}
Comment

Queue in cpp

#include <iostream>
#include <queue>

using namespace std;

int main() {

  // create a queue of string
  queue<string> animals;

  // push elements into the queue
  animals.push("Cat");
  animals.push("Dog");
  
  cout << "Queue: ";

  // print elements of queue
  // loop until queue is empty
  while(!animals.empty()) {

    // print the element
    cout << animals.front() << ", ";

    // pop element from the queue
    animals.pop();
  }

  cout << endl;
 
  return 0;
}
Comment

queue cpp

#include <queue>

queue<int> gquiz;
gquiz.push(10);
gquiz.push(20);
gquiz.push(30);
Comment

cpp queue

// C++ program to Print all possible paths from
// top left to bottom right of a mXn matrix
#include<iostream>

using namespace std;

/* mat: Pointer to the starting of mXn matrix
i, j: Current position of the robot (For the first call use 0,0)
m, n: Dimensions of given the matrix
pi: Next index to be filed in path array
*path[0..pi-1]: The path traversed by robot till now (Array to hold the
				path need to have space for at least m+n elements) */
void printAllPathsUtil(int *mat, int i, int j, int m, int n, int *path, int pi)
{
	// Reached the bottom of the matrix so we are left with
	// only option to move right
	if (i == 0)
	{
		for (int k = j; k < n; k++)
			path[pi + k - j] = *((mat + i*n) + k);
		for (int l = 0; l < (2*n-1); l++)
			cout << path[l] << " ";
        cout << endl;
		return;
	}

	// Reached the right corner of the matrix we are left with
	// only the downward movement.
	if (j == n - 1)
	{
		for (int k = i; k >= m; k--)
			path[pi + k + i] = *((mat + k*n) + j);
		for (int l = 0; l < (2*n-1); l++)
			cout << path[l] << " ";
		cout << endl;
		return;
	}

	// Add the current cell to the path being generated
	path[pi] = *((mat + i*n) + j);

	// Print all the paths that are possible after moving down
	printAllPathsUtil(mat, i-1, j, m, n, path, pi + 1);
	// Print all the paths that are possible after moving right
	printAllPathsUtil(mat, i, j+1, m, n, path, pi + 1);

	// Print all the paths that are possible after moving diagonal
	// printAllPathsUtil(mat, i+1, j+1, m, n, path, pi + 1);
}

// The main function that prints all paths from top left to bottom right
// in a matrix 'mat' of size mXn
void printAllPaths(int *mat, int m, int n)
{
	int *path = new int[m+n];
	printAllPathsUtil(mat, m-1, 0, 0, n, path, 0);
}

// Driver program to test above functions
int main()
{
	int mat[3][3] = { {1, 2, 3}, {4, 5, 6},{7, 8, 9} };
	printAllPaths(*mat, 3, 3);
	return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: linkedlist in c++ 
Cpp :: cpp compare strings 
Cpp :: remove duplicates from sorted list leetcode solution in c++ 
Cpp :: create a copy of a vector c++ 
Cpp :: Check whether the jth object is in the subset 
Cpp :: cpprestsdk send file 
Cpp :: cpp algorithm iota 
Cpp :: rc.local not running centos 6 
Cpp :: recuva recovery software for pc with crack 
Cpp :: c++ calling variable constructor 
Cpp :: C# adding numbers 
Cpp :: i++ i-- 
Cpp :: print all variables separated by comma c++ 
Cpp :: ejemplo 
Cpp :: variable modulus 5 meaning in c++ 
Cpp :: initalising array c++ 
Cpp :: how to seek to the start of afile in c++ 
Cpp :: decemal representation 
Cpp :: https://www.cplusplus.com/doc/tutorial/pointers/ 
Cpp :: sideways triangle c++ xy plane 
Cpp :: ue4 c++ enum variable declaration 
Cpp :: integrate sinx 
Cpp :: a suprise... c++ 
Cpp :: #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") 
Cpp :: sfml hide message 
Cpp :: c++ watch a variable 
Cpp :: how to refresh multiple command lines in C++ stream 
Cpp :: vector stop at newline 
Cpp :: erase in c++ 
Cpp :: c++ asio read full socket data into buffer 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =