Question

Implement a Queue ADT using a circular arraywith 5 string elements.Create a Queue object and try...

Implement a Queue ADT using a circular arraywith 5 string elements.Create

a Queue object and try various operations below, in C++.

Queue myQueue;

myQueue.enqueue(“Computer 1”);

myQueue.enqueue(“Computer 2”);

myQueue.enqueue(“Computer 3”);

myQueue.dequeue();

myQueue.enqueue(“Computer 4”);

myQueue.enqueue(“Computer 5”);

string course = myQueue.front(); // computer should be Comp 2

size = myQueue.size();// size should be 4

// output course and size

Homework Answers

Answer #1

#include <iostream>

using namespace std;

class Queue {

        string *arr;
        int queueFront;
        int queueRear;
        int capacity;
        int numElements;

        void grow(int newsize) {
                string *newArray = new string[newsize];
                int count = 0;

                if (queueRear >= queueFront) {
                        for (int i = queueFront; i <= queueRear; i++) {
                                newArray[count++] = arr[i];
                        }
                } else {
                        for (int i = queueFront; i < capacity; i++) {
                                newArray[count++] = arr[i];
                        }
                        for (int i = 0; i <= queueRear; i++) {
                                newArray[count++] = arr[i];
                        }
                }

                if(count == 0) {
                        queueFront = queueRear = -1;
                } else {
                        queueFront = 0;
                        queueRear = count - 1;
                }

                capacity = newsize;
                delete [] arr;
                arr = newArray;
        }

        public:

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

        void enqueue(string value) {
                if ((queueFront == 0 && queueRear == capacity-1) ||
                        (queueRear == (queueFront-1)%(capacity-1))) {
                        //Queue is Full
                        grow(2 * capacity);
                        enqueue(value);
                        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++;
        }

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

        // Just returns the front element O(1)
        string front() {
                if(queueFront != -1) {
                        return arr[queueFront];
                } else {
                        return NULL;
                }
        }

        int size() {
                return numElements;
        }
};

int main() {
        Queue myQueue;

        myQueue.enqueue("Computer 1");
        myQueue.enqueue("Computer 2");
        myQueue.enqueue("Computer 3");
        myQueue.dequeue();
        myQueue.enqueue("Computer 4");
        myQueue.enqueue("Computer 5");

        string course = myQueue.front(); // computer should be Comp 2

        int size = myQueue.size();// size should be 4

        // output course and size
        cout << course << endl;
        cout << size << 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.

Know the answer?
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
Similar Questions
Write a C program: Implement the abstract data type (ADT) queue (FIFO) of strings. ADT has...
Write a C program: Implement the abstract data type (ADT) queue (FIFO) of strings. ADT has the following methods: clear – clears the queue; is_empty – returns 1 if the queue is empty, otherwise 0; is_full – returns 1 if the queue is full, otherwise 0; add – adds a new string at the end of the queue; remove – removes the string from the front of the queue. Use ADT queue to solve the following problems: • Write an...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty queue,...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
Specify and implement an ADT character string by using a linked chain of characters. Include typical...
Specify and implement an ADT character string by using a linked chain of characters. Include typical operations such as fi nding its length, appending one string to another, fi nding the index of the leftmost occurrence of a character in a string, and testing whether one string is a substring of another. Do not #include<string>...you are making something like a STL string. Also do not use an array or vector...the point is to practice linked lists. Remember the last character...
Using c++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function....
Using c++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function. Given a string s, isPalindrome(s) can determine if s is a palindrome, considering only alphanumeric characters and ignoring cases. Note: for the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: ”A man, a plan, a canal: Panama” Output: true Example 2: Input: ”race a car” Output: false Requirement: There are many methods to check if a string is...
Assume you have a stack and a queue implemented using an array of size 4. show...
Assume you have a stack and a queue implemented using an array of size 4. show the content of the array for the stack (then the queue) for the following operations: (for the queue replace push by add and pop by remove; keep in mind that the queue uses a circular array): push(-3), push(-5), push(-9), push(-10), pop(), pop(), push(-13), pop(), push( -15), push(-17).
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your Stack class by determining if the following strings have matching open and closing ( ) and/or [ ] and/or { }. To test matches, push open (, [, { onto the stack. Input a close char, pop off the stack and see if input matches symbol form stack.   Use the following data to test your code:                               ( )                               [ ] ( )...
VIVA QUESTIONS: 1. Implement the following function using VHDL coding. (Try to minimize if you can)....
VIVA QUESTIONS: 1. Implement the following function using VHDL coding. (Try to minimize if you can). F(A,B,C,D)=(A'+B+C). (A+B'+D'). (B+C'+D') . (A+B+C+D) 2. What will be the no. of rows in the truth table of N variables? 3. What are the advantages of VHDL? 4. Design Ex-OR gate using behavioral model? 5. Implement the following function using VHDL code f=AB+CD. 6. What are the differences between half adder and full adder? 7. What are the advantages of minimizing the logical expressions?...
Data structures in java Implement the following classes: 1. class Course that includes three instance variables:...
Data structures in java Implement the following classes: 1. class Course that includes three instance variables: private String Name; // the course name private int ID; // the course ID private Course next; // the link Your class should have the following:  A constructor that initializes the two instance variables id and name.  Set and get methods for each instance variable. 2. class Department that includes three instance variables: private String deptName; private Course head, tail; Your class...
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in...
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in a single variable x (e.g., p = 4 x^5 + 7 x^3 – x^2 + 9 ). For this problem, consider only polynomials whose exponents are non-negative integers. You are required to identify a proper data representation schema to store such polynomials and hide such data from external users of this ADT. Additionally, your ADT will at least include the following member functions: One...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT