Advertisement
SlavkovB

[АПС] Подлиста на дадена листа

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