Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

All code should be properly documented, commented and have proper unit testing i

ID: 3746582 • Letter: A

Question

All code should be properly documented, commented and have proper unit testing implemented in main(). Explain ALL lines of code. Assume that the data stored in each element is a character or integer (you may choose whichever you think is more convenient) All JAVA implementations should implement a method returning string representation of the list queue where each element is placed between brackets "[x]" and adjacent elements are separated by a comma "," aing ba comnre ech elen mracies You are not allowed to use Java library implementations of the data structures (queues, lists etc.) You should implement the internals yourself! Question: In JAVA implement a generic iterable FIFO-queue based on a double linked list.

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// DoublyLinkedQueue.java

import java.util.Iterator;

public class DoublyLinkedQueue<T> implements Iterable<T> {

      // defining required attributes

      private DoublyNode<T> front;

      private DoublyNode<T> back;

      private int size;

      /**

      * default constructor

      */

      public DoublyLinkedQueue() {

            front = null;

            back = null;

            size = 0;

      }

      /**

      * method to push an item to the back of the queue

      *

      * @param item

      *            item to be added

      */

      public void push(T item) {

            // defining a DoublyNode using the item

            DoublyNode<T> node = new DoublyNode<T>(item);

            if (isEmpty()) {

                  // adding as the first item

                  front = node;

                  back = node;

                  size++;

            } else {

                  // appending to the end, and updating back variable

                  back.setNext(node);

                  node.setPrevious(back);

                  back = node;

                  size++;

            }

      }

      /**

      * method to remove and return an element from front

      *

      * @return the removed value, or null if the queue is empty

      */

      public T pop() {

            if (!isEmpty()) {

                  // getting data at front

                  T data = front.getData();

                  // updating front variable

                  front = front.getNext();

                  // decrementing size

                  size--;

                  // returning removed data

                  return data;

            }

            return null;

      }

      /**

      * this method returns true if the queue is empty, else false

      */

      public boolean isEmpty() {

            return size == 0;

      }

      /**

      * returns the current size of queue

      */

      public int size() {

            return size;

      }

      /**

      * returns an iterator, which can be used to iterate through the values in

      * queue, front to back

      */

      @Override

      public Iterator<T> iterator() {

            Iterator<T> iterator = new Iterator<T>() {

                  DoublyNode<T> node = front;

                  @Override

                  public boolean hasNext() {

                        return node != null;

                  }

                  @Override

                  public T next() {

                        T data = node.getData();

                        node = node.getNext();

                        return data;

                  }

                  @Override

                  public void remove() {

                  }

            };

            return iterator;

      }

      /**

      * return a properly formatted String containing all items in queue inside

      * square brackets, seperated by comma

      */

      @Override

      public String toString() {

            String data = "[";

            DoublyNode<T> node = front;

            for (int i = 0; i < size; i++) {

                  data += node.getData();

                  if (i != size - 1) {

                        data += ",";

                  }

                  node = node.getNext();

            }

            data += "]";

            return data;

      }

}

/**

* DoublyNode represents a single node in DoublyLinkedQueue class.

* This should be placed within DoublyLinkedQueue.java file

*/

class DoublyNode<T> {

      T data;

      DoublyNode<T> next;

      DoublyNode<T> previous;

      public DoublyNode(T data) {

            this.data = data;

            next = null;

            previous = null;

      }

      public T getData() {

            return data;

      }

      public void setData(T data) {

            this.data = data;

      }

      public DoublyNode<T> getNext() {

            return next;

      }

      public void setNext(DoublyNode<T> next) {

            this.next = next;

      }

      public DoublyNode<T> getPrevious() {

            return previous;

      }

      public void setPrevious(DoublyNode<T> previous) {

            this.previous = previous;

      }

}

// Test.java (with main method)

import java.util.Iterator;

public class Test {

      public static void main(String[] args) {

            /**

            * testing the DoublyLinkedQueue object and operations

            */

            DoublyLinkedQueue<Character> queue = new DoublyLinkedQueue<Character>();

            System.out.println("Queue size: " + queue.size() + ", should be 0");

            System.out.println("Is empty? " + queue.isEmpty() + ", should be true");

            System.out.println("Pushing A, B, C, D and E to the queue");

            queue.push('A');

            queue.push('B');

            queue.push('C');

            queue.push('D');

            queue.push('E');

            System.out.println("Queue contents, expected : [A,B,C,D,E], actual: "

                        + queue);

            System.out.println("Popping an element, should remove A");

            char c = queue.pop();

            System.out.println(c + " has been removed");

            System.out.println("Popping another element, should remove B");

            c = queue.pop();

            System.out.println(c + " has been removed");

            System.out.println("Current Queue size: " + queue.size()

                        + ", should be 3");

            System.out.println(" Printing remaining items using iterator");

            Iterator<Character> iterator = queue.iterator();

            while (iterator.hasNext()) {

                  System.out.println(iterator.next());

            }

      }

}

/*OUTPUT*/

Queue size: 0, should be 0

Is empty? true, should be true

Pushing A, B, C, D and E to the queue

Queue contents, expected : [A,B,C,D,E], actual: [A,B,C,D,E]

Popping an element, should remove A

A has been removed

Popping another element, should remove B

B has been removed

Current Queue size: 3, should be 3

Printing remaining items using iterator

C

D

E

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote