Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. public class Node {
  2.  
  3. private int position;
  4. private int value;
  5. private static int count = 0;
  6. private Node head = null;
  7. private Node next = null;
  8.  
  9. public Node( Node tmp) {
  10.  
  11. if(head == null) {
  12. this.head = tmp;
  13. }
  14. else {
  15. Node item = head;
  16. while(item.next != null){
  17. item = item.next;
  18. }
  19. item.next = tmp;
  20. }
  21. }
  22.  
  23. public Node( int position, int value) {
  24.  
  25. this.position = position;
  26. this.value = value;
  27. count++;
  28. }
  29.  
  30. public int getPosition(){
  31. return this.position;
  32. }
  33.  
  34. public int getValue(){
  35. return this.value;
  36. }
  37.  
  38. public void insertPos(int position, int value) {
  39. new Node (new Node (position, value));
  40. }
  41.  
  42. public void deletePos(int position) {
  43. Node item = head;
  44. Node preNode = head; // 9 'un öncesi 7 için pre diyorum.
  45.  
  46. preNode = searchPos(position, item);
  47.  
  48. if(preNode == head) {
  49. head = head.next;
  50. preNode = null;
  51. }
  52. else {
  53. item = preNode.next.next; // 11 i veriyor.
  54. preNode.next = null;
  55. }
  56. }
  57.  
  58. public Node searchPos(int hasElem, Node tmp){ // 3-> 5 -> 7 -> 9 -> 11 -> 13 -> 15 bu id ler içerisinde 9 var mı yok mu
  59. // var ise değer olarak true, yok ise false döndür;
  60.  
  61. if(head.getPosition() == hasElem) {
  62. return tmp;
  63. }
  64. else {
  65. while (tmp.next != null) {
  66. if (tmp.next.getPosition() == hasElem) {
  67. return tmp;
  68. }
  69. tmp = tmp.next;
  70. }
  71. }
  72. return null;
  73. }
  74.  
  75. public Node getElement(int position) {
  76. Node tmp = head;
  77.  
  78. while(tmp.next != null) {
  79. if(tmp.getPosition() == position) {
  80. return tmp;
  81. }
  82. tmp = tmp.next;
  83. }
  84. return null;
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement