Cap 6 - Vets Listas e Seqs-4spp

Getnext element headergetnextsetprevnewnode

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: mpty"); return trailer.getPrev(); } public Position before(Position p) throws InvalidPositionException, BoundaryViolationException { // O(1) time DNode v = checkPosition(p); DNode prev = v.getPrev(); if (prev == header) throw new BoundaryViolationException ("Cannot advance past the beginning of the list"); return prev; } public Position insertBefore(Position p, Object element) throws InvalidPositionException { // O(1) time DNode v = checkPosition(p); numElts++; DNode newNode = new DNode(v.getPrev(), v, element); v.getPrev().setNext(newNode); v.setPrev(newNode); return newNode; } public Position insertFirst(Object element) { // O(1) time numElts++; DNode newNode = new DNode(header, header.getNext(), element); header.getNext().setPrev(newNode); header.setNext(newNode); return newNode; } 23 • Estrutura de Dados • Estrutura de Dados Implementação Java (parcial) public class NodeList implements List { protected int numElts; // Number of items in the list protected DNode header, trailer; // Special sentinels // Constructor; O(1) time public NodeList() { numElts = 0; header = new DNode(null, null, null); // create header trailer = new DNode(header, null, null); // create trailer header.setNext(trailer); // make header and trailer point to each other } // Convenience function; O(1) time protected DNode checkPosition(Position p) throws InvalidPositionException { if (p == null) throw new InvalidPositionException ("Null Position passed to NodeList."); if (p == header) throw new InvalidPositionException ("The header node is not a valid position"); if (p == trailer) throw new InvalidPositionException ("The trailer node is not a valid position"); try { DNode temp = (DNode)p; if ((temp.getPrev() == null) || (temp.getNext() == null)) throw new InvalidPositionException ("Position does not belong to a valid NodeList"); return temp; } catch (ClassCastException e) { throw new InvalidPositionException ("Position is of wrong type f...
View Full Document

Ask a homework question - tutors are online