Advertisement
StefiIOE

Kreiranje na Lista (DLL)

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