Advertisement
Guest User

Untitled

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