#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 << "\n\nWhat 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 \nEnter an item to Enqueue in the Queue" << endl; cin >> value; q1.enqueue(value); break; case 2: cout << "Dequeue Operation \nDequeued 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 \nCount 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; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}