Untitled

mail@pastecode.io avatar
unknown
abc
3 years ago
1.2 kB
1
Indexable
Never
include <iostream>

using namespace std;

//Global variables

int front = -1, rear = -1;
const int size = 10;
int Queue[size] = {0};

void enqueue(int item)
{
    if(rear == size - 1) // isFull
        cout << "Queue is full." << endl;
    else if(rear == -1 && front == -1) // isEmpty
    {
        front = 0;
        rear = 0;
        Queue[rear] = item;
    }
    else
    {
        rear++;
        Queue[rear] = item;
    }
}

void dequeue()
{
    if(rear == -1 && front == -1) // isEmpty
        cout << "Queue is empty." << endl;
    else if(rear == front)
    {
        // reset the queue
        front = -1;
        rear = -1;
    }
    else
    {
        Queue[front] = 0;
        front++;
        
    }
}

void print()
{
    if(rear == -1 && front == -1) // isEmpty
        cout << "Queue is empty." << endl;
    else
    {
        for(int i = 0; i < rear + 1; i++)
            cout << Queue[i] << " ";
        cout << "\n---------------------\n";
    }
}

int main() {

    enqueue(13);
    enqueue(25);
    enqueue(63);
    enqueue(14);
    dequeue();
    dequeue();
    enqueue(141);
    enqueue(155);
    enqueue(243);
    
    print();
            
    return 0;            
}