Simple Queue Program Using Class and Member Functions in C++ Programming

Definition

In each of the cases, the customer or object at the front of the line was the first one to enter, while at the end of the line is the last to have entered. Every time a customer finishes paying for their items (or a person steps off the escalator, or the machine part is removed from the assembly line, etc.) that object leaves the queue from the front. This represents the queue dequeue function. Every time another object or customer enters the line to wait, they join the end of the line and represent the enqueue function. The queue size function would return the length of the line, and the empty function would return true only if there was nothing in the line.

Example Program

/* Simple Queue Program Using Class and Memeber Functions in C++*/
/* Data Structure C++ Programs,C++ Array Examples */

#include <iostream>
#include<conio.h>
#include<stdlib.h>

#define MAX_SIZE 100

using namespace std;

class Queue {
private:
    int item, i;
    int arr_queue[MAX_SIZE];
    int rear;
    int front;

public:

    Queue() {
        rear = 0;
        front = 0;
    }

    void insert() {
        if (rear == MAX_SIZE)
            cout << "\n## Queue Reached Max!";
        else {
            cout << "\nEnter The Value to be Insert : ";
            cin>>item;
            cout << "\n## Position : " << rear + 1 << " , Insert Value  : " << item;
            arr_queue[rear++] = item;
        }
    }

    void removeData() {
        if (front == rear)
            cout << "\n## Queue is Empty!";
        else {
            cout << "\n## Position : " << front << " , Remove Value  :" << arr_queue[front];
            front++;
        }
    }

    void display() {
        cout << "\n## Queue Size : " << (rear - front);
        for (i = front; i < rear; i++)
            cout << "\n## Position : " << i << " , Value  : " << arr_queue[i];
    }
};

int main() {
    int choice, exit_p = 1;
    Queue obj;
    cout << "\nSimple Queue Example - Class and Memeber Functions in C++";
    do {
        cout << "\n\n Queue Main Menu";

        cout << "\n1.Insert \n2.Remove \n3.Display \nOthers to exit";
        cout << "\nEnter Your Choice : ";
        cin>>choice;
        switch (choice) {
            case 1:
                obj.insert();
                break;
            case 2:
                obj.removeData();
                break;
            case 3:
                obj.display();
                break;
            default:
                exit_p = 0;
                break;
        }
    } while (exit_p);

    return 0;
}

Sample Output

Simple Queue Example - Class and Memeber Functions in C++

 Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 1

Enter The Value to be Insert : 1

## Position : 1 , Insert Value  : 1

 Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 1

Enter The Value to be Insert : 2

## Position : 2 , Insert Value  : 2

 Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 1

Enter The Value to be Insert : 3

## Position : 3 , Insert Value  : 3

 Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 2

## Position : 0 , Remove Value  :1

 Queue Main Menu
1.Insert
2.Remove
3.Display
Others to exit
Enter Your Choice : 3

## Queue Size : 2
## Position : 1 , Value  : 2
## Position : 2 , Value  : 3