Advertisement
Latkoski

Листа од листи

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