C++ Queue | C++ Static Queue | C++ Queue Top 12 Method | C++ Queue Dry Run

 
C++ Queue Static
C++ Queue Static

C++ Queue (Static)

Queue is data structure in which first will be serve first and last at last mean first entry will remove first and last will at the end.

Queue has different method which are,

1-      isEmpty()

Indicate whether queue is empty or not.

2-      isFull()

Indicate whether queue is full or not. This method only use in static queue not in dynamic.

3-      enQueue()

This method used to store value in queue.

4-      deQueue()

This method use to remove value from queue.

5-      print()

This method used to display queue value.

6-      getFront()

This method used to get front or first value of queue.

7-      getRear()

This method used to get rear or top of the value of queue.

8-      getMin()

This method used to get minimum value from queue. Its only used in numeric queue.

9-      getMax()

This method used to get maximum value from queue. Its only used in numeric queue.

10-  sum()

This method used to get sum of queue. Its only used in numeric queue.

11-  avg()

This method used to get average of queue. Its only used in numeric queue.

12-  length()

This method used to get total number of data in queue.

Dry Run of static Queue

IsEmpty() Method

isEmpty
isEmpty

IsFull() Method

isFull
isFull

 

enQueue() Method

enQueue
enQueue


enQueue
enQueue
enQueue
enQueue
enQueue
enQueue
enQueue
enQueue
enQueue
enQueue

enQueue
enQueue

deQueue() Method

deQueue
deQueue
deQueue
deQueue

deQueue
deQueue
deQueue
deQueue
deQueue
deQueue
deQueue
deQueue
deQueue
deQueue

Getfront() Method

getFront
getFront

getFront
getFront

getFront
getFront

getFront
getFront

getFront
getFront

getFront
getFront

getFront
getFront

getRear() Method

getRear
getRear

getRear
getRear

getRear
getRear

getRear
getRear

getRear
getRear

getRear
getRear

getRear
getRear


code

Queue.h

#pragma once

#include <iostream>

typedef int dataType;

#define CAPACITY 5

using namespace std;

class Queue

{

private:

     int front, rear;

     int queue[CAPACITY] = {};

public:

     Queue();

     dataType getFront();

     dataType getRear();

     dataType getMin();

     dataType getMax();

     dataType sum();

     int length();

     double avg();

     bool isEmpty();

     bool isFull();

     void enQueue(dataType value);

     void deQueue();

     void print();

};

Queue.cpp

#include "queue.h";

Queue :: Queue()

{

     front = rear = -1;

}

bool Queue::isEmpty()

{

     return (rear == front) ? true : false;

}

bool Queue::isFull()

{

     return (rear == CAPACITY-1) ? true : false;

}

dataType Queue::getFront()

{

     return (isEmpty()) ? NULL : queue[front+1];

}

dataType Queue::getRear()

{

     return (isEmpty()) ? NULL : queue[rear];

}

void Queue:: enQueue(dataType value)

{

     if (!isFull())

     {

           queue[++rear] = value;

     }

     else

           cout << "Queue is full\n";

}

void Queue::deQueue()

{

     if (!isEmpty())

           front++;

     else

           cout << "Queue is empty\n";

}

dataType Queue::getMax()

{

     int value = getFront();

     for (int index = front+1; index <= rear; index++)

     {

           if (value <= queue[index])

                value = queue[index];

     }

     return value;

}

dataType Queue::getMin()

{

     int value = getFront();

     for (int index = front+1; index <= rear; index++)

     {

           if (value >= queue[index])

                value = queue[index];

     }

     return value;

}

dataType Queue::sum()

{

     int value = 0;

     for (int index = front+1; index <= rear; index++)

     {

           value += queue[index];

     }

     return value;

}

int Queue::length()

{

     int counter = 0;

     if (!isEmpty())

     {

           for (int index = front+1; index <= rear; index++)

                counter++;

     }

     return counter;

}

double Queue::avg()

{

     int value = (double) sum();

     return value / length();

}

void Queue::print()

{

     if (!isEmpty())

     {

           for (int index = front+1; index <= rear; index++)

           {

                cout << queue[index] << " , ";

           }

           cout << endl;

     }

     else

           cout << "Queue is empty.\n";

}

Main.cpp

#include "queue.h"

using namespace std;

void main() {

     Queue queue;

     queue.enQueue(1);

     queue.enQueue(2);

     queue.enQueue(3);

     queue.enQueue(4);

     queue.enQueue(5);

     cout << "Orignal Queue " << endl;

     queue.print();

     queue.deQueue();

     cout << "After  deQueue one item" << endl;

queue.print();

     cout << "Get Front : " << queue.getFront() << endl;

     cout << "Get Rear : " << queue.getRear() << endl;

     cout << "Get Sum : " << queue.sum() << endl;

     cout << "Get Average : " << queue.avg() << endl;

     cout << "Get Maximum : " << queue.getMax() << endl;

     cout << "Get Minimum : " << queue.getMin() << endl;

     cout << "Get Length : " << queue.length() << endl;

}

Output

C++ Queue
C++ Queue




0 Comments