Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. class SLLNode<E> {
  2. protected E element;
  3. protected SLLNode<E> succ;
  4.  
  5. public SLLNode(E elem, SLLNode<E> succ) {
  6. this.element = elem;
  7. this.succ = succ;
  8. }
  9.  
  10. @Override
  11. public String toString() {
  12. return element.toString();
  13. }
  14. }
  15.  
  16. class SLL<E> {
  17. private SLLNode<E> first;
  18.  
  19. public SLL() {
  20. // Construct an empty SLL
  21. this.first = null;
  22. }
  23.  
  24. public void deleteList() {
  25. first = null;
  26. }
  27.  
  28. public int length() {
  29. int ret;
  30. if (first != null) {
  31. SLLNode<E> tmp = first;
  32. ret = 1;
  33. while (tmp.succ != null) {
  34. tmp = tmp.succ;
  35. ret++;
  36. }
  37. return ret;
  38. } else
  39. return 0;
  40.  
  41. }
  42.  
  43. @Override
  44. public String toString() {
  45. String ret = new String();
  46. if (first != null) {
  47. SLLNode<E> tmp = first;
  48. ret += tmp + "->";
  49. while (tmp.succ != null) {
  50. tmp = tmp.succ;
  51. ret += tmp + "->";
  52. }
  53. } else
  54. ret = "Prazna lista!!!";
  55. return ret;
  56. }
  57.  
  58. public void insertFirst(E o) {
  59. SLLNode<E> ins = new SLLNode<E>(o, first);
  60. first = ins;
  61. }
  62.  
  63. public void insertAfter(E o, SLLNode<E> node) {
  64. if (node != null) {
  65. SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  66. node.succ = ins;
  67. } else {
  68. System.out.println("Dadenot jazol e null");
  69. }
  70. }
  71.  
  72. public void insertBefore(E o, SLLNode<E> before) {
  73.  
  74. if (first != null) {
  75. SLLNode<E> tmp = first;
  76. if(first==before){
  77. this.insertFirst(o);
  78. return;
  79. }
  80. //ako first!=before
  81. while (tmp.succ != before)
  82. tmp = tmp.succ;
  83. if (tmp.succ == before) {
  84. SLLNode<E> ins = new SLLNode<E>(o, before);
  85. tmp.succ = ins;
  86. } else {
  87. System.out.println("Elementot ne postoi vo listata");
  88. }
  89. } else {
  90. System.out.println("Listata e prazna");
  91. }
  92. }
  93.  
  94. public void insertLast(E o) {
  95. if (first != null) {
  96. SLLNode<E> tmp = first;
  97. while (tmp.succ != null)
  98. tmp = tmp.succ;
  99. SLLNode<E> ins = new SLLNode<E>(o, null);
  100. tmp.succ = ins;
  101. } else {
  102. insertFirst(o);
  103. }
  104. }
  105.  
  106. public E deleteFirst() {
  107. if (first != null) {
  108. SLLNode<E> tmp = first;
  109. first = first.succ;
  110. return tmp.element;
  111. } else {
  112. System.out.println("Listata e prazna");
  113. return null;
  114. }
  115. }
  116.  
  117. public E delete(SLLNode<E> node) {
  118. if (first != null) {
  119. SLLNode<E> tmp = first;
  120. if(first ==node){
  121. return this.deleteFirst();
  122. }
  123. while (tmp.succ != node && tmp.succ.succ != null)
  124. tmp = tmp.succ;
  125. if (tmp.succ == node) {
  126. tmp.succ = tmp.succ.succ;
  127. return node.element;
  128. } else {
  129. System.out.println("Elementot ne postoi vo listata");
  130. return null;
  131. }
  132. } else {
  133. System.out.println("Listata e prazna");
  134. return null;
  135. }
  136.  
  137. }
  138.  
  139. public SLLNode<E> getFirst() {
  140. return first;
  141. }
  142.  
  143. public SLLNode<E> find(E o) {
  144. if (first != null) {
  145. SLLNode<E> tmp = first;
  146. while (tmp.element != o && tmp.succ != null)
  147. tmp = tmp.succ;
  148. if (tmp.element == o) {
  149. return tmp;
  150. } else {
  151. System.out.println("Elementot ne postoi vo listata");
  152. }
  153. } else {
  154. System.out.println("Listata e prazna");
  155. }
  156. return first;
  157. }
  158. }
  159.  
  160. public class HelloWorld {
  161. public static void changeList(SLL<Integer> list, int n){
  162. SLLNode<Integer> current = list.getFirst();
  163. SLLNode<Integer> next = null;
  164. while(current != null){
  165. if(current.element % 2 == 0){
  166. next = current.succ;
  167. while(next != null && !(next.element % 2 == 0)){
  168. next = next.succ;
  169. }
  170. if(next == null)
  171. break;
  172. int sum = current.element - next.element;
  173. list.insertBefore(sum, current);
  174. list.delete(current);
  175. list.delete(next);
  176. }else {
  177. next = current.succ;
  178. while(next != null && next.element % 2 == 0){
  179. next = next.succ;
  180. }
  181. if(next == null)
  182. break;
  183. int sum = current.element + next.element;
  184. list.insertBefore(sum, current);
  185. list.delete(current);
  186. list.delete(next);
  187. }
  188. current = current.succ;
  189. }
  190. System.out.println(list);
  191. }
  192.  
  193. public static void main(String args[]) {
  194. SLL<Integer> list = new SLL<Integer>();
  195. int n = 8;
  196. list.insertLast(1);
  197. list.insertLast(2);
  198. list.insertLast(3);
  199. list.insertLast(4);
  200. list.insertLast(5);
  201. list.insertLast(6);
  202. list.insertLast(7);
  203. list.insertLast(8);
  204.  
  205. changeList(list, n);
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement