Advertisement
teodor_dalavera

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

Nov 9th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Scanner;
  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.  
  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) first.pred = null;
  112.             if (first == null)
  113.                 last = null;
  114.             return tmp.element;
  115.         } else
  116.             return null;
  117.     }
  118.  
  119.     public E deleteLast() {
  120.         if (first != null) {
  121.             if (first.succ == null)
  122.                 return deleteFirst();
  123.             else {
  124.                 DLLNode<E> tmp = last;
  125.                 last = last.pred;
  126.                 last.succ = null;
  127.                 return tmp.element;
  128.             }
  129.         }
  130.         // else throw Exception
  131.         return null;
  132.     }
  133.  
  134.     public E delete(DLLNode<E> node) {
  135.         if(node==first){
  136.             deleteFirst();
  137.             return node.element;
  138.         }
  139.         if(node==last){
  140.             deleteLast();
  141.             return node.element;
  142.         }
  143.         node.pred.succ = node.succ;
  144.         node.succ.pred = node.pred;
  145.         return node.element;
  146.        
  147.     }
  148.  
  149.     @Override
  150.     public String toString() {
  151.         String ret = new String();
  152.         if (first != null) {
  153.             DLLNode<E> tmp = first;
  154.             ret += tmp + "<->";
  155.             while (tmp.succ != null) {
  156.                 tmp = tmp.succ;
  157.                 ret += tmp + "<->";
  158.             }
  159.         } else
  160.             ret = "Prazna lista!!!";
  161.         return ret;
  162.     }
  163.    
  164.     public String toStringR() {
  165.         String ret = new String();
  166.         if (last != null) {
  167.             DLLNode<E> tmp = last;
  168.             ret += tmp + "<->";
  169.             while (tmp.pred != null) {
  170.                 tmp = tmp.pred;
  171.                 ret += tmp + "<->";
  172.             }
  173.         } else
  174.             ret = "Prazna lista!!!";
  175.         return ret;
  176.     }
  177.  
  178.     public DLLNode<E> getFirst() {
  179.         return first;
  180.     }
  181.  
  182.     public DLLNode<E> getLast() {
  183.  
  184.         return last;
  185.     }
  186.    
  187.     public void izvadiDupliIPrebroj(){
  188.        
  189.     }
  190. }
  191.  
  192. public class ListaOdListi {
  193.    
  194.     public static long proizvod_sumi(DLL<DLL<Integer>> lista){
  195.         DLLNode<DLL<Integer>> tmp = lista.getFirst();
  196.         DLLNode<Integer> ins = tmp.element.getFirst();
  197.        
  198.         int sum = 0;
  199.         long mult = 1;
  200.         //System.out.println("ins element = " + ins.element);
  201.        
  202.         while(tmp != null){            
  203.             ins = tmp.element.getFirst();
  204.             while(ins != null){
  205.                 sum += ins.element;
  206.                 ins = ins.succ;
  207.             }
  208.             mult *= sum;
  209.             tmp = tmp.succ;
  210.             sum = 0;
  211.         }
  212.        
  213.         return mult;
  214.     }
  215.  
  216.     public static void main(String[] args) throws IOException {
  217.         Scanner in = new Scanner(System.in);
  218.         int n = in.nextInt();
  219.         int m = in.nextInt();
  220.        
  221.         DLL<DLL<Integer>> lista = new DLL<DLL<Integer>>();
  222.        
  223.         for (int i = 0; i < n; i++) {
  224.             DLL<Integer> tmp = new DLL<Integer>();
  225.             for (int j = 0; j < m; j++) {
  226.                 tmp.insertLast(in.nextInt());
  227.             }
  228.             lista.insertLast(tmp);
  229.         }
  230.        
  231.         System.out.println(proizvod_sumi(lista));
  232.     }
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement