Un de mes amis veut que je convertisse son code en une liste doublement chaînée, même si je ne la connais pas du tout. J'ai recherché des listes doublement liées mais je ne peux pas dire par son code quoi en faire. Je ne suis pas un programmeur maître. Avez-vous des suggestions?Liste de liens simples à une liste doublement chaînée
import java.util.Collection;
import java.util.List;
class SinglyLinkedList<E> implements List<E> {
private class SinglyLinkedListNode<T> {
T data;
SinglyLinkedListNode<T> next;
public SinglyLinkedListNode() {
this(null, null);
}
public SinglyLinkedListNode(T data) {
this(data, null);
}
public SinglyLinkedListNode(T d, SinglyLinkedListNode<T> n) {
data = d;
next = n;
}
public boolean equals(Object o) {
if (data != null && o != null) {
return data.equals(((SinglyLinkedListNode) o).data);
} else {
return (data == null && o == null);
}
}
}
private SinglyLinkedListNode<E> list, last;
private int size;
public SinglyLinkedList() {
clear();
}
public void clear() {
list = last = null;
size = 0;
}
public boolean contains(Object o) {
SinglyLinkedListNode<E> t = list;
while (t != null) {
if (t.data == null) {
if (o == null) {
return true;
}
} else if (t.data.equals(o)) {
return true;
}
t = t.next;
}
return false;
}
public boolean add(E e) {
SinglyLinkedListNode<E> n = new SinglyLinkedListNode<E>(e);
if (isEmpty()) {
list = last = n;
} else {
last = last.next = n;
}
size++;
return true;
}
public void add(int index, E e) {
int currSize = size();
if (index < 0 || index > currSize) {
throw new IndexOutOfBoundsException(
"Index: " + index + ", Size: " + size());
}
if (isEmpty()) // index must == 0
{
list = last = new SinglyLinkedListNode<E>(e);
} else {
if (index == 0) {
list = new SinglyLinkedListNode<E>(e, list);
} else {
SinglyLinkedListNode<E> n = list;
for (int i = 0; i < index - 1; i++) {
n = n.next;
}
n.next = new SinglyLinkedListNode<E>(e, n.next);
if (index == currSize) {
last = n.next;
}
}
}
size++;
}
public boolean equals(SinglyLinkedList<E> e) {
SinglyLinkedListNode<E> e1 = list, e2 = e.list;
try {
for (int i = 1; i <= size(); i++) {
if (!e1.equals(e2)) {
return false;
}
e1 = e1.next;
e2 = e2.next;
}
} catch (NullPointerException ex) {
return false;
}
return true;
}
public E get(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException(
"Index: " + index + ", Size: " + size());
}
SinglyLinkedListNode<E> n = list;
int i = 0;
for (; i < index; i++) {
n = n.next;
}
return n.data;
}
@SuppressWarnings("unchecked")
public int indexOf(Object o) {
SinglyLinkedListNode<E> n = list;
int i = 0;
while (n != null) {
if ((o == null
? (n.data == null)
: (((E) o).equals(n.data)))) {
return i;
}
n = n.next;
i++;
}
return -1;
}
public boolean isEmpty() {
return list == null;
}
public E remove(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException(
"Index: " + index + ", Size: " + size());
}
SinglyLinkedListNode<E> n = list, prevNode = null;
int i = 0;
while (true) {
if (index == i) {
if (n == list) // removing first node
{
list = list.next;
} else {
prevNode.next = n.next;
}
if (n == last) {
last = prevNode;
}
size--;
return n.data;
}
prevNode = n;
n = n.next;
i++;
}
}
@SuppressWarnings("unchecked")
public boolean remove(Object o) {
SinglyLinkedListNode<E> n = list, prevNode = null;
while (n != null) {
if ((o == null
? (n.data == null)
: (((E) o).equals(n.data)))) {
if (n == list) //removing first node
{
list = list.next;
} else {
prevNode.next = n.next;
}
if (n == last) {
last = prevNode;
}
size--;
return true;
}
prevNode = n;
n = n.next;
}
return false;
}
public int size() {
return size;
}
public String toString() {
String s = "((";
SinglyLinkedListNode<E> t = list;
if (t != null) {
while (t.next != null) {
s += t.data + ", ";
t = t.next;
}
s += last.data;
}
return s + "))";
}
Votre ami est-il votre professeur CS? – shoebox639
Est-ce que ce sont les devoirs? Pourquoi votre «ami» ne peut-il pas poster cette question elle-même? – MAK
@ shoebox639 Je souhaite pouvoir vous remettre en question –