Question

In: Computer Science

Implementation of a a queue with a "circular" array or a "regular" array. (a) "circular" array:...

Implementation of a a queue with a "circular" array or a "regular" array.

(a) "circular" array: consider using the smallest number of fields/variables in a struct/class. List the fields/variables that support the enqueue and dequeue operations in O(1) time in the worst case. Explain why the list is the smallest and the two operations are O(1) time.

(b) "regular" array: explain the worst-case time complexity in big-O for the operations (queue size is at most n).

Solutions

Expert Solution

enque and deque of circular queue will execute in constant time(as there is no loop). so the complexity is O(1).

though, enque of linear queue is O(1) but deque require a loop, and it is O(n).

Queue.cpp

#include<iostream>
#include<stdlib.h>

using namespace std;

template <class T>
class Queue {
   protected:
   int size, front=-1, back=-1;
   T** values;
   public:
   Queue(int s) {
       size = s;
       values = (T**)malloc(s*sizeof(T*));
   }
   virtual void enqueue(T* value){};
   virtual T* dequeue() = 0;
};

template <class T>
class CircularQueue:public Queue<T> {
   public:
   CircularQueue(int s):Queue<T>(s){}
   virtual void enqueue(T* value) {
       if(Queue<T>::front==-1 && Queue<T>::back==-1) {
           Queue<T>::front=Queue<T>::back=0;
           Queue<T>::values[Queue<T>::back]=value;
       } else {
          
           if((Queue<T>::back+1)%Queue<T>::size==Queue<T>::front) {
               // Queue is full
           } else {
               Queue<T>::back++;
               Queue<T>::back%=Queue<T>::size;
               Queue<T>::values[Queue<T>::back]=value;
           }
       }
   }
          
   virtual T* dequeue() {
       if(Queue<T>::front==-1 && Queue<T>::back==-1) {
           // underflow
           return 0;
       }
       T* t = Queue<T>::values[Queue<T>::front];
      
       if(Queue<T>::back==Queue<T>::front) {
           Queue<T>::back = Queue<T>::front = -1;
       } else {
           Queue<T>::front++;
           Queue<T>::front%=Queue<T>::size;
       }
       return t;
   }
};

template <class T>
class LinearQueue:public Queue<T> {
   public:
   LinearQueue(int s):Queue<T>(s){}
   virtual void enqueue(T* value) {
       if(Queue<T>::front==-1 && Queue<T>::back==-1) {
           Queue<T>::front=Queue<T>::back=0;
           Queue<T>::values[Queue<T>::back]=value;
       } else {
          
           if(Queue<T>::back==Queue<T>::size) {
               // Queue is full
           } else {
               Queue<T>::back++;
               Queue<T>::values[Queue<T>::back]=value;
           }
       }
   }
          
   virtual T* dequeue() {
       if(Queue<T>::front==-1 && Queue<T>::back==-1) {
           // underflow
           return 0;
       }
       T* t = Queue<T>::values[Queue<T>::front];
      
       if(Queue<T>::back==Queue<T>::front) {
           Queue<T>::back = Queue<T>::front = -1;
       } else {
          
           Queue<T>::front++;
           for(int i=Queue<T>::front;i<=Queue<T>::back;i++) {
               Queue<T>::values[i-Queue<T>::front]=Queue<T>::values[i];
           }
           Queue<T>::back = Queue<T>::back-Queue<T>::front;
           Queue<T>::front = 0;
       }
       return t;
   }
};

int* i(int value) {
   int *v = (int*)malloc(sizeof(int));
   *v=value;
   return v;
}

