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

Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
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...
Program in Java Write an algorithm to transfer the elements from queue Q1 to queue Q2,...
Program in Java Write an algorithm to transfer the elements from queue Q1 to queue Q2, so that the contents in Q2 will be in reverse order as they are in Q1 (e.g. if your queue Q1 has elements A, B, and C from front to rear, your queue Q2 should have C, B, and A from front to rear). Your algorithm must explicitly use an additional stack to solve the problem. Write your algorithm in pseudo code first, and...
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.
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H...
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal = top; top...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT