Advertisement
Guest User

TESY

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