Lista que não cresce

0 respostas
S

Todos os valores k adiciono substituem o valor existente na lista, não cria novos valores, apenas cria um nó...o k estou fazendo de errado ?

Output:

List Size: 5 First: 0 Last: 0 Valores: [] A lista não contém o valor 4 A lista não contém o valor 5 Depois de remover o valor 1 [] Depois de remover o primeiro [] Depois de remover o ultimo []
package SegundoTrabalhoSegundaParte;

import java.util.AbstractCollection;
import java.util.Iterator;
import java.util.NoSuchElementException;

@SuppressWarnings("unchecked")
public class DLinkedCollection extends AbstractCollection {
	// Nó da lista duplamente ligada
	private class Node {
		private Object element = null; // Referencia para os dados
		private Node next;             // Referencia para o nó seguinte
		private Node prev;             // Referencia para o nó anterior
		private Node(Object o, Node n, Node p) { element = o; next = n; prev = p; }
		private Node() { next = prev = this; }

	}

	// Iterador da colecção implementada com uma lista duplamente ligada
	private class LinkedIterator implements Iterator {
		//implementar
		private Node current, beforeLast ; //+

		int currentIndex; //+
		DLinkedCollection owner;

		public boolean hasNext() {	//+
			if(current!=null){
				return current.next != null; //+
			}
			return false;
		}

		public boolean hasPrevious() { //+
			if(current!=null){
				return current.prev != null; //+
			}
			return false;
		}


		//implementar(done)
		public Object next() {			//+
			if (!hasNext()) 
				throw new NoSuchElementException();
			//beforeLast = current;
			current = current.next;		//+ não colokei o beforeLast
			currentIndex++;				//+
			return current.element;		//+
		}

		public Object previous() {		//+
			if (!hasPrevious()) 
				throw new NoSuchElementException();
			current = current.prev;  //+
			currentIndex--;  		//+
			return current.element;		//+
		}

		//implementar(done)
		public void remove() {	//+
			if (beforeLast == null) 
				throw new IllegalStateException();
			current = beforeLast ; //+
			owner.removeNode(beforeLast);	//+
			beforeLast =null;	//+
			currentIndex -- ;	//+


		}
		// Implementar (done)
	}

	Node dummy = new Node();
	Node no = new Node();

	int size = 0;

	public int size() {	return size; }

	@SuppressWarnings("unchecked")
	public Iterator iterator() { return new LinkedIterator(); }

	// Adiciona à lista um novo nó depois do nó passado como parâmetro
	private void addNode(Node prev, Object element ) {
		// Implementar (done but )
		
		if (prev == null || dummy == null) 
			throw new IllegalStateException();
		dummy.next = prev.next;
		dummy.prev = prev;
		dummy.next.prev = dummy;
		dummy.prev.next=dummy ;
		dummy.element=element;
		++size;
	}

	// Remove da lista o nó passado como parâmetro
	private void removeNode( Node rem ) {
		dummy = rem;
		dummy.prev.next=dummy.next;
		dummy.next.prev=dummy.prev;
		--size;
	}


	public boolean add(Object element){ 
		addNode(dummy.prev, element); return true; 
	}
	public void addFirst(Object element) { addNode(dummy, element);	}
	public void addLast(Object element)  { addNode(dummy.prev, element); }
	public void removeFirst() {  removeNode(dummy.next); }
	public void removeLast() {  removeNode(dummy.prev); }
	public Object getFirst() {
		if ( size == 0 ) throw new NoSuchElementException();
		return dummy.next.element;
	}
	public Object getLast()  {  
		if ( size == 0 ) throw new NoSuchElementException();
		return dummy.prev.element; 
	}
}
package SegundoTrabalhoSegundaParte;



public class Test {

	public static void escrever( DLinkedCollection l ) {
		if ( l.isEmpty() ) System.out.println("Lista vazia");
		else {
			System.out.println("List" );
			System.out.println("  Size: " + l.size() );
			System.out.println("  First: " + l.getFirst() );
			System.out.println("  Last: " + l.getLast() );
			System.out.println("  Valores: " + l );
		}
	}
	public static void main(String[] args) {
		DLinkedCollection l = new DLinkedCollection();

		for (int i= 1; i < 5; ++ i) 
			l.add( new Integer(i));
		l.addFirst( new Integer(0) );

		escrever( l );
		for (int i= 4; i <= 5; ++ i) 
			if ( l.contains(i)) System.out.println("A lista contém o valor " + i);
			else System.out.println("A lista não contém o valor " + i);
		l.remove(new Integer(1));
		System.out.println("Depois de remover o valor 1 " + l);
		l.removeFirst();
		System.out.println("Depois de remover o primeiro " + l);
		l.removeLast();
		System.out.println("Depois de remover o ultimo " + l);
	}

}
Criado 15 de abril de 2006
Respostas 0
Participantes 1