int main() {
   Queue<int> *q = new CircularQueue<int>(5);
   cout<<"Circular Queue of size 5"<<endl;
   cout<<"enquing 5..."<<endl;
   q->enqueue(i(5));
   cout<<"enquing 7..."<<endl;
   q->enqueue(i(7));
   cout<<"enquing 4..."<<endl;
   q->enqueue(i(4));
   cout<<"enquing 3..."<<endl;
   q->enqueue(i(3));
   cout<<*(q->dequeue())<<" dequed"<<endl;
   cout<<*(q->dequeue())<<" dequed"<<endl;
   cout<<*(q->dequeue())<<" dequed"<<endl;
   cout<<"enquing 2..."<<endl;
   q->enqueue(i(2));
   cout<<"enquing 9..."<<endl;
   q->enqueue(i(9));
   cout<<"enquing 1..."<<endl;
   q->enqueue(i(1));
   cout<<*(q->dequeue())<<" dequed"<<endl;
   cout<<*(q->dequeue())<<" dequed"<<endl;
   cout<<*(q->dequeue())<<" dequed"<<endl;
   cout<<*(q->dequeue())<<" dequed"<<endl;
  
   q = new LinearQueue<int>(5);
   cout<<"Linear Queue of size 5"<<endl;
   cout<<"enquing 5..."<<endl;
   q->enqueue(i(5));
   cout<<"enquing 7..."<<endl;
   q->enqueue(i(7));
   cout<<"enquing 4..."<<endl;
   q->enqueue(i(4));
   cout<<"enquing 3..."<<endl;
   q->enqueue(i(3));
   cout<<*(q->dequeue())<<" dequed"<<endl;
   cout<<*(q->dequeue())<<" dequed"<<endl;
   cout<<*(q->dequeue())<<" dequed"<<endl;
   cout<<"enquing 2..."<<endl;
   q->enqueue(i(2));
   cout<<"enquing 9..."<<endl;
   q->enqueue(i(9));
   cout<<"enquing 1..."<<endl;
   q->enqueue(i(1));
   cout<<*(q->dequeue())<<" dequed"<<endl;
   cout<<*(q->dequeue())<<" dequed"<<endl;
   cout<<*(q->dequeue())<<" dequed"<<endl;
   cout<<*(q->dequeue())<<" dequed"<<endl;
   return 0;
}

output

Circular Queue of size 5
enquing 5...
enquing 7...
enquing 4...
enquing 3...
5 dequed
7 dequed
4 dequed
enquing 2...
enquing 9...
enquing 1...
3 dequed
2 dequed
9 dequed
1 dequed
Linear Queue of size 5
enquing 5...
enquing 7...
enquing 4...
enquing 3...
5 dequed
7 dequed
4 dequed
enquing 2...
enquing 9...
enquing 1...
3 dequed
2 dequed
9 dequed
1 dequed


Related Solutions

C++ Project The following is a simple implementation of the circular queue Important: for this project,...
C++ Project The following is a simple implementation of the circular queue Important: for this project, you must complete the work based on this initial code. 1. Fix the full() method. 2. Fix the empty() method. 3. Place "X" in the front position using a statement in the constructor. 4. Adjust the DEQ() method so that the front position in the array would always have "X" in it and the previous X should be blanked out. 5. Write the code...
Why a circular queue is more benefiting than a single dimension array queue? How to do...
Why a circular queue is more benefiting than a single dimension array queue? How to do indexing in a circular queue? (explain briefly in java)
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
Why a circular queue is more benefiting than a single dimension array queue? How to do...
Why a circular queue is more benefiting than a single dimension array queue? How to do indexing in a circular queue? (In java)
1) Given an array queue implementation with one unused entry and frontIndex and backIndex references, match...
1) Given an array queue implementation with one unused entry and frontIndex and backIndex references, match the status of the queue with the following information about frontIndex and backIndex for a queue with capacity of 5: a.) frontIndex is 0 and backIndex is 4 b.) frontIndex is 1 and backIndex is 4 c.) frontIndex is 3 and backIndex is 0 empty queue one element in the queue three elements in the queue four elements in the queue five elements in...
One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue <T>{ } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
#include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a...
#include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a heap using namespace std; /* In this lab you will implement a data structure that supports 2 primary operations: - insert a new item - remove and return the smallest item A data structure that supports these two operations is called a "priority queue". There are many ways to implement a priority queue, with differernt efficiencies for the two primary operations. For this lab,...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT