Cap 6 - Vets Listas e Seqs-4spp

Invalidpositionexception public position insertafter

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: . !  Todas as operações executam em tempo O (1). 21 • Estrutura de Dados • Estrutura de Dados !  public interface List { public int size(); public boolean isEmpty(); public boolean isFirst(Position p) throws InvalidPositionException; public boolean isLast(Position p) throws InvalidPositionException; public Position first() throws EmptyContainerException; public Position last() throws EmptyContainerException; public Position before (Position p) throws InvalidPositionException, BoundaryViolationException; public Position after (Position p) throws InvalidPositionException, BoundaryViolationException; public Position insertBefore (Position p, Object element) throws InvalidPositionException; public Position insertAfter (Position p, Object element) throws InvalidPositionException; public Position insertFirst(Object element); public Position insertLast(Object element); public Object remove(Position p) throws InvalidPositionException; public Object replaceElement (Position p, Object element) throws InvalidPositionException; public void setPrev(DNode newPrev) { prev = newPrev; } public void setElement(Object newElement) { element = newElement; } } // A run-time exception for invalid positions public class InvalidPositionException extends RuntimeException { public InvalidPositionException(String err) { super (err); } } public void swapElements (Position a, Position b) throws InvalidPositionException; } 22 Implementação Java (parcial) public Object remove(Position p) throws InvalidPositionException { // O(1) time DNode v = checkPosition(p); numElts--; DNode vPrev = v.getPrev(); DNode vNext = v.getNext(); vPrev.setNext(vNext); vNext.setPrev(vPrev); Object vElem = v.element(); // unlink the position from the list and make it invalid v.setNext(null); v.setPrev(null); return vElem; } // Some Other Methods: public Position first() throws EmptyContainerException { // O(1) time if (isEmpty()) throw new EmptyContainerException("List is empty"); return header.getNext(); } public Position last() throws EmptyContainerException { // O(1) time if (isEmpty()) throw new EmptyContainerException("List is e...
View Full Document

This note was uploaded on 04/02/2012 for the course DC 11 taught by Professor Milton during the Spring '12 term at Alaska Pacific University.

Ask a homework question - tutors are online