Question

Write a class CircularArrayQueue such that all the following methods are implemented: 1. enqueue 2. dequeue...

Write a class CircularArrayQueue such that all the following methods are implemented:

1. enqueue

2. dequeue

3. first

4. isEmpty

5. size

6. toString

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class CircularArrayQueue {

    private static final int capacity = 100;
    private int front, rear, size;
    private Object[] data;

    public CircularArrayQueue() {
        front = rear = size = 0;
        data = new Object[capacity];
    }

    public CircularArrayQueue(int init) {
        front = rear = size = 0;
        data = new Object[init];
    }

    public void enqueue(Object element) {
        data[rear] = element;
        rear = (rear + 1) % data.length;
        size++;
    }


    public Object dequeue() {
        Object value = data[front];
        data[front] = null;
        front = (front + 1) % data.length;
        size--;
        return value;
    }


    public Object first() {
        return data[front];
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int size() {
        return size;
    }

    public String toString() {
        String value = "";
        int s = 0;
        while (s < size) {
            if (data[s] != null) {
                value += data[s].toString() + "\n";
            }
            s++;
        }
        return value;
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a class CircularArrayQueue such that all the following methods are implemented: 1. enqueue 2. dequeue...
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
  • Python use dequeue and enqueue methods, alter the dequeue method and the enqueue method. remove items...

    Python use dequeue and enqueue methods, alter the dequeue method and the enqueue method. remove items from the first element of the list (the front of the queue) and return this value and you need to add items to the last element of the list (the rear of the queue). 4. (20 points) Programming Exercise 5 from Chapter 3 of your textbook. Name the class Queuex, and use the other method names given in Chapter 3.11 in your digital textbook....

  • [Java] Efficiency Comparison of two Implemented Queues : Circular Array Queue and SSQueue and its enqueue...

    [Java] Efficiency Comparison of two Implemented Queues : Circular Array Queue and SSQueue and its enqueue and dequeue operations Need to write a program that compares the efficiency of the queue implementations. To do so, you need to find and compare the running times of 1 the following two scenarios for both queue implementations: 3.1 Scenario 1: Alternating Sequence of Enqueues and Dequeues For every n ∈ {20, 50, 100, 1000, 10000, 100000, 1000000}, do the following: 1. long startTime...

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

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

  • 3. Some circular queue implementations use the mod operator % in enqueue and dequeue operations. Explain...

    3. Some circular queue implementations use the mod operator % in enqueue and dequeue operations. Explain why this is inefficient. 4. If a queue is implemented using a singly linked list with a head and tail pointer, you should always insert at the tail and remove from the head. Explain why this is so. 5. What is a Priority Queue, and how does it differ from a standard queue? 6. Priority Queues are almost always implemented with an ordered data...

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

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

  • (java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E...

    (java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E e)  // two methods are the same. You could implement either one • •void addFirst(E e) • •E getFirst( ) = E peek( ) // two methods are the same. You could implement either one •E getLast( ) •E removeFirst() •E removeLast() Case 1 : Queue •Create a queue which is an instance of Dequeue. The queue should perform with given following input and print...

  • Create a program named NonZeroNumQueue.java then copy the contents from NonZeroNumQueue.txt. Then implement enqueue method. To...

    Create a program named NonZeroNumQueue.java then copy the contents from NonZeroNumQueue.txt. Then implement enqueue method. To get credit, DO NOT add or alter any data members/methods except the enqueue(int num). The NonZeroNumQueue will NOT accept zero, which mean you can enqueue any whole numbers EXCEPT 0(zero). You have already noticed that the queue is NEVER full, so the isFull() always returns false. given NonZeroNumQueue.txt public class NonZeroNumQueue{ private int[] data; private int total; private int front; private int tail; public...

  • In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods a...

    In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods also listed below: Data Members: Declare and initialize, as needed, the data item(s) you will need to manage a queue of integers. You may only use arrays and primitives for your instance and/or static variables (I,e You can’t use Java defined Queue / Stack / List...

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
Active Questions
ADVERTISEMENT