Advertisement
Guest User

list

a guest
Jan 22nd, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class List {
  4. private static class Node {
  5. private Node(int v, Node n) {
  6. val = v;
  7. next = n;
  8. }
  9. private int val;
  10. private Node next;
  11. }
  12. private Node head;
  13.  
  14. public List(int rangeOfRandomNumbers, int amountOfRandomNumbers) {
  15. head = null;
  16. for (int i = 0; i < amountOfRandomNumbers; i++) {
  17. addRandom(rangeOfRandomNumbers);
  18. }
  19. }
  20. public List(boolean) {
  21. head = null;
  22. }
  23. public void add(int i) {
  24. head = new Node(i, head);
  25. }
  26. public void addRandom(int r) {
  27. Random obj = new Random();
  28. add(obj.nextInt() % r);
  29. }
  30. public void del() {
  31. Node h = head;
  32. if (h != null) {
  33. head = head.next;
  34. h = null;
  35. }
  36. }
  37. public void print() {
  38. Node h = head;
  39. while (h != null) {
  40. System.out.print(h.val + " ");
  41. h = h.next;
  42. }
  43. System.out.println();
  44. }
  45. public void reverse() {
  46. Node cp = head;
  47. Node np = head.next;
  48. head.next = null;
  49. Node pp;
  50. while (np != null) {
  51. pp = cp;
  52. cp = np;
  53. np = np.next;
  54. cp.next = pp;
  55. }
  56. head = cp;
  57. }
  58. public void delRep() {
  59. Node outCycle = head;
  60. Node inCycle;
  61. while (outCycle != null && outCycle.next != null) {
  62. inCycle = outCycle;
  63. while (inCycle.next != null) {
  64. if (inCycle.next.val == outCycle.val)
  65. inCycle.next = inCycle.next.next;
  66. else inCycle = inCycle.next;
  67. }
  68. outCycle = outCycle.next;
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement