Estou utilizando as classes do Prof, nada das coleções padrão do Java. Consigo fazer inserções no inicio e no fim da lista, mas no meio não consigo de forma alguma.
A lógica é a seguinte:
- Se menor que primeiro nodo, então insere antes do primeiro nodo
- Se maior que ultimo nodo, insere após o último nodo
- Se não, corre a lista e insere antes do nodo que seja maior que ele
Dessa forma teria no final uma lista em organizada de forma crescente.
Pra correr a lista eu sei que preciso buscar no Nodo as informações do nodo seguinte e anterior e criar um nodo “atual”, porém não consigo buscar esses dados… Uso a classe lista pra fazer uma lista, e tenho que criar a classe filha listaOrdenada.
Alguém poderia me ajudar a correr a lista duplamente encadeada verificando os valores dos nodos? Qualquer ajuda é bem vinda. Abaixo os códigos:
public class Node {
private String data;
private Node nextNode;
public Node( String element ) {
this( element, null );
}
public Node( String element, Node node ) {
data = element;
nextNode = node;
}
public String getData() {
return data;
}
public void setData(String element){
data = element;
}
public Node getNext() {
return nextNode;
}
public void setNext(Node node) {
nextNode = node;
}
}
public class List {
private Node firstNode;
private Node lastNode;
private String name;
public List() {
this("list");
}
public List(String listName) {
name = listName;
firstNode = lastNode = null;
}
public boolean isEmpty() {
return firstNode == null;
}
public String getFirst() throws UnderflowException {
if (isEmpty()) throw new UnderflowException();
return firstNode.getData();
}
public String getLast() throws UnderflowException {
if (isEmpty()) throw new UnderflowException();
return lastNode.getData();
}
public void insertAtFront (String insertItem) {
if (isEmpty()) {
firstNode = lastNode = new Node(insertItem);
} else {
firstNode = new Node(insertItem, firstNode);
}
}
public void insertAtBack (String insertItem) {
if (isEmpty()) {
firstNode = lastNode = new Node(insertItem);
} else {
lastNode.setNext(new Node(insertItem));
lastNode = lastNode.getNext();
}
}
public String removeFromFront() throws UnderflowException {
if (isEmpty()) {
throw new UnderflowException();
}
String removedItem = firstNode.getData();
if (firstNode == lastNode) {
firstNode = lastNode = null;
} else {
firstNode = firstNode.getNext();
}return removedItem;
}
public String removeFromBack() throws UnderflowException {
if (isEmpty()) {
throw new UnderflowException();
}
String removedItem = lastNode.getData();
if (firstNode == lastNode) {
firstNode = lastNode = null;
} else {
Node current = firstNode;
while (current.getNext() != lastNode) {
current = current.getNext();
} lastNode = current;
current.setNext(null);
}
return removedItem;
}
public void print() {
if (isEmpty()) {
System.out.println("Empty " + name);
} else {
System.out.print("The " + name + " is: ");
Node current = firstNode;
while (current != null) {
System.out.print(current.getData().toString() + " ");
current = current.getNext();
}
System.out.println("\n");
}
}
public class ListTest {
public static void main(String args[]) {
List list = new List();
list.insertAtFront("a");
list.insertAtFront("b");
list.insertAtBack("c");
list.insertAtBack("d");
list.print();
String removedEl;
try {
removedEl = list.removeFromFront();
System.out.println(removedEl.toString() + " removed");
removedEl = list.removeFromFront();
System.out.println(removedEl.toString() + " removed");
removedEl = list.removeFromBack();
System.out.println(removedEl.toString() + " removed");
removedEl = list.removeFromBack();
System.out.println(removedEl.toString() + " removed");
} catch (UnderflowException e) {
System.out.println(e.toString());
}
}
}
