Question

In: Computer Science

java method for dequeue write the “dequeue” method for a queue of type double. If the...

java method for dequeue

write the “dequeue” method for a queue of type double. If the queue is empty return 0.0. Make sure to change the links properly ensure that the data structure remains a queue.

use the code provided below

public class Q04 
{
        public class ListNode//Public for testing purposes
        {
                public double data;
                public ListNode link;
                public ListNode(double aData, ListNode aLink)
                {
                        data = aData;
                        link = aLink;
                }
        }
        public ListNode head;//Public for testing purposes
        public ListNode tail;//Public for testing purposes
                
        public double dequeue()
        {
//------------------------------------------------------------------------------------  
                //Write your code here

                
                
                
                
                
                
                
                
                
                
                
                

        }//Do not alter 
        //Test your code here. You may alter this as needed.
        public static void main(String[] args)
        {
                //Example
                Question04 intQ = new Question04();
                intQ.head = intQ.new ListNode(0, 
                                                        intQ.new ListNode(1, 
                                                                        intQ.new ListNode(2,
                                                                                        intQ.new ListNode(3,
                                                                                                        intQ.new ListNode(4,null)))));
                //Printing Results
                System.out.println(intQ.dequeue());
        }
        //--------------------------------------------------------------------------------

}

Solutions

Expert Solution

public class Question04 {
    public class ListNode//Public for testing purposes
    {
        public double data;
        public ListNode link;

        public ListNode(double aData, ListNode aLink) {
            data = aData;
            link = aLink;
        }
    }

    public ListNode head;//Public for testing purposes
    public ListNode tail;//Public for testing purposes

    public double dequeue() {
//------------------------------------------------------------------------------------
        if (head == null) {
            return 0;
        } else {
            double result = head.data;
            head = head.link;
            if (head == null)
                tail = null;
            return result;
        }
    }//Do not alter

    //Test your code here. You may alter this as needed.
    public static void main(String[] args) {
        //Example
        Question04 intQ = new Question04();
        intQ.head = intQ.new ListNode(0,
                intQ.new ListNode(1,
                        intQ.new ListNode(2,
                                intQ.new ListNode(3,
                                        intQ.new ListNode(4, null)))));
        //Printing Results
        System.out.println(intQ.dequeue());
    }
    //--------------------------------------------------------------------------------
}

Related Solutions

write a method in java that insert First(Queue<E> s E e)that receives a queue and an...
write a method in java that insert First(Queue<E> s E e)that receives a queue and an elment, it inserts the element at the beginnings of queue
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. You may not add any more fields to the class. 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;    /**    * Initialize an empty queue.    */    public StringQueue() { //TODO   ...
In java Q2. Write a program that reads grades of type double of eight students that...
In java Q2. Write a program that reads grades of type double of eight students that the user provides. The grades lie between 0 and 10. These grades should be written to a binary file and read from it. The program outputs the highest and lowest grades achieved by students on the screen. The file contains nothing but numbers of type double written to the file with writeDouble.
in java Write a while loop to ask the user to type number of hours(double) they...
in java Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than or equal to 0 and less than 5, then:  salary = numberofhours * 5, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 10, then: salary = numberofours * 8, loop continues, the user...
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 please. Write a static method sqrt()that takes a double argument and returns the square root...
Java please. Write a static method sqrt()that takes a double argument and returns the square root of that number using newton's method to compute result.
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on...
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on input two sets represented as hash sets, returns their Jaccard similarity. The following are a few sample runs: Input :    A=1, 2, 3, 4,   B=2, 4, 6, 8 Return:   0.3333333333333333 Input :    A=Larry, Michael, Shaq, Kobe, LeBron                    B=Steve, Kobe, Shaq, LeBron, Steph, Jeremy, Michael Return:   0.5 Your method must have time complexity On and space complexity O1, where n is the size of...
A deque (short for double-ended queue, but pronounced “deck”) is an abstract data type that supports...
A deque (short for double-ended queue, but pronounced “deck”) is an abstract data type that supports adding and removing at both the front and back. So, at a bare minimum, a deque has four operations: addFront(), removeFront(), addBack(), removeBack(). Suppose you have a deque D containing the numbers (1, 2, 3, 4, 5, 6, 7, 8), in this order. Suppose further that you have an initially empty queue Q. Give pseudo-code description of a method that uses only D and...
Java Write a method intersect_or_union_fcn() that gets vectors of type integer v1, v2, and v3 and...
Java Write a method intersect_or_union_fcn() that gets vectors of type integer v1, v2, and v3 and determines if the vector v3 is the intersection or union of vectors v1 and v2. Example 1: If v1 = {2, 3, 1, 5}, v2 = {3, 4, 5} and v3 = {3, 5}, then:               intersect_or_union_fcn(v1, v2, v3) will print:                              v3 is the intersection of v1 and v2 Example 2: If v1 = {2, 3, 1, 5}, v2 = {3, 4, 5}...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and a method to get all the values of the queue. (This is not writing a file implementing the java class PriorityQueue, but rather you are writing a program that is a priority queue).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT