X700: LinkedQueueEnqueue

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

x
 
1
2
public static final class LinkedQueue<T> implements QueueInterface<T> {
3
    private Node firstNode;
4
    private Node lastNode;
5
6
    public LinkedQueue() {
7
        firstNode = null;
8
        lastNode = null;
9
    }
10
11
12
    @Override
13
    public T getFront() {
14
        if (isEmpty()) {
15
            return null;
16
        }
17
        return firstNode.getData();
18
    }
19
20
21
    @Override
22
    public boolean isEmpty() {
23
        return firstNode == null;
24
    }
25
26
27
    @Override
28
    public void clear() {
29
        firstNode = null;
30
        lastNode = null;
31
32
    }
33
34
    public class Node{
35
        private T data; // Entry in bag
36
        private Node next; // Link to next node
37
38
        public Node(T dataPortion) {
39
            this(dataPortion, null);
40
        } // end constructor
41
42
43
        public Node(T dataPortion, Node nextNode) {
44
            data = dataPortion;
45
            next = nextNode;
46
        } // end constructor
47
48
49
        public T getData() {
50
            if (data != null) {
51
                return data;
52
            }
53
            return null;
54
        }
55
56
57
        public Node getNext() {
58
            return next;
59
        }
60
61
62
        public void setNext(Node newNext) {
63
            next = newNext;
64
        }
65
66
} // end LinkedQueue

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

Your Answer:

xxxxxxxxxx
8
 
1
public void enqueuePractice(T newEntry) {
2
    Node<T> newNode = new Node<T>(newEntry);
3
    if (isEmpty()) {
4
        firstNode = newNode;
5
    }
6
    
7
}
8

Feedback

Your feedback will appear here when you check your answer.