Advertisement
porteno

Untitled

Nov 27th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Q1 {
  4.  
  5. public static void main(String[] args) {
  6. //calling to method - build and initiaize a Node-List
  7. int[] values = new int[]{4,3,2,6};
  8. Node<Integer> l1 = valuesToList(values);
  9. Node<Integer> l2 = valuesToList(new int[]{10,11,19,1,7,100}); //short way to initialize
  10. Node<Integer> l3 = null;
  11.  
  12. System.out.println("----BEFORE----");
  13. System.out.print("l1: ");
  14. print(l1);
  15. System.out.print("l2: ");
  16. print(l2);
  17.  
  18. Node<Integer> tmp1 = l1;
  19. Node<Integer> tmp2 = l2;
  20.  
  21. int k = 0;
  22. while(tmp1!=null) {
  23. k = tmp1.getValue();
  24. //if k is even
  25. if(k%2==0) {
  26. deleteByPosition(tmp2, k);
  27. }
  28. //if k is odd
  29. else {
  30. Node<Integer> tmp = byPosition(tmp2, k);
  31.  
  32. if(tmp!=null) {
  33. if(l3==null) //1st node in l3-chain
  34. l3 = new Node<Integer>(tmp.getValue());
  35. else
  36. l3.setNext(new Node<Integer>(tmp.getValue()));
  37. }
  38.  
  39. }
  40. tmp1 = tmp1.getNext();
  41. }
  42. System.out.println("-----AFTER----");
  43. System.out.print("l1: ");
  44. print(l1);
  45. System.out.print("l2: ");
  46. print(l2);
  47. System.out.print("l3: ");
  48. print(l3);
  49.  
  50. } //end of main
  51.  
  52. /*
  53. * deletes a node in the given position
  54. * return the altered node-list
  55. */
  56. public static Node<Integer> deleteByPosition(Node<Integer> head, int pos){
  57. Node<Integer> tmp = head;
  58. Node<Integer> previous = byPosition(head, pos-1);
  59. Node<Integer> oneAfter = byPosition(head, pos+1);
  60. if(oneAfter!=null)
  61. previous.setNext(oneAfter);
  62. return tmp;
  63.  
  64. //return previous.setNext(previous.getNext().getNext());
  65. }
  66.  
  67. /*
  68. * return the node in the given position in a node-list
  69. * returns the altered node-list
  70. */
  71. public static Node<Integer> byPosition(Node<Integer> head, int pos){
  72. while(pos>1 && head!=null) {
  73. pos--;
  74. head = head.getNext();
  75. }
  76. return head;
  77. }
  78.  
  79. /*
  80. * prints a given node-list
  81. */
  82. public static void print(Node<Integer> chain) {
  83. while(chain!=null) {
  84. System.out.print(chain.getValue()+"->");
  85. chain=chain.getNext();
  86. }
  87. System.out.print("null"+"\n");
  88. }
  89. /*
  90. * builds a new chain of nodes of random genarted numbers
  91. * input length
  92. */
  93. public static Node<Integer> randomList(int length){
  94. Random rnd = new Random();
  95. int val = rnd.nextInt();
  96. Node<Integer> head = new Node<Integer>(val);
  97. Node<Integer> tmp = head;
  98. while(length>0) {
  99. length--;
  100. val = rnd.nextInt();
  101. tmp.setNext(new Node<Integer>(val));
  102. tmp = tmp.getNext();
  103. }
  104. return head;
  105. }
  106. //--------Util methods to initialize the question--------------------------//
  107.  
  108. /*
  109. * builds a new chain of nodes
  110. * input - array of values
  111. */
  112. public static Node<Integer> valuesToList(int[] values){
  113. Node<Integer> head = new Node<Integer>(values[0]);
  114. Node<Integer> tmp = head;
  115.  
  116. for (int i = 1; i < values.length; i++) {
  117. tmp.setNext(new Node<Integer>(values[i]));
  118. tmp = tmp.getNext();
  119. }
  120. return head;
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement