Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pt.ipleiria.estg.dei.aed.colecoes.iteraveis.lineares.naoordenadas.estruturas;
- import pt.ipleiria.estg.dei.aed.colecoes.iteraveis.IteradorIteravelDuplo;
- import pt.ipleiria.estg.dei.aed.colecoes.iteraveis.lineares.naoordenadas.ColecaoIteravelLinearNaoOrdenada;
- import java.io.Serializable;
- import java.util.NoSuchElementException;
- public class ListaDuplaCircularBaseNaoOrdenada<T> implements ColecaoIteravelLinearNaoOrdenada<T> {
- protected No base;
- protected int numeroElementos;
- protected class No implements Serializable {
- private static final long serialVersionUID = 1;
- protected No seguinte;
- protected No anterior;
- protected T elemento;
- public No() {
- this.elemento = null;
- this.anterior = this;
- this.seguinte = this;
- }
- public No(T elemento, No seguinte) {
- this.elemento = elemento;
- this.seguinte = seguinte;
- this.anterior = seguinte.anterior;
- seguinte.anterior = anterior.seguinte = this;
- }
- }
- protected class Iterador implements IteradorIteravelDuplo<T>{
- protected No atual;
- public Iterador() {
- reiniciar();
- }
- @Override
- public void reiniciar() {
- this.atual = base;
- }
- @Override
- public boolean podeRecuar() {
- if (atual.anterior != base){
- return true;
- }else{
- return false;
- }
- }
- @Override
- public T recuar() {
- if (!podeRecuar()){
- return null;
- }
- atual = atual.anterior;
- return atual.elemento;
- }
- @Override
- public T corrente() {
- if (!(atual == base)){
- return atual.elemento;
- }
- return atual.elemento;
- }
- @Override
- public boolean podeAvancar() {
- if (atual.seguinte != base){
- return true;
- }else{
- return false;
- }
- }
- @Override
- public T avancar() {
- if (!podeAvancar()){
- return null;
- }
- atual = atual.seguinte;
- return atual.elemento;
- }
- }
- public ListaDuplaCircularBaseNaoOrdenada() {
- base = new No();
- this.numeroElementos = 0;
- }
- protected No getNo(int indice){
- if (indice < 0 || indice >= numeroElementos) {
- throw new IndexOutOfBoundsException("Indice inválido");
- }
- No auxiliar = null;
- int contador = 0;
- if (indice < numeroElementos/2){ //procurar do primeiro para o ultimo
- auxiliar = base.seguinte;
- for (int i = 0; i < indice; i++) {
- auxiliar = auxiliar.seguinte;
- contador++;
- if (contador == indice) {
- return auxiliar;
- }
- }
- }else{ //procurar do ultimo para o primeiro
- auxiliar = base.anterior;
- contador = numeroElementos;
- for (int i = numeroElementos-1; i > indice; i--) {
- auxiliar = auxiliar.anterior;
- contador--;
- if (contador == indice) {
- return auxiliar;
- }
- }
- }
- return null;
- }
- private No getNo(T elem){
- No auxiliar = base.seguinte;
- while(auxiliar != null){
- if (auxiliar.elemento.equals(elem)){
- return auxiliar;
- }
- auxiliar = auxiliar.seguinte;
- }
- throw new NoSuchElementException("Elemento não existe");
- }
- private No getNoPorRefencia(T elem){
- No auxiliar = base.seguinte;
- while(auxiliar != null){
- if (auxiliar.elemento == elem){
- return auxiliar;
- }
- auxiliar = auxiliar.seguinte;
- }
- throw new NoSuchElementException("Elemento não existe");
- }
- private T removerNo(No no) {
- no.anterior.seguinte = no.seguinte;
- no.seguinte.anterior = no.anterior;
- return no.elemento;
- }
- @Override
- public void inserirNoInicio(T elem) {
- new No(elem, base.seguinte);
- numeroElementos++;
- }
- @Override
- public void inserirNoFim(T elem) {
- new No(elem, base);
- numeroElementos++;
- }
- @Override
- public void inserir(int indice, T elem) {
- if (indice == numeroElementos){
- inserirNoFim(elem);
- }else{
- new No(elem, getNo(indice));
- numeroElementos++;
- }
- }
- @Override
- public T removerDoInicio() {
- numeroElementos--;
- return removerNo(base.seguinte);
- }
- @Override
- public T removerDoFim() {
- numeroElementos--;
- return removerNo(base.anterior);
- }
- @Override
- public T remover(T elem) {
- numeroElementos--;
- return removerNo(getNo(elem));
- }
- @Override
- public T remover(int indice) {
- numeroElementos--;
- return removerNo(getNo(indice));
- }
- @Override
- public T removerPorReferencia(T elem) {
- numeroElementos--;
- return removerNo(getNoPorRefencia(elem));
- }
- @Override
- public T consultar(int indice) {
- return getNo(indice).elemento;
- }
- @Override
- public boolean contem(T elem) {
- try{
- getNo(elem);
- return true;
- }catch(Exception e){
- return false;
- }
- }
- @Override
- public boolean contemReferencia(T elem) {
- try{
- getNoPorRefencia(elem);
- return true;
- }catch(Exception e){
- return false;
- }
- }
- @Override
- public IteradorIteravelDuplo<T> iterador() {
- return new Iterador();
- }
- @Override
- public int getNumeroElementos() {
- return numeroElementos;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement