Date post: | 10-Jan-2016 |
Category: |
Documents |
Upload: | joao-paulo |
View: | 215 times |
Download: | 0 times |
of 25
Estrutura de DadosTAD DequeMaikol M. RodriguesTurmas A e B (UNIFOR)
Gladys Castillo Jordn
Fila de duas Extremidades (Deque) Uma Deque suporta inseres e delees de ambas as extremidades.
The Deque Abstract Data TypeinsertFirst(e): Insero no incio do deque.insertLast(e):Insero no final do dequeremoveFirst():Remove e retorna o primeiro elementoremoveLast(): Remove e retorna o ltimo elemento
Adicionamente pode-se acrescentar os seguintes mtodos:first()last()size()isEmpty()
Gladys Castillo Jordn
Fila de duas Extremidades public interface Deque { public int size();public Object first() throws DequeEmptyException; public Object last() throws DequeEmptyException;public boolean isEmpty(); public void insertLast(Object o); public void insertFirst(Object o); public Object removeLast() throwsDequeEmptyException; public Object removeFist() throwsDequeEmptyException; }
Gladys Castillo Jordn
Fila de duas Extremidades public class DequeEmptyException extends RuntimeException { public DequeEmptyException(String err) { super(err); }}
Gladys Castillo Jordn
Implementando Deque com Lista Duplamente Encadeada (LDE) Deleo do ltimo elemento de uma lista simplesmente encadeada no pode ser feito em tempo constante.Para implementar o Deque utilizaremos uma lista duplamente encadeada com dois ns SENTINELAS.
Gladys Castillo Jordn
Classe DLNode class DLNode { private Object element; private DLNode next, prev;
DLNode() { this(null, null, null); }
DLNode(Object e, DLNode p, DLNode n) { element = e; next = n; prev = p; }
Gladys Castillo Jordn
Classe DLNode
void setElement(Object newElem){element = newElem;}void setNext(DLNode newNext) { next = newNext; }void setPrev(DLNode newPrev) { prev = newPrev; }Object getElement() { return element; }DLNode getNext() { return next; }DLNode getPrev() { return prev; }
}
Gladys Castillo Jordn
Deque usando LDE public class MyDeque implements Deque { DLNode header, trailer; // sentinels int size; // number of elements public MyDeque() { header = new DLNode(); trailer = new DLNode(); header.setNext(trailer); // make header point to trailer trailer.setPrev(header); // make trailer point to header size = 0; }
Gladys Castillo Jordn
Deque usando LDE public boolean isEmpty() { return (size==0); } public Object first() throws DequeEmptyException { if (isEmpty()) throw new DequeEmptyException("Deque is empty."); return header.getNext().getElement(); } public Object last() throws DequeEmptyException { if (isEmpty()) throw new DequeEmptyException("Deque is empty."); return trailer.getPrev().getElement(); }
Gladys Castillo Jordn
Deque usando LDE public void insertFirst(Object o) { DLNode second = header.getNext(); DLNode first = new DLNode(o, header, second); second.setPrev(first); header.setNext(first); size++; }
Gladys Castillo Jordn
Deque usando LDE public Object removeLast() throws DequeEmptyException { if (isEmpty()) throw new DequeEmptyException("Deque is empty."); DLNode last = trailer.getPrev(); Object o = last.getElement(); DLNode secondtolast = last.getPrev(); trailer.setPrev(secondtolast); secondtolast.setNext(trailer); size--; return o; }
Gladys Castillo Jordn
Deque usando LDE (removeLast)Aqui uma visualizao do removeLast().
Gladys Castillo Jordn
Exerccios de Reviso
Gladys Castillo Jordn
Exerccios
Gladys Castillo Jordn
Exerccios
Gladys Castillo Jordn
Deque usando LDE public void insertLast(Object o) { DLNode ant = trailer.getPrev(); DLNode last = new DLNode(o, ant, trailer); trailer.setPrev(last); ant.setNext(last); size++; }
Gladys Castillo Jordn
RemoveFirst public Object removeFirst() throws DequeEmptyException { if (isEmpty()) throw new DequeEmptyException("Deque is empty."); DLNode first = header.getNext(); Object o = first.getElement(); DLNode second = first.getNext(); header.setNext(second); second.setPrev(header); size--; return o; }
Gladys Castillo Jordn
Implementao de Pilha e Fila com DequeStacks with Deques:Queues with Deques:
Gladys Castillo Jordn
Implementao de Pilha com Deque public class DequeStack implements Stack { private MyDeque D; public DequeStack() { D = new MyDeque(); } public int size() { return D.size(); } public boolean isEmpty() { return D.isEmpty(); } public void push(Object obj) { D.insertLast(obj); }
Gladys Castillo Jordn
Implementao de Pilha com Deque public Object top() throws StackEmptyException { try { return D.last(); } catch (DequeEmptyException e) { throw new StackEmptyException("Stack is empty!"); } } public Object pop() throws StackEmptyException { try { return D.removeLast(); } catch (DequeEmptyException e) { throw new StackEmptyException("Stack is empty!"); } }
Gladys Castillo Jordn
Implementao de Pilha com Deque public static void main(String args[]) { DequeStack A1 = new DequeStack(); Character b = new Character('A'); Integer c = new Integer(9); A1.push(new Integer(3)); A1.push(b); A1.push(c); System.out.println(A1.pop() + "\n" + A1.pop() + "\n" + A1.pop() + "\n"); if (A1.isEmpty()) System.out.println("A Pilha esta VAZIA... "); if (A1.isEmpty()) System.out.println("A Pilha esta VAZIA... "); } }
Gladys Castillo Jordn
Exerccios
Gladys Castillo Jordn
Exerccios
Gladys Castillo Jordn
Exerccios
Gladys Castillo Jordn
Exerccios public static int maior(int[] a, int pos, int maior) { if (pos == a.length) return maior; else if (a[pos] > maior) return maior(a, (pos+1), a[pos]); else return maior(a, (pos+1), maior); }
Gladys Castillo Jordn