Advertisement
Guest User

Untitled

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