Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Zadachiche;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- class SLLNode<E extends Comparable> {
- public E element;
- public SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class SLL<E extends Comparable> {
- SLLNode<E> first;
- public SLL() {
- // Construct an empty SLL
- this.first = null;
- }
- public void deleteList() {
- first = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- SLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- SLLNode<E> tmp = first;
- ret += tmp + "->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + "->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public void insertFirst(E o) {
- SLLNode<E> ins = new SLLNode<E>(o, first);
- first = ins;
- }
- public void insertAfter(E o, SLLNode<E> node) {
- if (node != null) {
- SLLNode<E> ins = new SLLNode<E>(o, node.succ);
- node.succ = ins;
- } else {
- System.out.println("Dadenot jazol e null");
- }
- }
- public void insertBefore(E o, SLLNode<E> before) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if(first==before) {
- this.insertFirst(o);
- return;
- }
- //ako first!=before
- while (tmp.succ != before)
- tmp = tmp.succ;
- if (tmp.succ == before) {
- SLLNode<E> ins = new SLLNode<E>(o, before);
- tmp.succ = ins;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- }
- public void insertLast(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- SLLNode<E> ins = new SLLNode<E>(o, null);
- tmp.succ = ins;
- } else {
- insertFirst(o);
- }
- }
- public E deleteFirst() {
- if (first != null) {
- SLLNode<E> tmp = first;
- first = first.succ;
- return tmp.element;
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public E delete(SLLNode<E> node) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if(first ==node) {
- return this.deleteFirst();
- }
- while (tmp.succ != node&&tmp.succ.succ != null)
- tmp = tmp.succ;
- if (tmp.succ == node) {
- tmp.succ = tmp.succ.succ;
- return node.element;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- return null;
- }
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public SLLNode<E> getFirst() {
- return first;
- }
- public SLLNode<E> getLast(){
- SLLNode<E> temp=first;
- while(temp.succ!=null) {
- temp=temp.succ;
- }
- return temp;
- }
- public SLLNode<E> find(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.element != o&&tmp.succ != null)
- tmp = tmp.succ;
- if (tmp.element == o) {
- return tmp;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- return first;
- }
- public Iterator<E> iterator () {
- // Return an iterator that visits all elements of this list, in left-to-right order.
- return new LRIterator<E>();
- }
- // //////////Inner class ////////////
- private class LRIterator<E extends Comparable> implements Iterator<E> {
- private SLLNode<E> place, curr;
- private LRIterator() {
- place = (SLLNode<E>) first;
- curr = null;
- }
- public boolean hasNext() {
- return (place != null);
- }
- public E next() {
- if (place == null)
- throw new NoSuchElementException();
- E nextElem = place.element;
- curr = place;
- place = place.succ;
- return nextElem;
- }
- public void remove() {
- //Not implemented
- }
- }
- public SLL<E> joinLists(SLL<E> l2) {
- SLLNode<E> d1 = this.getFirst();//this e referenca na lista1
- SLLNode<E> d2 = l2.getFirst();
- SLL<E> result = new SLL();
- while(d1!=null && d2!=null) {
- if(d1.element.compareTo(d2.element)<0) {
- result.insertLast(d1.element);
- d1 = d1.succ;
- } else if(d1.element.compareTo(d2.element)>0) {
- result.insertLast(d2.element);
- d2 = d2.succ;
- } else {
- result.insertLast(d1.element);
- d1 = d1.succ;
- d2 = d2.succ;
- }
- }
- while(d1!=null) {
- result.insertLast(d1.element);
- d1 = d1.succ;
- }
- while(d2!=null) {
- result.insertLast(d2.element);
- d2 = d2.succ;
- }
- return result;
- }
- }
- public class hehe {
- public static void presmetaj(String izraz1,String izraz2) {
- SLL<Integer> lista1=new SLL<>();
- SLL<Integer> lista2=new SLL<>();
- for (int i = 0; i < izraz1.length(); i++) {
- lista1.insertLast(izraz1.charAt(i)-'0');
- }
- for (int i = 0; i < izraz2.length(); i++) {
- lista2.insertLast(izraz2.charAt(i)-'0');
- }
- SLLNode<Integer> dvizi1=lista1.first;
- SLLNode<Integer> dvizi2=lista2.first;
- int N=izraz1.length()-1;
- long suma1=0;
- while(dvizi1!=null && N!=-1) {
- suma1=suma1+dvizi1.element*10^(N);
- N--;
- dvizi1=dvizi1.succ;
- }
- int M=izraz2.length()-1;
- long suma2=0;
- while(dvizi2!=null || M!=-1) {
- suma2=suma2+dvizi2.element*10^(M);
- M--;
- dvizi2=dvizi2.succ;
- }
- long rezultat=suma1+suma2;
- System.out.print(rezultat);
- }
- public static void main(String[] args) throws IOException {
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- String izraz1=br.readLine();
- String izraz2=br.readLine();
- presmetaj(izraz1,izraz2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment