Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. package Book.LinkedList;
  2.  
  3. /**
  4. * Created by Сергей on 06.07.2015.
  5. */
  6. public class Link {
  7.  
  8. Link(int serial) {
  9. this.serial = serial;
  10. next = null;
  11. }
  12. private int serial;
  13. private Link next;
  14.  
  15.  
  16.  
  17. public void setSerial(int serial) {
  18. this.serial = serial;
  19. }
  20. public int getSerial() {
  21. return serial;
  22. }
  23. public void setNext(Link tmp) {
  24. next = tmp;
  25. }
  26. public Link getNext() {
  27. return next;
  28. }
  29.  
  30. public void display() {
  31. System.out.print(serial + " ");
  32. }
  33. }
  34.  
  35. package Book.LinkedList;
  36.  
  37.  
  38. public class LinkList {
  39. LinkList() {
  40. first = null;
  41.  
  42.  
  43. }
  44.  
  45. private Link first;
  46.  
  47. public boolean isEmpty() {
  48. return first == null;
  49. }
  50.  
  51. public void insertFirst(int serial) {
  52. Link newLink = new Link(serial);
  53. if(first == null ) { newLink.setNext(null); }
  54. else {
  55. newLink.setNext(first);
  56. }
  57. first = newLink;
  58. }
  59.  
  60. public void deleteFirst(){
  61. if( isEmpty() == false){
  62. first = first.getNext();
  63. System.out.println("The link was successfully deleted!");
  64.  
  65. }
  66.  
  67. else {
  68. System.out.println("The LinkList is empty, we can't delete element!");
  69. }
  70.  
  71. }
  72. public boolean find(int key) {
  73. Link current = first;
  74. if (current == null) {
  75. System.out.println("The list is empty.");
  76. return false;
  77. }
  78. do {
  79. if (current.getSerial() == key) {
  80. return true;
  81. }
  82. else {
  83. current = current.getNext();
  84. }
  85. }
  86. while (current != null);
  87. return false;
  88. }
  89. // delete chosen/ selected element
  90. // пока не понятно как данное удаление влияет именно на first, почему удаляется из first
  91. public void delete(int key) {
  92. Link previous = null;
  93. Link current = first;
  94. if (current == null) {
  95. System.out.println("The list is empty.");
  96. return;
  97. }
  98. do {
  99. if (current.getSerial() == key) {
  100. if(previous == null) { first = first.getNext(); return;}
  101. else {
  102. previous.setNext(current.getNext());
  103. return;
  104. }
  105.  
  106. }
  107. else {
  108. previous = current;
  109. current = current.getNext();
  110. }
  111. }
  112. while (current != null);
  113. System.out.println("There isn't element with entered serial;");
  114.  
  115. }
  116.  
  117. public void displayList() {
  118. Link current = first;
  119. while(current != null) {
  120. current.display();
  121. current = current.getNext();
  122. }
  123. System.out.println("The linkList was successfully displayed");
  124.  
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement