Question

(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue:...

(Please use Java Eclipse)

QUESTION 1:

For the question below, assume the following implementation of LinkedQueue:

public static final class LinkedQueue<T> implements QueueInterface<T> {
    private Node<T> firstNode;
    private Node<T> lastNode;

    public LinkedQueue() {
        firstNode = null;
        lastNode = null;
    }


    @Override
    public T getFront() {
        if (isEmpty()) {
            return null;
        }
        return firstNode.getData();
    }


    @Override
    public boolean isEmpty() {
        return firstNode == null;
    }


    @Override
    public void clear() {
        firstNode = null;
        lastNode = null;

    }

    public class Node<E> {
        private E data; // Entry in bag
        private Node<E> next; // Link to next node

        public Node(E dataPortion) {
            this(dataPortion, null);
        } // end constructor


        public Node(E dataPortion, Node<E> nextNode) {
            data = dataPortion;
            next = nextNode;
        } // end constructor


        public E getData() {
            if (data != null) {
                return data;
            }
            return null;
        }


        public Node<E> getNext() {
            return next;
        }


        public void setNext(Node<E> newNext) {
            next = newNext;
        }

} // end LinkedQueue

Below, finish creating the enqueue method that has been started for you.

 
public void enqueuePractice(T newEntry) {
    Node<T> newNode = new Node<T>(newEntry);
    if (isEmpty()) {
        firstNode = newNode;
    }
 
}

QUESTION 2:

For the question below, assume the following implementation of LinkedQueue:

public static final class LinkedQueue<T> implements QueueInterface<T> {
    private Node<T> firstNode;
    private Node<T> lastNode;

    public LinkedQueue() {
        firstNode = null;
        lastNode = null;
    }


    @Override
    public T getFront() {
        if (isEmpty()) {
            return null;
        }
        return firstNode.getData();
    }


    @Override
    public boolean isEmpty() {
        return firstNode == null;
    }


    @Override
    public void clear() {
        firstNode = null;
        lastNode = null;

    }

    public class Node<E> {
        private E data; // Entry in bag
        private Node<E> next; // Link to next node

        public Node(E dataPortion) {
            this(dataPortion, null);
        } // end constructor


        public Node(E dataPortion, Node<E> nextNode) {
            data = dataPortion;
            next = nextNode;
        } // end constructor


        public E getData() {
            if (data != null) {
                return data;
            }
            return null;
        }


        public Node<E> getNext() {
            return next;
        }


        public void setNext(Node<E> newNext) {
            next = newNext;
        }

} // end LinkedQueue

Your task is to create a "priority" enqueue method. If something is enqueued, it normally goes to the back of the line, but in this method it will go to the front of the line.

For example, if you had a linked queue that looked like:

(firstNode) 1 --> 2 --> 3 (lastNode)

and you ran priorityEnqueue(4)

the result would be:

(firstNode) 4 --> 1 --> 2 --> 3 (lastNode)

 
public void priorityEnqueue(T newEntry) {
    Node<T> newNode = new Node<T>(newEntry);
    if (isEmpty()) {
        lastNode = newNode;
    } 
 
}

QUESTION 3:

For the question below, assume the following implementation of LinkedQueue:

public static final class LinkedQueue<T> implements QueueInterface<T> {
    private Node<T> firstNode;
    private Node<T> lastNode;

    public LinkedQueue() {
        firstNode = null;
        lastNode = null;
    }


    @Override
    public T getFront() {
        if (isEmpty()) {
            return null;
        }
        return firstNode.getData();
    }


    @Override
    public boolean isEmpty() {
        return firstNode == null;
    }


    @Override
    public void clear() {
        firstNode = null;
        lastNode = null;

    }

    public class Node<E> {
        private E data; // Entry in bag
        private Node<E> next; // Link to next node

        public Node(E dataPortion) {
            this(dataPortion, null);
        } // end constructor


        public Node(E dataPortion, Node<E> nextNode) {
            data = dataPortion;
            next = nextNode;
        } // end constructor


        public E getData() {
            if (data != null) {
                return data;
            }
            return null;
        }


        public Node<E> getNext() {
            return next;
        }


        public void setNext(Node<E> newNext) {
            next = newNext;
        }

} // end LinkedQueue

Your task is to create an "almostPriority" enqueue method. If something is enqueued, it normally goes to the back of the queue, but in this method it will be set immediately after the first element in the queue.

For example, if you had a linked queue that looked like:

(firstNode) 1 --> 2 --> 3 (lastNode)

and you ran almostPriorityEnqueue(4)

the result would be:

(firstNode) 1 --> 4 --> 2 --> 3 (lastNode)

 
@SuppressWarnings("unchecked")
public void almostPriorityEnqueue(T newEntry) {
    Node<T> newNode = new Node<T>(newEntry);
    if (isEmpty()) {
        lastNode = newNode;
        firstNode = newNode;
    }
 
 
 
}

Homework Answers

Answer #1

Here is the completed code for each of the required methods for questions 1, 2 and 3. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks



//enqueue method for question 1
public void enqueuePractice(T newEntry) {
        // creating a new node
        Node<T> newNode = new Node<T>(newEntry);
        // if queue is empty, adding as both first and last node
        if (isEmpty()) {
                firstNode = newNode;
                lastNode = newNode;
        } else {
                // otherwise appending next of last node and setting as new last
                // node.
                lastNode.setNext(newNode);
                lastNode = newNode;
        }

}

// priorityEnqueue method for question 2
public void priorityEnqueue(T newEntry) {
        Node<T> newNode = new Node<T>(newEntry);
        // if queue is empty, adding as both first and last node
        if (isEmpty()) {
                firstNode = lastNode = newNode;
        } else {
                // otherwise adding before current firstNode and setting as new
                // firstNode
                newNode.setNext(firstNode);
                firstNode = newNode;
        }
}

// almostPriorityEnqueue method for question 3
@SuppressWarnings("unchecked")
public void almostPriorityEnqueue(T newEntry) {
        Node<T> newNode = new Node<T>(newEntry);
        // if queue is empty, adding as both first and last node
        if (isEmpty()) {
                lastNode = newNode;
                firstNode = newNode;
        } else {
                // adding between firstNode and firstNode.next
                newNode.setNext(firstNode.getNext());
                firstNode.setNext(newNode);
                // if next is null, setting newNode as last node.
                if (newNode.getNext() == null) {
                        lastNode = newNode;
                }
        }

}
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
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
Given this definition of a generic Linked List node: public class LLNode {     private T...
Given this definition of a generic Linked List node: public class LLNode {     private T data;     private LLNode next;     public LLNode(T data, LLNode next) {           this.data = data;           this.next = next;     }     public void setNext(LLNode newNext){ next = newNext; }     public LLNode getNext(){ return next; }     public T getData() {return data;} } Write the findMinimumNode method body. This method returns the linked list node that contains the minimum value in the...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
based on the code below, answer the questions Question 1: The LinkedList class uses another class...
based on the code below, answer the questions Question 1: The LinkedList class uses another class called Node (which is defined inside LinkedList.java). What are the fields in the Node class? Question 2: The Node class uses generics. Why? Question 3: The linkFirst method contains the following lines of code: if (f == null) last = newNode; else f.prev = newNode; What is the value of the linked list’s size attribute at this point in the code if f ==...
The class CourseList implements a simple database of students registered in a course. Complete the method...
The class CourseList implements a simple database of students registered in a course. Complete the method countAttendees in the code below such that it returns the number of students in the instance of CourseList who have not attended any classes. Your response may be written in Java or pseudocode. Your code cannot modify the linked list. The class Node is similar to the linked list node object discussed in lecture. You can use helper methods, but this is not necessary....
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...