Dúvida em um método com lista duplamente encadeada

3 respostas
F
public class DLinkedList<E> {
	protected DNode<E> head; // nodo cabeça da lista
	protected DNode<E> tail; // nodo cauda da lista
	protected long size; // número de nodos da lista

	public DLinkedList() {
		size = 0;
		head = tail = null;
	}

	public boolean isEmpty() {
		return head == null;
	}

	public E getFirst() throws UnderflowException {
		if (isEmpty())
			throw new UnderflowException();
		return head.getElement();
	}

	public E getLast() throws UnderflowException {
		if (isEmpty())
			throw new UnderflowException();
		return tail.getElement();
	}

	public void addFirst(E insertItem) {
		DNode<E> n = new DNode<E>(insertItem);
		if (isEmpty()) {
			head = tail = n;
		} else {
			head.setPrevious(n);
			n.setNext(head);
			head = n;
		}
		size++;
	}

	public void addLast(E insertItem) {
		DNode<E> n = new DNode<E>(insertItem);
		if (isEmpty()) {
			head = tail = n;
		} else {
			tail.setNext(n);
			n.setPrevious(tail);
			tail = n;
		}
		size++;
	}

	public E removeFirst() throws UnderflowException {
		if (isEmpty()) {
			throw new UnderflowException();
		}
		E removedItem = head.getElement();
		if (head == tail) {
			head = tail = null;
		} else {
			head = head.getNext();
			head.setPrevious(null);
		}
		size--;
		return removedItem;
	}

	public E removeLast() throws UnderflowException {
		if (isEmpty()) {
			throw new UnderflowException();
		}
		E removedItem = tail.getElement();
		if (head == tail) {
			head = tail = null;
		} else {
			DNode<E> penultimo = tail.getPrevious();
			tail = penultimo;
			tail.setNext(null);
		}
		size--;
		return removedItem;
	}

	public void print() {
		System.out.println("Exibindo a lista:");
		DNode<E> current = head;
		while (current != null) {
			System.out.println(current.getElement());
			current = current.getNext();
		}
	}

	public DNode<E> find(E key) {
		DNode<E> current = head;
		while (current != null) {
			if (current.getElement().equals(key)) {
				return current;
			}
			current = current.getNext();
		}
		return null;
	}

	public boolean addBefore(E el, E chave) {
		DNode<E> current = find(chave);
		if (current == null) {
			return false;
		} else if (current == head) {
			addFirst(el);
			return true;
		} else {
			DNode<E> n2 = new DNode<E>(el);
			DNode<E> before = current.getPrevious();
			before.setNext(n2);
			n2.setPrevious(before);
			n2.setNext(current);
			current.setPrevious(n2);
			return true;
		}
	}



} // end class List
public class DNode<E> {
	private E element;
	private DNode<E> next;
	private DNode<E> previous;

	public DNode(E element) {
		this (element, null, null);
	}
	public DNode(E element, DNode<E> next, DNode<E> previous) {
		super();
		this.element = element;
		this.next = next;
		this.previous = previous;
	}
	public E getElement() {
		return element;
	}
	public void setElement(E element) {
		this.element = element;
	}
	public DNode<E> getNext() {
		return next;
	}
	public void setNext(DNode<E> next) {
		this.next = next;
	}
	public DNode<E> getPrevious() {
		return previous;
	}
	public void setPrevious(DNode<E> previous) {
		this.previous = previous;
	}

	public String toString() {
		return "DNode [element=" + element + "]";
	}
	
	

}

Na classe de Lista Dulplamente Encadeada (DLinkedList), preciso criar um método recursivo para realizar a Busca Binária de um determinado elemento E. Utilizando as seguintes assinaturas (público e privado):

public DNode<E> pesquisaSequencialR (E key) { 
return pesquisaSequencialR(key, head); 
} 

private DNode<E> pesquisaSequencialR(E key, DNode<E> n){

}
O método de pesquisa binária é
public static int pesquisaBinaria (int tab[], int arg) {
int inf, sup, med;
inf = 0; sup = tab.length-1;
while (inf <= sup) {
med = (inf + sup)/2; //divisão inteira
if (arg == tab[med]) return med;
else if (arg > tab[med]) inf = med + 1; // procura na 2a. metade
else if (arg < tab[med]) sup = med - 1; // procura na 1ª metade
}
return -1;
}

Como eu poderia transformá-lo para mexer com esta lista duplamente encadeada?

3 Respostas

F

editei o topico pra nao criar outro…

F

Eu tinha esse tópico antigo, então só editei pra não precisar criar outro.

Se alguém entende aí de busca binária com listas duplamente encadeada me dê um help aí! :stuck_out_tongue:

R

Tente:
http://www.guj.com.br/java/286911-lista-dup-encadeada-com-no-cabeca-perfeita-para-fins-didaticos#1516601

Criado 17 de junho de 2011
Ultima resposta 9 de nov. de 2012
Respostas 3
Participantes 2