Jump to content
alexnaita

Lista inlantuita.

Recommended Posts

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());

}

}

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

@tjt

deleteNext() 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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...