Advertisement
mitkomitrov

[JAVA] Избриши ги сите парни од листата

Sep 7th, 2019
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. public static int example5(SLL<Integer> lista)
  2. {
  3. /*
  4. Дадена е линеарно поврзана листа. Да се напише функција која од листата ќе ги
  5. избрише сите елементи кои во info полето имаат парен број. Како резултат функцијата
  6. да враќа колку јазли биле избришани.
  7. */
  8. if(lista.first == null)
  9. {
  10. return 1;
  11. }
  12.  
  13. int i = 0;
  14. SLLNode<Integer> current = lista.first;
  15. SLLNode<Integer> prev = null;
  16.  
  17. while(current != null)
  18. {
  19. if(current.element % 2 == 0)
  20. {
  21. i++;
  22. SLLNode<Integer> sled = current.succ;
  23. if(prev == null)
  24. {
  25. lista.first = sled;
  26. current = lista.first;
  27. }
  28. else
  29. {
  30. prev.succ = sled;
  31. current = prev.succ;
  32. }
  33. }
  34. else
  35. {
  36. prev = current;
  37. current = current.succ;
  38. }
  39. }
  40.  
  41. return i;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement