Question

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 is empty, false otherwise

isFull - – returns true if the queue is full, false otherwise

Tip: In a queue of 2 or more elements, the index of the rear is “larger” than the index of the front – EXCEPT, the queue will need to “wrap around” the array. For example: If the array is 20 elements. the queue front is at index 15 and the queue rear is at index 19, the next enqueue will be at index 20, which doesn't exist. The index must “wrap around” the array, i. e. the next enqueue will be at index 0. The solution is to modulus by the array size whenever increasing an index, either rear of front. For example, to change the index of the rear to the next index,use the formula

rear = (rear + 1) % arraySize;

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

myqueue.java

public class myqueue
{
public String[] array ;
int front,rear,size;

public myqueue(int n) {
    this.array = new String[n];
    this.front = -1;
    this.rear = -1;
    this.size = n;
}
public myqueue()
{
    this.array = new String[5];
    this.front = -1;
    this.rear = -1;
    this.size = 5;
}

public void enqueue(String value)
   {
       if((this.front == 0 && this.rear == this.size - 1) || (this.front == this.rear+1))
       {
                System.out.println("Queue is Full");
       }
       else
       {
          if(this.rear == this.size -1 && this.front != 0)
          {
           this.rear = -1;
          }
          this.rear = this.rear + 1;
          this.array[this.rear] = value;
          System.out.println("Inserted!");
          if(this.front == -1)
          {
           this.front = 0;
          }

       }
   }

public void dequeue()
   {
       if(this.front == -1 && this.rear == -1)
              {
                System.out.println("Queue is empty");
              }
       else{         
              System.out.println("Deleted element " + this.array[this.front]);
              this.front = this.front + 1;

              if(this.front-1 == this.rear)
              {
               this.front = -1;
               this.rear = -1;
              }

              if(this.front == this.size)
              {
                  this.front = 0;
              }

          }
   }

public boolean isfull()
   {
       if((this.front == 0 && this.rear == this.size - 1) || (this.front == this.rear+1))
       {
                System.out.println("Queue is Full");
                return true;
       }
       else
       {
           return false;
       }
   }

public boolean isempty()
   {

       if(this.front == -1 && this.rear == -1)
              {
                System.out.println("Queue is empty");
                return true;
              }
              else
              {
                  return false;
              }
   }

}

driver.java

public class driver
{
    public static void main(String[] args)
    {
     myqueue Q = new myqueue(5);
     Q.enqueue("abc1");
     Q.enqueue("abc2");
     Q.enqueue("abc3");
     Q.enqueue("abc4");
     Q.enqueue("abc5");
     Q.isfull();
     Q.enqueue("abc6");

     Q.dequeue();
     Q.dequeue();
     Q.dequeue();
     Q.dequeue();
     Q.dequeue();   

     Q.isempty();
     Q.dequeue();   
}
}

Sample Output:

Inserted!
Inserted!
Inserted!
Inserted!
Inserted!
Queue is Full
Queue is Full
Deleted element abc1
Deleted element abc2
Deleted element abc3
Deleted element abc4
Deleted element abc5
Queue is empty
Queue is empty

Add a comment
Know the answer?
Add Answer to:
Build and use your own minimal queue class using an array of type String of fixed...
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
  • 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;...

  • Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...

    Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT: 3) Queue ADT that uses an array internally (call it AQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics. The Queue ADT The Queue ADT...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • 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...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static...

    AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static final int DEFAULT_SIZE = 10; private int maxSize; // Maximum size of queue private int front; // Index of front element private int rear; // Index of rear element // Constructors @SuppressWarnings("unchecked") // Generic array allocation AQueue(int size) { //BUG #1: maxSize = size maxSize = size+1; // One extra space is allocated rear = 0; front = 1; queueArray = (E[])new Object[maxSize]; //...

  • PROBLEM- void CBQueue::printF2B( ) If the queue is empty, the method should print “Queue is empty”,...

    PROBLEM- void CBQueue::printF2B( ) If the queue is empty, the method should print “Queue is empty”, otherwise, this method should print the items in the queue starting at the front of the queue and proceeding to the rear of the queue. The items should be printed one per line. Now that this method is written, you can do a more thorough job of testing enqueue( ). You will want to call printF2B( ) and printB2F( ) after implementing each method...

  • PROBLEM: string CBQueue::dequeue( ) This method should remove and return the item at the front of...

    PROBLEM: string CBQueue::dequeue( ) This method should remove and return the item at the front of the queue- please add comments EXISTING CODE: #include // this allows you to declare and use strings #include using namespace std; struct qNode {   string data;   qNode* next;   qNode* prev; }; class CBQueue {   public:     CBQueue(); int CBQueue::getSize( ); bool CBQueue::isEmpty( );   private:     qNode* front;     qNode* rear;     int size; }; #include "CBQueue.h" CBQueue::CBQueue() { front = NULL; rear = NULL; size = 0; }...

  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • 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...

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