Question

In: Computer Science

Create a dynamic array-based Queue ADT class in C++ that contains enqueue(Inserts newElement at the back...

Create a dynamic array-based Queue ADT class in C++ that contains enqueue(Inserts newElement at the back ) and dequeue(Removes the frontmost element ). If the size of the array is equal to the capacity a new array of twice the capacity must be made.

The interface is shown:

class Queue {
    private:
        int* elements;  
        unsigned elementCount;  // number of elements in the queue
        unsigned capacity;      // number of cells in the array
        unsigned frontindex;    // index the topmost element
        unsigned backindex;     // index where the next element will be placed

    public:
        // Description: Constructor
        Queue();


        // Description: Inserts newElement at the back (O(1))
        void enqueue(int newElement);

 
        // Description: Removes the frontmost element (O(1))
        // Precondition: Queue not empty
        void dequeue();


        // Description: Returns a copy of the frontmost element (O(1))
        // Precondition: Queue not empty
        int peek() const;


        // Description: Returns true if and only if queue empty (O(1))
        bool isEmpty() const;
};

Thanks!

Solutions

Expert Solution

#include<iostream>
using namespace std;
class Queue {
private:
int* elements;
unsigned elementCount; // number of elements in the queue
unsigned capacity; // number of cells in the array
unsigned frontindex; // index the topmost element
unsigned backindex; // index where the next element will be placed

public:
// Description: Constructor
Queue()
{
           elements = new int[10];
           capacity =10;
           frontindex=backindex=-1;  
       }
  


// Description: Inserts newElement at the back (O(1))
void enqueue(int newElement)
{
           if(backindex+1==capacity)   //then expanding queue by double size
           {
               int *t = new int[capacity*2];
               for(int i=frontindex;i<=backindex;i++)
               t[i]=elements[i];
               elements = t;
               enqueue(newElement);
              
           }
           else
           {
               if(frontindex==-1)
               {
                   elements[frontindex+1]=newElement;
                   frontindex++;
                   backindex++;  
               }
               else
               {
                   elements[backindex+1]=newElement;
                   backindex++;  
               }  
           }  
       }


// Description: Removes the frontmost element (O(1))
// Precondition: Queue not empty
void dequeue()
{
           if(!isEmpty())//if list is not empty
           {
               for(int i=frontindex;i<backindex;i++)
               elements[i]=elements[i+1];
               backindex--;
               if(backindex==-1)
               frontindex=-1;
                  
           }
          
              
       }


// Description: Returns a copy of the frontmost element (O(1))
// Precondition: Queue not empty
int peek() const
{
           return elements[frontindex];  
       }


// Description: Returns true if and only if queue empty (O(1))
bool isEmpty() const
{
           if(frontindex==-1)return true;
           return false;  
       }
};

int main()
{//testing
   Queue q;
   q.enqueue(1);
   q.enqueue(2);
   q.enqueue(3);
   cout<<q.peek()<<endl;
   q.dequeue();
   cout<<q.peek()<<endl;
   q.dequeue();
   cout<<q.peek()<<endl;
   q.dequeue();
   return 0;
}

output:

1
2
3


Process exited normally.
Press any key to continue . . .

//PLS give a thumbs up if you find this helpful, it helps me alot, thanks.


//if you have any doubts, ask me in the comments


Related Solutions

Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with...
Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with a fixed-front approach as opposed to a floating-front approach.
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue...
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue object and try various operations below. Queue myQueue = new Queue(); myQueue.enqueue(“CPS 123”); myQueue.enqueue(“CPS 223”); myQueue.enqueue(“CPS 323”); myQueue.dequeue(); myQueue.enqueue(“CPS 113”); myQueue.enqueue(“CPS 153”); string course = myQueue.front(); // course should be CPS 223 size = myQueue.size(); // size should be 4 // output course and size
please complete the header file that contains a class template for ADT Queue and complete all...
please complete the header file that contains a class template for ADT Queue and complete all the member functions in the class template. Submit the header file only, but please write a source file that tests all the member functions to make sure they are working correctly. queue.h #ifndef _QUEUE #define _QUEUE #include"Node.h" template<class ItemType> class Queue { private:    Node<ItemType> *backPtr;    Node<ItemType> *frontPtr; public:    Queue(); //Default constructor    Queue(const Queue<ItemType> &aQueue);    bool isEmpty() const;    bool...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array of signed integers. Write a program that creates an Array container of size 100 and fills it with random numbers in the range [1..999]. (Use std::rand() with std::srand(1).) When building the array, if the random number is evenly divisible by 3 or 5, store it as a negative number. Within your main.cpp source code file, write a function for each of the following processes....
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the...
Using the Queue ADT: Create a program that uses a Queue. Your program should ask the user to input a few lines of text and then outputs strings in same order of entry. (use of the library ArrayDeque) In Java please.
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS:...
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS: Not allowed to use ANY built-in Python data structures and their methods. You must solve by importing the DynamicArray class and using class methods to write solution. Also not allowed to directly access any variables of the DynamicArray class (like self.size, self.capacity and self.data in part 1). All work must be done by only using class methods. Below is the Bag ADT starter code...
Write an array-based implementation of the ADT list that expands the size of the array of...
Write an array-based implementation of the ADT list that expands the size of the array of list entries as needed so that the list can always accommodate a new entry. Also reduce the size of the array as needed to accommodate several removals. When the size of the array is greater than 20 and the number of entries in the list is less than half the size of the array, reduce the size of the array so that it is...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT