alexnaita Posted November 20, 2015 Report Posted November 20, 2015 Salut , am urmatorul fragment de cod dar tipului la care l-am prezentat nu stiu ce nu-i place si zice ca inloc de "nod2.deleteNext();" sa pun un "for" pt a face lista sa ruleze in mod repetitiv. Are cineva vreo idee ?Multumesc , codul este asta : public class TestL { public static void manin(String[] args) { Linked first= new Linked(3); Linked nod2= new Linked(-5); Linked nod3= new Linked(7); first.addEnd(nod2); nod2.addEnd(nod3); System.out.print(first); System.out.print(first.getAdr()); System.out.print(nod2.getAdr()); //System.out.println(first.getAdr().getAdr()); Linked nou=new Linked(10); nod2.addAfter(nou); System.out.println(); System.out.print(first); System.out.print(first.getAdr()); System.out.print(nod2.getAdr()); System.out.print(nod2.getAdr().getAdr()); //System.out.print(nou.getAdr()); Linked nod1=new Linked(0); first.addStart(nod1); System.out.println(); System.out.print(nod1); System.out.print(nod1.getAdr()); System.out.print(nod2.getAdr()); System.out.print(nod2.getAdr().getAdr()); nod2.deleteNext(); System.out.println(); System.out.print(nod1); System.out.print(nod1.getAdr()); System.out.print(nod2.getAdr()); System.out.print(nod2.getAdr().getAdr()); }} Quote
tjt Posted November 25, 2015 Report Posted November 25, 2015 Eu nu am mai auzit pana acum la liste de metoda DeleteNext(). Mai exact ce face aceasta metoda ?La liste exista o metoda numita delete(Node head, Node n) sau delete(Node n) care sterge nodul n din lista ce are ca prim element head, adica capul listei.Uite un exemplu aici:Node delete(Node head, Node n){ Node temp = head; if(temp.data == n.data){ return head.next; } while(temp.next != null){ if(temp.next.data == n.data){ temp.next = temp.next.next; return head; } temp = temp.next; } return head;} Quote
gogusan Posted November 25, 2015 Report Posted November 25, 2015 @tjtdeleteNext() este o functie creata de el dar care s-a pierdut in eter ceva de genul: public void removeNext(SinglyLinkedListNode previous) { if (previous == null) removeFirst(); else if (previous.next == tail) { tail = previous; tail.next = null; } else if (previous == tail) return; else { previous.next = previous.next.next; } }?sursa Quote