implementacja linkedlist - niepoprawne dodawanie węzła na początku listy

0

Mam za zadanie zaimplementować listę jednokierunkową i mam problem taki,że po wywołaniu metody addFirst i addAtPosition nie dodaje mi na początku listy tylko na jej końcu.
Oto mój kod:

public class ListOne<T> implements IList<T>  {
        
    private ElemOne poczatek;
    private ElemOne koniec;
    private ElemOne next;
    private T zawiera;
    int size;
    public ListOne() 
    {
        poczatek=null;
        koniec=null;
        size=0;
    }
    public boolean czyPusta()
    { 
        return (poczatek == null);
    }    
     public void addFirst(T newData)
     {
         ElemOne wezel=new ElemOne(newData,poczatek);
         if(czyPusta())
         {
             koniec=wezel;
             poczatek=wezel;
         }
          else {  
   ElemOne temp = poczatek;
   
   while(temp.getNext() != null) {
    temp = temp.getNext();
   }
   ElemOne newNode = new ElemOne(newData,null);
   temp.setNext(newNode);
  }
          
    }
     
         
    public void addLast(T newData)
    {
        ElemOne newWezel = new ElemOne(newData,null);
        if(czyPusta())
        {
            poczatek = newWezel;
            koniec=newWezel;
        }
        else{
            koniec.next=newWezel;
            koniec=newWezel;
        }
        
    
            
        
    }        
    public void addAtPosition(T newData, int position) throws ListException
    {
        if ((position < 0) || (position > size)) 
              throw new ListException("zly index");
    
    if (position == 0) {
      addFirst(newData);
    } 
    size++;
    }    
    public int size()
    {
        return size;
    }    
   // public T removeFirst() throws ListException
    {
    /*    if (czyPusta())
        {
            throw new ListException("lista jest pusta!!!!");
        }
        else
        {
            ElemOne pomoczniczy = poczatek;
            poczatek = poczatek.getNext();
            return pomoczniczy;
        }*/
    }    
  //  public T removeLast() throws ListException
    {
        
    }
  //  public T remove(int position) throws ListException
    {
        
    }

    
   // public int find(T dataToFind)
    {
        
    }        
  /* public boolean contains(T data)
    {
         for(int i = 1;i<=size();i++) {
            if(getData(i).equals(data)) {
                return true;
            }
        }
        return false;
    }    */
    public void print()
    {
         int count = 0;
  
  if(poczatek == null) {
   System.out.println("\n List is empty !!");
  } else {
   ElemOne temp = poczatek;
   
   while(temp.getNext() != null) {
    System.out.println("count("+count+") , node value="+temp.getData());
    count++;
    temp = temp.getNext();
   }
  }
    }
   
    
    
    public static void main(String[] args) {
       ListOne aa=new ListOne();
       System.out.println(aa.czyPusta());
      try{
           
           
        aa.addLast("sad");
        aa.addFirst("baz");
        aa.addFirst("foo");
        aa.print();
        aa.addAtPosition("aaaaaaa",0);
        aa.print();
        
        
      }catch(ListException e){e.printStackTrace();}
    System.out.println(aa.czyPusta());
        
    //aa.print(aa.getNext());
    
    
    }
}

 

i klasa ElemOne:

 
public class ElemOne<T> {
    private T data;
     ElemOne next;

    public ElemOne(T data) {
        this.data = data;
    }

    public ElemOne(T data, ElemOne next) {
        this.data = data;
        this.next = next;
    }
     public ElemOne() {
        this.data = data;
        this.next = next;
    }
    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public ElemOne getNext() {
        return next;
    }

    public void setNext(ElemOne next) {
        this.next = next;
    }
    
}

Z góry dzięki za pomoc

0
  1. Czy słyszałeś o formatowaniu kodu?
  2. Po kiego masz to:
 private ElemOne next;
 private T zawiera;

?
3. addFirst masz totalnie przekombinowany:

public void addFirst(T newData)
  {
   poczatek=new ElemOne(newData,poczatek);
   if(koniec==null) koniec=poczatek;
  }
  1. Użycie addFirst przez addAtPosition jest totalnym nieporozumieniem.
0

To powiedz to mojej pani Doktor,że to nieporozumienie :D

0

Dobra już sobie poradziłem,mam teraz inny problem chcę usunąć element pod podanym indeksem
Oto co udało mi się napisać:

  public T remove(int position) throws ListException
    {
        if (position < 0 || position > size())
            throw new ListException("Podano zły index");
        else     
        {
            ElemOne temp = poczatek; //start at the beginning of the list
     
            //iterate to the position before the index
            for (int i = 0; i < position; i++) {
                temp = temp.next;
            }
            ElemOne two = temp.next;
     
    //set temp.next to point to the Node next to the Node to be removed
            temp.next = two;
            temp=temp.next;
           
        }
        return zawiera;
    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1