Advertisement
MeWWEbcw_Roblox

Scanner

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