196040

APS Prv kolokvium

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