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

Need help with a programming problem using Processing: Description You will impl

ID: 3823545 • Letter: N

Question

Need help with a programming problem using Processing:

Description You will implement and submit a doubly link list, with the following requirements. Requirements: You MUST to implement the following requirements to your doubly link list Class Node o int value o Node next, prev Holds the next and previous nodes in the list. Class Link List o Node head Holds the node at the front of the list o Node tail Holds the node at the end of the list o int size() returns a number nodes in the list o void print() prints all the values of all the nodes in the list o void AddTo Front (Node n) adds a node 'n' to the front of the list

Explanation / Answer

hi, below is the complete doubly linked list implementation in java.

public class DoublyLinkedListImpl<E> {

/**

    * this class keeps track of each element information

    *

    * @author vishal

    *

    */

   private class Node {

       E element;

       Node next;

       Node prev;

       public Node(E element, Node next, Node prev) {

           this.element = element;

           this.next = next;

           this.prev = prev;

       }

   }

   // variables

   private Node head;

   private Node tail;

   private int size;

   public DoublyLinkedListImpl() {

       size = 0;

   }

   /**

    * returns the size of the linked list

    *

    * @return

    */

   public int size() {

       return size;

   }

   /**

    * return whether the list is empty or not

    *

    * @return

    */

   public boolean isEmpty() {

       return size == 0;

   }

   /**

    * adds element at the starting of the linked list

    *

    * @param element

    */

   public void addFirst(E element) {

       Node tmp = new Node(element, head, null);

       if (head != null) {

           head.prev = tmp;

       }

       head = tmp;

       if (tail == null) {

           tail = tmp;

       }

       size++;

       System.out.println("adding: " + element);

   }

   /**

    * adds element at the end of the linked list

    *

    * @param element

    */

   public void addLast(E element) {

       Node tmp = new Node(element, null, tail);

       if (tail != null) {

           tail.next = tmp;

       }

       tail = tmp;

       if (head == null) {

           head = tmp;

       }

       size++;

       System.out.println("adding: " + element);

   }

   /**

    * this method walks forward through the linked list

    */

   public void print() {

       System.out.println("iterating forward..");

       Node tmp = head;

       while (tmp != null) {

           System.out.println(tmp.element);

           tmp = tmp.next;

       }

   }

   /* Function to delete node at position */

   public void remove(int pos) {

       if (pos == 0) {

           if (size == 1) {

               head = null;

               tail = null;

               size = 0;

               return;

           }

           head = head.next;

           head.prev = null;

           size--;

           return;

       }

       if (pos == size) {

           tail = tail.prev;

           tail.next = null;

           size--;

       }

       Node ptr = head.next;

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

           if (i == pos) {

               Node p = ptr.prev;

               Node n = ptr.next;

               p.next = n;

               n.prev = p;

               size--;

               return;

           }

           ptr = ptr.next;

       }

   }

   public static void main(String a[]) {

       DoublyLinkedListImpl<Integer> dll = new DoublyLinkedListImpl<>();

       dll.addFirst(10);

       dll.addFirst(34);

       dll.addLast(56);

       dll.addLast(364);

       dll.print();

       dll.remove(1);

       dll.print();

   }

}


Hope you understand the concept behind the code.

If in case of doubt feel free to comment here. I’ll be very happy to help you.