Question: Your task is to complete the Java program of the DoublyLinkedList.java file in order to get the output of the Devoir5_Output.txt. The DoublyLinkedList class implements
Your task is to complete the Java program of the DoublyLinkedList.java file in order to get the output of the Devoir5_Output.txt.
The DoublyLinkedList class implements a doubly linked list. An inner class DoublyLinkedListIterator implements an iterator that allows iterating through the elements of the doubly linked list.
The DoublyLinkedList class implements the List interface which defines the classic methods of a list. The particularity of the list interface is that it defines functions that we will implement recursively (such as the functions rec_get, rec_remove and rec_size)
interface List { boolean add(E elem); E get(int index); boolean remove(E elem); intsize(); boolean isEmpty(); E rec_get(int index); boolean rec_remove(E elem); int rec_size(); } We will test the program with an argument = 10.
This is theDevoir5_Output.txt which should be your output
10 Adding the square of each index 0 1 4 9 16 25 36 49 64 81 (size = 10) (rec_size = 10) 1 4 9 16 25 36 49 64 81 (size = 9) (rec_size = 9) 1 4 9 16 25 36 49 64 (size = 8) (rec_size = 8) 1 4 9 16 36 49 64 (size = 7) (rec_size = 7) 1 4 9 16 36 49 64 70 (size = 8) (rec_size = 8) 1 4 9 16 36 49 64 70 80 (size = 9) (rec_size = 9) 1 4 9 16 36 64 70 80 (size = 8) (rec_size = 8) Elment index 0: 1 Elment index 1: 4 Elment index 2: 9 Elment index 3: 16 Elment index 4: 36 Elment index 5: 64 Elment index 6: 70 Elment index 7: 80 0 1 4 9 16 25 36 49 64 81 (size = 10) (rec_size = 10) 1 4 9 16 25 36 49 64 81 (size = 9) (rec_size = 9) 1 4 9 16 25 36 49 64 (size = 8) (rec_size = 8) 1 4 9 16 36 49 64 (size = 7) (rec_size = 7) 1 4 9 16 36 49 64 70 (size = 8) (rec_size = 8) 1 4 9 16 36 49 64 70 80 (size = 9) (rec_size = 9) 1 4 9 16 36 64 70 80 (size = 8) (rec_size = 8) Elment index 0: 1 Elment index 1: 4 Elment index 2: 9 Elment index 3: 16 Elment index 4: 36 Elment index 5: 64 Elment index 6: 70 Elment index 7: 80
And this is the code you have to adjust to release that output.
import java.util.Iterator; import java.util.NoSuchElementException; import java.lang.NullPointerException; import java.lang.IndexOutOfBoundsException; interface List { boolean add(E elem); E get(int index); boolean remove(E elem); int size(); boolean isEmpty(); E rec_get(int index); boolean rec_remove(E elem); int rec_size(); } public class DoublyLinkedList implements List{ private int n; // Le nombre des lments dans la liste private Node head; private Node factice; // Le noeud fictif public DoublyLinkedList() { n = ?; factice = ?; factice.next = ?; factice.prev = ?; head = ?; } // La classe Node private class Node { private E value; private Node next; private Node prev; } public boolean isEmpty() { return n == ?; } public int size() { return ?; } // Ajouter un lment la liste public boolean add(E elem) { if (elem == null) { throw new ?; } Node last = ?; Node x = ?; x.value = ?; x.next = ?; x.prev = ?; factice.prev = ?; last.next = ?; n?; return ?; } public E get(int index) { if (index < 0 || index >= n ) { throw new ?; } Node current = ?; for (int i = 0; i <= ?; i++) { current = ?; } return ?; } public boolean remove(E elem) { if (elem == null) { throw new ?; } Node current = ?; for (int i = 0; i < n; i++) { if (current.value.equals(?)) { current.prev.next = ?; current.next.prev = ?; n?; return ?; } current = ?; } return ?; } public E rec_get(int index) { if (index < ?) { throw new ?; } return rec_get(?, ?); } private E rec_get(int index, Node current) { if (current.next == ?) { throw new ?; } if (index == ?) { return ?; } return rec_get(?, ?); } public boolean rec_remove (E elem) { if (elem == ?) { throw new ?; } return rec_remove(?, ?); } private boolean rec_remove (E elem, Node current) { if (current.next == ?) { throw new ?; } if (current.next.value.equals(?)) { current.next = ?; current.next.prev = ?; n?; return ?; } else { return rec_remove(?, ?); } } private int rec_size(Node current){ if (current.next == ?) { return ?; // cas de base } return 1 + ?; } public int rec_size() { return rec_size(?); } public Iterator iterator() { return new ?; } private class DoublyLinkedListIterator implements Iterator { private Node current = ?; // Le noeud retourner par la mthode next private int index = ?; public boolean hasNext() { return index < ?; } public E next() { if (!hasNext()) throw new ?; E value = ?; current = ?; index?; return ?; } } public String toString() { StringBuilder s = new StringBuilder(); Iterator it = this.iterator(); while(it.hasNext()) s.append(it.next() + " "); s.append(" (size = " + n +")"); s.append(" (rec_size = " + rec_size() +")"); return s.toString(); } // Fonction main pour tester les mthodes de la classe public static void main(String[] args) { int n = Integer.parseInt(args[0]); /*****************TRAITEMENT ITERATIF**************************/ // Ajouter les lments 1, ..., n System.out.println(n + " Adding the square of each index"); DoublyLinkedList list = new DoublyLinkedList(); for (int i = 0; i < n; i++) list.add(i*i); System.out.println(list); list.remove(0); System.out.println(list); list.remove(81); System.out.println(list); list.remove(25); System.out.println(list); list.add(70); System.out.println(list); list.add(80); System.out.println(list); list.remove(49); System.out.println(list); for (int i = 0; i < list.size(); i++) { System.out.println("Elment index "+ i + ": " + list.get(i)); } /*****************TRAITEMENT RECURSIF**************************/ list = new DoublyLinkedList(); for (int i = 0; i < n; i++) list.add(i*i); System.out.println(list); list.rec_remove(0); System.out.println(list); list.rec_remove(81); System.out.println(list); list.rec_remove(25); System.out.println(list); list.add(70); System.out.println(list); list.add(80); System.out.println(list); list.rec_remove(49); System.out.println(list); for (int i = 0; i < list.rec_size(); i++) { System.out.println("Elment index "+ i + ": " + list.rec_get(i)); } } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
