Advertisement
NebojshaTodorovic

AIPS-Vezbi za prv kolokvium-Lista od Listi

Jan 17th, 2018
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  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.     @Override
  15.     public String toString() {
  16.         return "<-" + element.toString() + "->";
  17.     }
  18. }
  19.  
  20. class DLL<E> {
  21.     private DLLNode<E> first, last;
  22.  
  23.     public DLL() {
  24.         // Construct an empty SLL
  25.         this.first = null;
  26.         this.last = null;
  27.     }
  28.  
  29.     public void deleteList() {
  30.         first = null;
  31.         last = null;
  32.     }
  33.  
  34.     public int length() {
  35.         int ret;
  36.         if (first != null) {
  37.             DLLNode<E> tmp = first;
  38.             ret = 1;
  39.             while (tmp.succ != null) {
  40.                 tmp = tmp.succ;
  41.                 ret++;
  42.             }
  43.             return ret;
  44.         } else
  45.             return 0;
  46.  
  47.     }
  48.  
  49.     public void insertFirst(E o) {
  50.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  51.         if (first == null)
  52.             last = ins;
  53.         else
  54.             first.pred = ins;
  55.         first = ins;
  56.     }
  57.  
  58.     public void insertLast(E o) {
  59.         if (first == null)
  60.             insertFirst(o);
  61.         else {
  62.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  63.             last.succ = ins;
  64.             last = ins;
  65.         }
  66.     }
  67.  
  68.     public void insertAfter(E o, DLLNode<E> after) {
  69.         if (after == last) {
  70.             insertLast(o);
  71.             return;
  72.         }
  73.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  74.         after.succ.pred = ins;
  75.         after.succ = ins;
  76.     }
  77.  
  78.     public void insertBefore(E o, DLLNode<E> before) {
  79.         if (before == first) {
  80.             insertFirst(o);
  81.             return;
  82.         }
  83.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  84.         before.pred.succ = ins;
  85.         before.pred = ins;
  86.     }
  87.  
  88.     public E deleteFirst() {
  89.         if (first != null) {
  90.             DLLNode<E> tmp = first;
  91.             first = first.succ;
  92.             if (first != null) first.pred = null;
  93.             if (first == null)
  94.                 last = null;
  95.             return tmp.element;
  96.         } else
  97.             return null;
  98.     }
  99.  
  100.     public E deleteLast() {
  101.         if (first != null) {
  102.             if (first.succ == null)
  103.                 return deleteFirst();
  104.             else {
  105.                 DLLNode<E> tmp = last;
  106.                 last = last.pred;
  107.                 last.succ = null;
  108.                 return tmp.element;
  109.             }
  110.         }
  111.         // else throw Exception
  112.         return null;
  113.     }
  114.  
  115.     @Override
  116.     public String toString() {
  117.         String ret = new String();
  118.         if (first != null) {
  119.             DLLNode<E> tmp = first;
  120.             ret += tmp + "<->";
  121.             while (tmp.succ != null) {
  122.                 tmp = tmp.succ;
  123.                 ret += tmp + "<->";
  124.             }
  125.         } else
  126.             ret = "Prazna lista!!!";
  127.         return ret;
  128.     }
  129.  
  130.     public DLLNode<E> getFirst() {
  131.         return first;
  132.     }
  133.  
  134.     public DLLNode<E> getLast() {
  135.  
  136.         return last;
  137.     }
  138.  
  139. }
  140.  
  141. public class ListaOdListi {
  142.  
  143.     public static long findMagicNumber(DLL<DLL<Integer>> list) {
  144.         DLLNode<DLL<Integer>> pom=list.getFirst();
  145.         ArrayList<Long> sumi=new ArrayList<>();
  146.         while(pom!=null){
  147.             DLL<Integer> l1=pom.element;
  148.             DLLNode<Integer>pom1=l1.getFirst();
  149.             long s=0;
  150.             while(pom1!=null){
  151.                 s+=pom1.element;
  152.                 pom1=pom1.succ;
  153.             }
  154.             //System.out.println(s);
  155.             sumi.add(s);
  156.             pom=pom.succ;
  157.         }
  158.  
  159.         return sumi.stream().reduce(Long.parseLong("1"),(a,b)->a*b);
  160.     }
  161.  
  162.     public static void main(String[] args) {
  163.         Scanner in = new Scanner(System.in);
  164.         int n = in.nextInt();
  165.         int m = in.nextInt();
  166.         DLL<DLL<Integer>> list = new DLL<DLL<Integer>>();
  167.         for (int i = 0; i < n; i++) {
  168.             DLL<Integer> tmp = new DLL<Integer>();
  169.             for (int j = 0; j < m; j++) {
  170.                 tmp.insertLast(in.nextInt());
  171.             }
  172.             list.insertLast(tmp);
  173.         }
  174.         in.close();
  175.         System.out.println(findMagicNumber(list));
  176.     }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement