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 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...
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).
Create a Deque class based on the following description of deque (double-ended queues). A dequeue is...
Create a Deque class based on the following description of deque (double-ended queues). A dequeue is a double-ended queue. You can insert items at either end and delete them from either end. Therefore, the deque offers two insert operations and two remove operations: insertLeft() and insertRight() removeLeft() and removeRight(). Deques may also have "peek" methods ( peekLeft() and peekRight() )that let you see the left or right item in the deque without remove the item from the deque. For this...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT