Advertisement
porteno

Untitled

Nov 24th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1.  
  2.  
  3. public class Run {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. Node<Integer> chain1 = buildChain1();
  8. Node<Integer> chain2 = buildChain2();
  9. Node<Integer> chain3 = buildChain3();
  10. printChain(chain1);
  11. Node<Integer> nd;
  12. while(chain1.hasNext()) {
  13. nd = deleteByValue(chain1.getNext(), chain1.getValue());
  14. nd = nd.getNext();
  15. printChain(chain1);
  16. chain1 = chain1.getNext();
  17. }
  18.  
  19. //Node<Integer> nd = append_By_Value(nd5, node3);
  20.  
  21. //Node<Integer> nd = returnLast(node3);
  22.  
  23.  
  24. }
  25. public static Node<Integer> buildChain1(){
  26. Node<Integer> nd1 = new Node<Integer>(50);
  27. Node<Integer> nd2 = new Node<Integer>(50, nd1);
  28. Node<Integer> nd3 = new Node<Integer>(12, nd2);
  29. Node<Integer> nd4 = new Node<Integer>(12, nd3);
  30. Node<Integer> nd5 = new Node<Integer>(12, nd4);
  31.  
  32. return nd5;
  33. }
  34. public static Node<Integer> buildChain2(){
  35. Node<Integer> nd1 = new Node<Integer>(51);
  36. Node<Integer> nd2 = new Node<Integer>(50, nd1);
  37. Node<Integer> nd3 = new Node<Integer>(20, nd2);
  38. Node<Integer> nd4 = new Node<Integer>(15, nd3);
  39. Node<Integer> nd5 = new Node<Integer>(12, nd4);
  40.  
  41. return nd5;
  42. }
  43.  
  44. public static Node<Integer> buildChain3(){
  45. Node<Integer> node1 = new Node<Integer>(46);
  46. Node<Integer> node2 = new Node<Integer>(44, node1);
  47. Node<Integer> node3 = new Node<Integer>(42, node2);
  48.  
  49. return node3;
  50. }
  51. //deletes the node with the given value in the chain
  52. public static Node<Integer> deleteByValue(Node<Integer> chain, int value){
  53. Node<Integer> tmp = chain.getNext();
  54.  
  55. while(tmp.hasNext() && tmp!=null) {
  56. if(tmp.getNext().getValue() != value) {
  57. chain.setNext(tmp.getNext());
  58. chain = chain.getNext();
  59. tmp = tmp.getNext();
  60. }
  61. tmp = tmp.getNext();
  62. }
  63. if(tmp.getValue()!= value)
  64. chain.setNext(tmp);
  65.  
  66. return chain;
  67. }
  68. //return the last node in given chain
  69. public static void printChain(Node<Integer> first){
  70. while(first.hasNext()) {
  71. System.out.print(first.getValue() + "->");
  72. first = first.getNext();
  73. }
  74. System.out.println(first.getValue() + "->null");
  75.  
  76. }
  77. //return the last node in given chain
  78. public static Node<Integer> returnLast(Node<Integer> first){
  79. while(first.hasNext()) {
  80. System.out.print(first.getValue() + "->");
  81. first = first.getNext();
  82. }
  83. //prints last node
  84. System.out.println(first.getValue()+"->null"+"\n");
  85. return first;
  86. }
  87. public static Node<Integer> append_By_Value(Node<Integer> ndList1,
  88. Node<Integer> ndList2){
  89.  
  90. Node<Integer> tmp1 = ndList1;
  91. Node<Integer> tmp2 = ndList2;
  92. int value1 = ndList1.getValue();
  93. int value2 = ndList2.getValue();
  94.  
  95. //go to last node in the nodeList
  96. while(tmp2.hasNext()) {
  97. tmp2 = tmp2.getNext();
  98. //System.out.println(tmp2.print());
  99. }
  100.  
  101.  
  102. boolean search = true;
  103.  
  104. while(tmp1.hasNext() && search) {
  105. if(value2 < tmp1.getNext().getValue()) {
  106. tmp2.setNext(tmp1.getNext());
  107. tmp1.setNext(ndList2);
  108. search = false;
  109. }
  110. tmp1 = tmp1.getNext();
  111. }
  112.  
  113. if(search)
  114. tmp1.setNext(ndList2);
  115.  
  116.  
  117.  
  118. return ndList1;
  119. }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement