Question

Code in Java Create a queue class to store integers and implement following methods: 1) void...

Code in Java

Create a queue class to store integers and implement following methods:

1) void enqueue(int num): This method will add an integer to the queue (end of the queue).

2) int dequeue(): This method will return the first item in the queue (First In First Out).

3) void display(): This method will display all items in the queue (First item will be displayed first).

4) Boolean isEmpty(): This method will check the queue and if it is empty, this will return true, otherwise false.

Homework Answers

Answer #1
class Queue { 
    //front points to front of Queue
    //rear points to rear of Queue
    //size is the length of Queue
    //array is to store the values
    //capacity is the length of the array 
    int front, rear, size; 
    int capacity; 
    int array[]; 
  
    public Queue(int capacity) 
    { 
        this.capacity = capacity; 
        front = this.size = 0; 
        rear = capacity - 1; 
        array = new int[this.capacity]; 
    } 
  
  
    // Queue is empty when size is 0 
    boolean isEmpty(Queue queue) 
    { 
        return (queue.size == 0); 
    } 
  
    //enqueue method to add an num to the queue. 
    //This changes rear and size 
    void enqueue(int num) 
    { 
        //rear=(rear+1)%capacity
        //mod by capacity is to keep rear in range of array length
        this.rear = (this.rear + 1) % this.capacity; 
        
        //Storing the val in array
        this.array[this.rear] = num; 
        
        //Queue is increased by 1
        this.size = this.size + 1; 
    } 
  
    //dequeue method to remove an num from queue. 
    //This changes front and size 
    int dequeue() 
    { 
        //If queue is empty 
        //Case of Underflow
        if (isEmpty(this)) 
            return Integer.MIN_VALUE; 
  
        int num = this.array[this.front];
        
        //front=(front+1)%capacity
        //mod by capacity is to keep fornt in range of array length
        this.front = (this.front + 1) % this.capacity;
        
        //Queue is decreased by 1
        this.size = this.size - 1; 
        return num; 
    } 
    
    void display()
    {
        for(int i=this.front;i < this.front+this.size;i++)
        {
            System.out.print(this.array[i]+" ");
        }
    }
  
} 
  
//Driver class 
public class Test { 
    public static void main(String[] args) 
    { 
        Queue queue = new Queue(100); 
  
        queue.enqueue(100); 
        queue.enqueue(150); 
        queue.enqueue(200); 
        queue.enqueue(400); 
  
        System.out.println("Queue is (before deque) ");
        queue.display();
        System.out.println();
        System.out.println(queue.dequeue()+" dequeued from queue\n"); 
        System.out.println("Queue is (after deque) ");
        queue.display();
        System.out.println();
        System.out.println(queue.dequeue()+" dequeued from queue\n"); 
        System.out.println("Queue is (after deque) ");
        queue.display();
        
        
    } 
} 

Output Screenshot:

(Note: Please refer to the screenshot of the code to understand the indentation of the code.)

Code Screenshots:

Queue and its constructor:

isEmpty() and enqueue():

dequeue() and display():

main():

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
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may not use the original methods of the stack api to answer. import java.util.NoSuchElementException; import edu.princeton.cs.algs4.Stack; public class StringQueue {    //You may NOT add any more fields to this class.    private Stack stack1;    private Stack stack2;    /**    * Initializes an empty queue.    */    public StringQueue() { //TODO    }    /**    * Returns true if this queue...
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 { } 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,...
can you please explain how to complete all methods in java ? thanks /* Note:   Do...
can you please explain how to complete all methods in java ? thanks /* Note:   Do not add any additional methods, attributes.         Do not modify the given part of the program.         Run your program against the provided Homework2Driver.java for requirements. */ /* Hint:   This Queue implementation will always dequeue from the first element of         the array i.e, elements[0]. Therefore, remember to shift all elements         toward front of the queue after each dequeue. */ public class QueueArray<T>...
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....
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements....
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements. But in a queue, the elements are retrieved in a first-in first-out fashion. The class contains: An int[] data field named elements that stores the int values in the queue. A data field named size that stores the number of elements in the queue. A constructor that creates a Queue object with default capacity 8. The method enqueue(int v) that adds v into the...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads in a sequence of strings from standard input using StdIn.readString() , and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. You may assume that 0 k N , where N is the number of string on standard input. The running time of the program must be linear in the size of...
Write a queue client, "LineNum," that takes an integer command line argument ā€œnā€ and prints the...
Write a queue client, "LineNum," that takes an integer command line argument ā€œnā€ and prints the nth string from the first string found on standard input. [MO6.2] Please note that you would need to use a queue to implement it for full credit. You should add the strings inputted by the user to a queue using the enqueue method. Then you should remove and return "n" strings from the queue using the dequeue method. The nth string that is returned...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT