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()...
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; } /*...
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,...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
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;...
Used the next seudoucode pseudocode and implement in the next code below run in c ++...
Used the next seudoucode pseudocode and implement in the next code below run in c ++ this code What is the output of the following pseudocode, where num1 , num2 , and num3 are integer variables? num1 = 5 num2 = 1 num3 = 4 aQueue.enqueue(num2) aQueue.enqueue(num3) aQueue.dequeue() aQueue.enqueue(num1 - num2) num1 = aQueue.peek() aQueue.dequeue() num2 = aQueue.peek() aQueue.dequeue() cout << num2 << " " << num1 << " " << num3 << endl ____________________________________________________________________________________ a// Created by Frank M....
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT