Implementation of a Linkedlist

Asked

Viewed 1,929 times

0

I’m having trouble implementing a LinkedList for the discipline of Data Structure. The code is giving this error:

Exception in thread "main" java.lang.NullPointerException, and I don’t really understand why. It follows code:

Main Class Program

package br.com.magnoliamedeiros.linkedlist;

import java.util.Iterator;

public class Program {

    public static void main(String[] args){

        LinkedList<String> minhaLista = new LinkedList<String>();

        minhaLista.add("Magnolia");
        minhaLista.add("Maria");

        Iterator<String> it = minhaLista.iterator();
        while(it.hasNext()){
            String s = it.next();
            System.out.println(s);
        }     
    }
}

Node class

package br.com.magnoliamedeiros.linkedlist;

public class Node<T> {
    Node<T> proximo;
    Node<T> anterior;
    T valor;

    public void setProximo(Node<T> proximo){
        this.proximo = proximo;
    }
    public Node<T> getProximo(){
        return proximo;
    }
    public void setAnterior(Node<T> anterior){
        this.anterior = anterior;
    }
    public Node<T> getAnterior(){
        return anterior;
    }
    public void setValor(T valor){
        this.valor = valor;
    }
    public T getValor(){
        return valor;
    }

}

Linkdeklist class

package br.com.magnoliamedeiros.linkedlist;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class LinkedList<T> implements java.util.List<T> {
    private Node<T> inicio;
    private Node<T> fim;

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean contains(Object o) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Iterator<T> iterator() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object[] toArray() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean add(T e) {
        Node<T> newNode = new Node<T>();
        newNode.setValor(e);

        if(inicio == null){
            inicio = newNode;
        }
        if(fim == null){
            fim = newNode;
        }else{
            fim.setProximo(newNode);
            newNode.setAnterior(fim);
            fim = newNode;
        }
        return true;
        //return false;
    }

    @Override
    public boolean remove(Object o) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends T> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean addAll(int index, Collection<? extends T> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub

    }

    @Override
    public T get(int index) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public T set(int index, T element) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void add(int index, T element) {
        // TODO Auto-generated method stub

    }

    @Override
    public T remove(int index) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int indexOf(Object o) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public ListIterator<T> listIterator() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ListIterator<T> listIterator(int index) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<T> subList(int fromIndex, int toIndex) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public <string> string[] toArray(string[] a) {
        // TODO Auto-generated method stub
        return null;
    }


}

Linkedlistiterator class

package br.com.magnoliamedeiros.linkedlist;

public class LinkedListIterator<T> implements java.util.Iterator<T> {
    private Node<T> proximoNode = null;

    LinkedListIterator(Node<T> inicio){
        proximoNode = inicio;
    }

    @Override
    public boolean hasNext() {
        return proximoNode != null;
    }

    @Override
    public T next() {
        T valor = proximoNode.getValor();
        proximoNode = proximoNode.getProximo();
        return valor;
    }
}

1 answer

0


Your method that returns the Iterator is not implemented. It is so in the code you presented:

@Override
public Iterator<T> iterator() {
  // TODO Auto-generated method stub
  return null;
}

So it fits implementation so that your method main work.

By the way, it’s interesting that you put the stack trace entire error, as in this case:

Exception in thread "main" java.lang.NullPointerException
    at padrao.Teste.main(Teste.java:19)
  • I modified: @Override public Iterator<T> iterator() { Return new Linkedlistiterator<T>(start); } Still the error persists... :-(

  • The error that appears is: Exception in thread "main" java.lang.Nullpointerexception at br.com.magnoliamedeiros.linkedlist.Program.main(Program.java:15)

  • @Magshania compiled here with the change you reported in the first comment and it worked correctly. Are you sure you built your program correctly after changing?

  • It worked here too, now the problem is already another, when implementing the remove and run method shows nothing... not even error... follows method:

  • @Magshania if the problem is another you should open another question and close this as solved.

  • All right, I’m sorry, but how do I close?

  • @Magshania mark the V next to the answer that helped you solve the problem

  • Who gave downvote please feel free to comment on the error of the answer so I can correct.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.