Natasha10

Untitled

Feb 15th, 2019
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. package Zadachiche;
  2.  
  3.  
  4.  
  5.  
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.util.Iterator;
  10. import java.util.NoSuchElementException;
  11.  
  12.  
  13.  
  14. class SLLNode<E extends Comparable> {
  15. public E element;
  16. public SLLNode<E> succ;
  17.  
  18. public SLLNode(E elem, SLLNode<E> succ) {
  19. this.element = elem;
  20. this.succ = succ;
  21. }
  22.  
  23. @Override
  24. public String toString() {
  25. return element.toString();
  26. }
  27. }
  28.  
  29. class SLL<E extends Comparable> {
  30. SLLNode<E> first;
  31.  
  32. public SLL() {
  33. // Construct an empty SLL
  34. this.first = null;
  35. }
  36.  
  37. public void deleteList() {
  38. first = null;
  39. }
  40.  
  41. public int length() {
  42. int ret;
  43. if (first != null) {
  44. SLLNode<E> tmp = first;
  45. ret = 1;
  46. while (tmp.succ != null) {
  47. tmp = tmp.succ;
  48. ret++;
  49. }
  50. return ret;
  51. } else
  52. return 0;
  53.  
  54. }
  55.  
  56. @Override
  57. public String toString() {
  58. String ret = new String();
  59. if (first != null) {
  60. SLLNode<E> tmp = first;
  61. ret += tmp + "->";
  62. while (tmp.succ != null) {
  63. tmp = tmp.succ;
  64. ret += tmp + "->";
  65. }
  66. } else
  67. ret = "Prazna lista!!!";
  68. return ret;
  69. }
  70.  
  71. public void insertFirst(E o) {
  72. SLLNode<E> ins = new SLLNode<E>(o, first);
  73. first = ins;
  74. }
  75.  
  76. public void insertAfter(E o, SLLNode<E> node) {
  77. if (node != null) {
  78. SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  79. node.succ = ins;
  80. } else {
  81. System.out.println("Dadenot jazol e null");
  82. }
  83. }
  84.  
  85. public void insertBefore(E o, SLLNode<E> before) {
  86.  
  87. if (first != null) {
  88. SLLNode<E> tmp = first;
  89. if(first==before) {
  90. this.insertFirst(o);
  91. return;
  92. }
  93. //ako first!=before
  94. while (tmp.succ != before)
  95. tmp = tmp.succ;
  96. if (tmp.succ == before) {
  97. SLLNode<E> ins = new SLLNode<E>(o, before);
  98. tmp.succ = ins;
  99. } else {
  100. System.out.println("Elementot ne postoi vo listata");
  101. }
  102. } else {
  103. System.out.println("Listata e prazna");
  104. }
  105. }
  106.  
  107. public void insertLast(E o) {
  108. if (first != null) {
  109. SLLNode<E> tmp = first;
  110. while (tmp.succ != null)
  111. tmp = tmp.succ;
  112. SLLNode<E> ins = new SLLNode<E>(o, null);
  113. tmp.succ = ins;
  114. } else {
  115. insertFirst(o);
  116. }
  117. }
  118.  
  119. public E deleteFirst() {
  120. if (first != null) {
  121. SLLNode<E> tmp = first;
  122. first = first.succ;
  123. return tmp.element;
  124. } else {
  125. System.out.println("Listata e prazna");
  126. return null;
  127. }
  128. }
  129.  
  130. public E delete(SLLNode<E> node) {
  131. if (first != null) {
  132. SLLNode<E> tmp = first;
  133. if(first ==node) {
  134. return this.deleteFirst();
  135. }
  136. while (tmp.succ != node&&tmp.succ.succ != null)
  137. tmp = tmp.succ;
  138. if (tmp.succ == node) {
  139. tmp.succ = tmp.succ.succ;
  140. return node.element;
  141. } else {
  142. System.out.println("Elementot ne postoi vo listata");
  143. return null;
  144. }
  145. } else {
  146. System.out.println("Listata e prazna");
  147. return null;
  148. }
  149.  
  150. }
  151.  
  152. public SLLNode<E> getFirst() {
  153. return first;
  154. }
  155.  
  156. public SLLNode<E> getLast(){
  157. SLLNode<E> temp=first;
  158. while(temp.succ!=null) {
  159. temp=temp.succ;
  160. }
  161. return temp;
  162. }
  163.  
  164. public SLLNode<E> find(E o) {
  165. if (first != null) {
  166. SLLNode<E> tmp = first;
  167. while (tmp.element != o&&tmp.succ != null)
  168. tmp = tmp.succ;
  169. if (tmp.element == o) {
  170. return tmp;
  171. } else {
  172. System.out.println("Elementot ne postoi vo listata");
  173. }
  174. } else {
  175. System.out.println("Listata e prazna");
  176. }
  177. return first;
  178. }
  179.  
  180. public Iterator<E> iterator () {
  181. // Return an iterator that visits all elements of this list, in left-to-right order.
  182. return new LRIterator<E>();
  183. }
  184.  
  185. // //////////Inner class ////////////
  186.  
  187. private class LRIterator<E extends Comparable> implements Iterator<E> {
  188.  
  189. private SLLNode<E> place, curr;
  190.  
  191. private LRIterator() {
  192. place = (SLLNode<E>) first;
  193. curr = null;
  194. }
  195.  
  196. public boolean hasNext() {
  197. return (place != null);
  198. }
  199.  
  200. public E next() {
  201. if (place == null)
  202. throw new NoSuchElementException();
  203. E nextElem = place.element;
  204. curr = place;
  205. place = place.succ;
  206. return nextElem;
  207. }
  208.  
  209. public void remove() {
  210. //Not implemented
  211. }
  212. }
  213. public SLL<E> joinLists(SLL<E> l2) {
  214. SLLNode<E> d1 = this.getFirst();//this e referenca na lista1
  215. SLLNode<E> d2 = l2.getFirst();
  216.  
  217. SLL<E> result = new SLL();
  218.  
  219.  
  220. while(d1!=null && d2!=null) {
  221. if(d1.element.compareTo(d2.element)<0) {
  222. result.insertLast(d1.element);
  223. d1 = d1.succ;
  224. } else if(d1.element.compareTo(d2.element)>0) {
  225. result.insertLast(d2.element);
  226. d2 = d2.succ;
  227. } else {
  228. result.insertLast(d1.element);
  229. d1 = d1.succ;
  230. d2 = d2.succ;
  231. }
  232. }
  233. while(d1!=null) {
  234. result.insertLast(d1.element);
  235. d1 = d1.succ;
  236. }
  237. while(d2!=null) {
  238. result.insertLast(d2.element);
  239. d2 = d2.succ;
  240. }
  241. return result;
  242. }
  243.  
  244.  
  245. }
  246.  
  247.  
  248. public class hehe {
  249.  
  250. public static void presmetaj(String izraz1,String izraz2) {
  251. SLL<Integer> lista1=new SLL<>();
  252. SLL<Integer> lista2=new SLL<>();
  253.  
  254. for (int i = 0; i < izraz1.length(); i++) {
  255. lista1.insertLast(izraz1.charAt(i)-'0');
  256. }
  257.  
  258. for (int i = 0; i < izraz2.length(); i++) {
  259. lista2.insertLast(izraz2.charAt(i)-'0');
  260. }
  261.  
  262.  
  263. SLLNode<Integer> dvizi1=lista1.first;
  264. SLLNode<Integer> dvizi2=lista2.first;
  265.  
  266. int N=izraz1.length()-1;
  267. long suma1=0;
  268. while(dvizi1!=null && N!=-1) {
  269.  
  270.  
  271. suma1=suma1+dvizi1.element*10^(N);
  272. N--;
  273. dvizi1=dvizi1.succ;
  274.  
  275.  
  276. }
  277.  
  278. int M=izraz2.length()-1;
  279. long suma2=0;
  280. while(dvizi2!=null || M!=-1) {
  281.  
  282.  
  283. suma2=suma2+dvizi2.element*10^(M);
  284. M--;
  285. dvizi2=dvizi2.succ;
  286.  
  287. }
  288.  
  289. long rezultat=suma1+suma2;
  290. System.out.print(rezultat);
  291.  
  292. }
  293.  
  294.  
  295.  
  296. public static void main(String[] args) throws IOException {
  297.  
  298.  
  299. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  300. String izraz1=br.readLine();
  301. String izraz2=br.readLine();
  302.  
  303.  
  304. presmetaj(izraz1,izraz2);
  305. }
  306.  
  307.  
  308. }
Advertisement
Add Comment
Please, Sign In to add comment