Question

Define a class for static Queue for storing integer numbers. Your class must include a constructor,...

Define a class for static Queue for storing integer numbers. Your class must include a constructor, a destructor, enqueue, dequeue, display, isFull, and isEmpty functions. Write a simple main function in your program that demonstrates the class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Talll. PP https://Petty Serpentine Flashdrive-1.a Savcu return numElements == 0; int size() { return numElements; clang versi

#include <iostream>

using namespace std;

class Queue {
    int *arr;
    int queueFront;
    int queueRear;
    int capacity;
    int numElements;

public:

    Queue(int size) {
        capacity = size;        
        arr = new int[capacity];
        queueFront = -1;
        queueRear = -1;
        numElements = 0;
    }

    ~Queue() {
        delete [] arr;
    }

    void enqueue(int value) {
        if ((queueFront == 0 && queueRear == capacity-1) ||
            (queueRear == (queueFront-1)%(capacity-1))) {
            //Queue is Full
            return;
        } else if(queueFront == -1) {
            // empty queue
            queueFront = queueRear = 0;
            arr[queueRear] = value;
        }  else if(queueRear == capacity-1 && queueFront != 0) {
            queueRear = 0;
            arr[queueRear] = value;
        }  else {
            queueRear += 1;
            arr[queueRear] = value;
        }
        numElements++;
    }

    int dequeue() {
        if(queueFront == -1) {
            // empty queue.
            return -1;
        }
        int data = arr[queueFront];
        
        if(queueFront == queueRear) {
            // single element
            queueFront = -1;
            queueRear = -1;
        } else if (queueFront == capacity-1) {
            queueFront = 0;
        } else {
            queueFront++;
        }
        numElements--;
        return data;
    }

    void display() {
        int i = queueFront;
        int count = 0;
        while(count < numElements) {
            int index = (i + count) % numElements;
            cout << arr[index] << " ";
            count++;
        }
        cout << endl;
    }

    bool isFull() {
        return numElements == capacity;
    }
    bool isEmpty () {
        return numElements == 0;
    }

    int size() {
        return numElements;
    }
};

int main() {
    Queue myQueue(4);

    myQueue.enqueue(1);
    myQueue.enqueue(2);
    myQueue.enqueue(3);
    myQueue.enqueue(4);

    myQueue.display();

    cout << myQueue.isFull() << endl;
    cout << myQueue.isEmpty() << endl;

    cout << myQueue.dequeue() << endl;
    cout << myQueue.dequeue() << endl;
    cout << myQueue.dequeue() << endl;
    cout << myQueue.dequeue() << endl;

    cout << myQueue.isFull() << endl;
    cout << myQueue.isEmpty() << endl;

}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
Define a class for static Queue for storing integer numbers. Your class must include a constructor,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • How do I pass values to this function? class DynIntQueue { struct QueueNode { int value;...

    How do I pass values to this function? class DynIntQueue { struct QueueNode { int value; QueueNode *next; QueueNode(int value1, QueueNode *next1 = nullptr) { value = value1; next = next1; } }; // These track the front and rear of the queue QueueNode *front; QueueNode *rear; public: // Constructor and Destructor DynIntQueue(); ~DynIntQueue(); // Member functions void enqueue(int); void dequeue(int &); bool isEmpty() const; void clear(); }; main #include <iostream> #include "DynIntQueue.h" using namespace std; int main() {DynIntQueue list;...

  • Write a function that takes a string parameter and determines whether the string contains matching grouping...

    Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use...

  • The class pictured below is designed to implement an integer queue using two stacks. Assume the...

    The class pictured below is designed to implement an integer queue using two stacks. Assume the stack methods all work as desired (though their implementations are not shown). .(a) Trace what happens in the following situation, showing intermediate steps (values of variables, what is in the stacks, and what is in the queue at various points in the methods, not just the results of the methods). • Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue twice....

  • Design and implement a class Q that uses Q.java as a code base. The queue ADT...

    Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...

  • Build and use your own minimal queue class using an array of type String of fixed...

    Build and use your own minimal queue class using an array of type String of fixed size to hold the queue. The array should have the default size of 5. The array size should be setable via a constructor. The following methods must be implemented: enqueue – inserts a value at the rear of the queue dequeue – returns the value at the front of the queue, removing the value from the queue isEmpty – returns true if the queue...

  • (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a...

    (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a Singly Linked-List and Implementing a Priority Queue Tasks/Assignments(s) ■ Write your own program to implement priority queue using the Linked-List and implement the Enqueue, Dequeue and Display methods. implement the Merge methods in your main program that receive Q1 and Q2 and merge the two queues into one queue. Public static PriorityQueue MergeQueue(PriorityQueue Q1,PriorityQueue Q2) Write main program to test priority queue class and...

  • in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

    in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...

  • Design a Java class named StringQueue for storing strings of first names into a queue. The...

    Design a Java class named StringQueue for storing strings of first names into a queue. The StringQueue.java class contains: * A String[] data field named names that stores the String values in the queue. * A private data field named size that stores the number of names in the queue. * A public static final data field named DEFAULT_CAPACITY = 10 for the array. * A private static data field named firstInitCtr that counts the number of names with the...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT