Code to Implement Dynamic Queue in C++ using arrays

Implementing Queues in c++ with arrays:

 introduction:

           Queues are linear data structures. They are used almost everywhere in software development , AI , machinelearning etc..

Topics Used:

code:

Buyahh copy and paste the code ==>.
#include <iostream>
using namespace std;
#define SIZE 10
template < class Eltype >
        class Queue {
                /*
                 *  MAXSIZE carries the max no of element a queue can have.
                 *  nItem  carries the no of element queue have.
                 *   front carries current front index
                 *   rear carries current rear index.
                 *   """" CIRCULAR  QUEUE """
                 */
                int MAXSIZE, front, rear, nItem;
                Eltype queue[1000];
                public:
                        Queue(int size = SIZE) { //constructor:-)
                                MAXSIZE = size;
                                front = -1;
                                rear = -1;
                                nItem = 0;
                        }
                int push() {
                        Eltype val;
                        //pushes element in queue
                        if (null()) {
                                //queue is null so front = rear=-1
                                cout << " Enter the item : ";
                                cin >> val;
                                front++;
                                rear++;
                                // adding alement at 0
                                queue[rear] = val;
                                nItem++;
                        } else if (!full()) {
                                // queue is not null as well as it is not full
                                if (rear != MAXSIZE - 1) {
                                        cout << " Enter the item : ";
                                        cin >> val;
                                        // checking that rear is not the end
                                        //if not, adding an element at rear++;
                                        rear++;
                                        queue[rear] = val;;
                                        nItem++;
                                } else {
                                        cout << " Enter the item : ";
                                        cin >> val;
                                        /* Imagine a situation where first two element is empty but 3 ,4 ,5 is full.
                                         * so. without this condition it can give Queue overflow.
                                         * but intialising rear by zero can solve this problem
                                         */
                                        // means rear == MAXSIZE but queue is not full so
                                        rear = 0;
                                        // initialisin rear by 0;
                                        queue[rear] = val;
                                        nItem++;
                                }
                        } else {
                                cout << "Queue overflow";
                                // getch();
                                exit(0);
                                return 0;
                        }
                        return 1;
                }
                int pop() {
                        // /deleting  an element from queue.
                        if (null()) {
                                // its already null. No need to be popped;
                                cout << "Queue underflow";
                                // getch();
                                exit(0);
                                return 0;
                        } else {
                                // it is not null
                                if (front != MAXSIZE) {
                                        // checking that front is not equal to end (not rear). rear and end are two different terminologies.
                                        // current index element is equal to null
                                        queue[front] = NULL;
                                        front++;
                                        nItem--;
                                } else {
                                        // means queue is not null and front = rear ("Circural Queue condition")
                                        // so , front = 0;
                                        front = 0;
                                        queue[front] = NULL;
                                        nItem--;
                                }

                        }
                        return 1;
                }
                void printall() {
                        // printing all elements in queue
                        int i;
                        for (i = 0; i < MAXSIZE; i++) {
                                cout << " " << queue[i];
                        }
                }
                void debug() {
                        // printting terminologies fro debugging.
                        cout << "nItems : " << nItem << "front :" << front << "rear :" << rear;
                }
                private:
                        int full() {
                                // checking if full
                                return ((nItem == MAXSIZE) ? 1 : 0);
                        }
                int null() {
                        (nItem <= 0) ? front = rear = -1: 0;
                        // checking if null.
                        return ((nItem <= 0) ? 1 : 0);
                }

        };
void switcher(char c) {
        int ch;
        if (c == 'c') {
                char c;
                Queue < char > queue;
                while (1) {
                        cout << "1.for insertion \n2. for deletion\n3 for printall\n4 for exit\n";
                        cin >> ch;
                        switch (ch) {
                        case 1:
                                queue.push();
                                break;
                        case 2:
                                queue.pop();
                                break;
                        case 3:
                                queue.printall();
                                break;
                        case 4:
                                exit(0);
                                break;
                        default:
                                cout << "Enter a valid choice";
                        }
                }
        }
        if (c == 'i') {
                char c;
                Queue < int > queue;
                while (1) {
                        cout << "1.for insertion \n2. for deletion\n3 for printall\n4 for exit\n";
                        cin >> ch;
                        switch (ch) {
                        case 1:
                                queue.push();
                                break;
                        case 2:
                                queue.pop();
                                break;
                        case 3:
                                queue.printall();
                                break;
                        case 4:
                                exit(0);
                                break;
                        default:
                                cout << "Enter a valid choice";
                        }
                }
        }
        if (c == 'f') {
                char c;
                Queue < float > queue;
                while (1) {
                        cout << "1.for insertion \n2. for deletion\n3 for printall\n4 for exit\n";
                        cin >> ch;
                        switch (ch) {
                        case 1:
                                queue.push();
                                break;
                        case 2:
                                queue.pop();
                                break;
                        case 3:
                                queue.printall();
                                break;
                        case 4:
                                exit(0);
                                break;
                        default:
                                cout << "Enter a valid choice";
                        }
                }
        }
        if (c == 'b') {
                char c;
                Queue < bool > queue;
                while (1) {
                        cout << "1.for insertion \n2. for deletion\n3 for printall\n4 for exit\n";
                        cin >> ch;
                        switch (ch) {
                        case 1:
                                queue.push();
                                break;
                        case 2:
                                queue.pop();
                                break;
                        case 3:
                                queue.printall();
                                break;
                        case 4:
                                exit(0);
                                break;
                        default:
                                cout << "Enter a valid choice";
                        }
                }
        }
}
int main() {
        char c;
        int ch;
        while (1) {
                cout << "Enter the datatype of queue : ";
                cin >> c;
                switcher(c);
        }
        return 0;
}
or download the file from here.

Comments

  1. This code for implementing a dynamic queue in C++ using arrays is really helpful for understanding queue operations and memory management. For students struggling with similar topics, I highly recommend checking out c programming assignment help. It provides detailed explanations and practical examples that make learning much easier. Whether you are dealing with queues, stacks, or other data structures, this resource can guide you step-by-step through your C programming assignments efficiently.

    ReplyDelete

Post a Comment

Popular posts from this blog

NPTEL Introduction to programming in c Assignment 4 solutions

C PROGRAM TO ECHO "*" - C PROGRAM TO GET PASSWORD FROM USER