DoublyLinkedList_Solution

java

School

University of Ottawa *

*We aren’t endorsed by this school

Course

1521

Subject

Computer Science

Date

Oct 30, 2023

Type

java

Pages

4

Uploaded by LieutenantPencilHummingbird25

Report
import java.util.Iterator; import java.util.NoSuchElementException; import java.lang.NullPointerException; import java.lang.IndexOutOfBoundsException; interface List<E extends Comparable> { 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_Solution<E extends Comparable> implements List<E>{ private int n; // Le nombre des l ments dans la liste � � private Node head; private Node factice; // Le noeud fictif public DoublyLinkedList_Solution() { n = 0; factice = new Node(); factice.next = factice; factice.prev = factice; head = factice; } // La classe Node private class Node { private E value; private Node next; private Node prev; } public boolean isEmpty() { return n == 0; } public int size() { return n; } // Ajouter un l ment la liste � � public boolean add(E elem) { if (elem == null) { throw new NullPointerException("Element null"); } Node last = factice.prev; Node x = new Node(); x.value = elem; x.next = factice; x.prev = last; factice.prev = x; last.next = x; n++; return true; }
public E get(int index) { if (index < 0 || index >= n ) { throw new IndexOutOfBoundsException(index); } Node current = head; for (int i = 0; i <= index; i++) { current = current.next; } return current.value; } public boolean remove(E elem) { if (elem == null) { throw new NullPointerException("Element null"); } Node current = head.next; for (int i = 0; i < n; i++) { if (current.value.equals(elem)) { current.prev.next = current.next; current.next.prev = current.prev; n--; return true; } current = current.next; } return false; } public E rec_get(int index) { if (index < 0) { throw new IndexOutOfBoundsException(index); } return rec_get(index, head); } private E rec_get(int index, Node current) { if (current.next == factice) { throw new IndexOutOfBoundsException(index); } if (index == 0) { return current.next.value; } return rec_get(index - 1, current.next); } public boolean rec_remove (E elem) { if (elem == null) { throw new NullPointerException("Element null"); } return rec_remove(elem, head); } private boolean rec_remove (E elem, Node current) { if (current.next == factice) { throw new NoSuchElementException(); } if (current.next.value.equals(elem)) { current.next = current.next.next; current.next.prev = current;
n--; return true; } else { return rec_remove(elem, current.next); } } private int rec_size(Node current){ if (current.next == factice) { return 0; // cas de base } return 1 + rec_size(current.next); } public int rec_size() { return rec_size(head); } public Iterator<E> iterator() { return new DoublyLinkedListIterator(); } private class DoublyLinkedListIterator implements Iterator<E> { private Node current = head.next; // Le noeud retourner par la m thode next private int index = 0; public boolean hasNext() { return index < n; } public E next() { if (!hasNext()) throw new NoSuchElementException(); E value = current.value; current = current.next; index++; return value; } } public String toString() { StringBuilder s = new StringBuilder(); Iterator<E> 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 m thodes de la classe public static void main(String[] args) { int n = Integer.parseInt(args[0]); /*****************TRAITEMENT ITERATIF**************************/ // Ajouter les l ments 1, ..., n � � System.out.println(n + " Adding the square of each index"); DoublyLinkedList_Solution<Integer> list = new DoublyLinkedList_Solution<Integer>(); for (int i = 0; i < n; i++)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
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("El ment index "+ i + ": " + list.get(i)); } /*****************TRAITEMENT RECURSIF**************************/ list = new DoublyLinkedList_Solution<Integer>(); 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("El ment index "+ i + ": " + list.rec_get(i)); } } }

Browse Popular Homework Q&A

Q: Domain and Range The function f is defined as a set or ordered pairs. Using that set of data,…
Q: Q3 Find all Solutions to 4-7x+11=20 Зеч- following:
Q: Assume the following sales data for a company: Current year $896,005 Preceding year 607,844 What is…
Q: he decline of salmon fisheries along the Columbia River in Oregon has caused great concern among…
Q: The following information is available from the current period financial statements: Net income…
Q: 16. Write the letter of the correct answer. When chlorine occurs in nature, there are three atoms of…
Q: 1) Rank the alkali metal cations (Na+, K+, Rb, and Cs*) from smallest to largest ionic radius. 2)…
Q: 6. Buses arrive at ten minute intervals starting at noon. A man arrives at the bus stop a random…
Q: how to do a mla citation
Q: What quantity of heat (in kJ) will be released if 1.20 mol of SrO is mixed with 0.951 mol of CO₂ in…
Q: Identify each compound's stability relative to each other. (i.e middle, least, and most stable)
Q: y=-x-3 3 y ==x+2 2
Q: Let 8 be an acute angle in a right triangle, where sin( 8) = and cot(8) 1. cos(8)= 2. tan(8)= 3.…
Q: What is the difference between the dark web and the deep web? 1 page
Q: What technological innovations facilitated the process of southernization?
Q: A frame EFG is shown in the figure. All members are prismatic and have equal flexural rigidity. The…
Q: write the equation of the line that satisfies the given condition. Express the final answer in…
Q: How to calculate volume of concentrated solution needed to prepare a diluted solution 1) How many…
Q: One ball of mass m 1.4 kg is being swung in the vertical, circular path with radius of r = 2.9 m. At…
Q: 9.102 For the BiCMOS differential amplifier in Fig. P9.102 let VDD = Vss=3 V, 1 = 0.2 mA, kW/L = 6.4…
Q: 4. What is the upper limit of a brown dwarf’s size? a. no upper limit b. 0.50 solar masses c. 0.10…
Q: Assessment: Is r1 correct? Is r2 correct? Is function mod() used?