fensa08

APS ISPITNA SEPTEMVRI 2019

Nov 17th, 2019
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.04 KB | None | 0 0
  1. APS Zadaca 1 - Termin 2 Septemvriska Sesija 2019.
  2.  
  3. Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
  4.  
  5. You should preserve the original relative order of the nodes in each of the two partitions.
  6.  
  7. Example:
  8.  
  9. Input: head = 1->4->3->2->5->2, x = 3
  10. Output: 1->2->2->4->3->5
  11.  
  12. =======================================================================================================================================
  13.  
  14. import java.io.BufferedReader;
  15. import java.io.IOException;
  16. import java.io.InputStreamReader;
  17. import java.util.Iterator;
  18. import java.util.NoSuchElementException;
  19.  
  20.  class SLL<E> {
  21.     private SLLNode<E> first;
  22.  
  23.     public SLL() {
  24.         // Construct an empty SLL
  25.         this.first = null;
  26.     }
  27.  
  28.     public void deleteList() {
  29.         first = null;
  30.     }
  31.  
  32.     public int length() {
  33.         int ret;
  34.         if (first != null) {
  35.             SLLNode<E> tmp = first;
  36.             ret = 1;
  37.             while (tmp.succ != null) {
  38.                 tmp = tmp.succ;
  39.                 ret++;
  40.             }
  41.             return ret;
  42.         } else
  43.             return 0;
  44.  
  45.     }
  46.  
  47.     @Override
  48.     public String toString() {
  49.         String ret = new String();
  50.         if (first != null) {
  51.             SLLNode<E> tmp = first;
  52.             ret += tmp + "->";
  53.             while (tmp.succ != null) {
  54.                 tmp = tmp.succ;
  55.                 ret += tmp + "->";
  56.             }
  57.         } else
  58.             ret = "Prazna lista!!!";
  59.         return ret;
  60.     }
  61.  
  62.     public void insertFirst(E o) {
  63.         SLLNode<E> ins = new SLLNode<E>(o, first);
  64.         first = ins;
  65.     }
  66.  
  67.     public void insertAfter(E o, SLLNode<E> node) {
  68.         if (node != null) {
  69.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  70.             node.succ = ins;
  71.         } else {
  72.             System.out.println("Dadenot jazol e null");
  73.         }
  74.     }
  75.  
  76.     public void insertBefore(E o, SLLNode<E> before) {
  77.  
  78.         if (first != null) {
  79.             SLLNode<E> tmp = first;
  80.             if(first==before){
  81.                 this.insertFirst(o);
  82.                 return;
  83.             }
  84.             //ako first!=before
  85.             while (tmp.succ != before)
  86.                 tmp = tmp.succ;
  87.             if (tmp.succ == before) {
  88.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  89.                 tmp.succ = ins;
  90.             } else {
  91.                 System.out.println("Elementot ne postoi vo listata");
  92.             }
  93.         } else {
  94.             System.out.println("Listata e prazna");
  95.         }
  96.     }
  97.  
  98.     public void insertLast(E o) {
  99.         if (first != null) {
  100.             SLLNode<E> tmp = first;
  101.             while (tmp.succ != null)
  102.                 tmp = tmp.succ;
  103.             SLLNode<E> ins = new SLLNode<E>(o, null);
  104.             tmp.succ = ins;
  105.         } else {
  106.             insertFirst(o);
  107.         }
  108.     }
  109.  
  110.     public E deleteFirst() {
  111.         if (first != null) {
  112.             SLLNode<E> tmp = first;
  113.             first = first.succ;
  114.             return tmp.element;
  115.         } else {
  116.             System.out.println("Listata e prazna");
  117.             return null;
  118.         }
  119.     }
  120.  
  121.  
  122.  
  123.  
  124.     public E delete(SLLNode<E> node) {
  125.         if (first != null) {
  126.             SLLNode<E> tmp = first;
  127.             if(first ==node){
  128.                 return this.deleteFirst();
  129.             }
  130.             while (tmp.succ != node && tmp.succ.succ != null)
  131.                 tmp = tmp.succ;
  132.             if (tmp.succ == node) {
  133.                 tmp.succ = tmp.succ.succ;
  134.                 return node.element;
  135.             } else {
  136.                 System.out.println("Elementot ne postoi vo listata");
  137.                 return null;
  138.             }
  139.         } else {
  140.             System.out.println("Listata e prazna");
  141.             return null;
  142.         }
  143.  
  144.     }
  145.  
  146.     public SLLNode<E> getFirst() {
  147.         return first;
  148.     }
  149.  
  150.     public SLLNode<E> find(E o) {
  151.         if (first != null) {
  152.             SLLNode<E> tmp = first;
  153.             while (tmp.element != o && tmp.succ != null)
  154.                 tmp = tmp.succ;
  155.             if (tmp.element == o) {
  156.                 return tmp;
  157.             } else {
  158.                 System.out.println("Elementot ne postoi vo listata");
  159.             }
  160.         } else {
  161.             System.out.println("Listata e prazna");
  162.         }
  163.         return first;
  164.     }
  165.  
  166.     public Iterator<E> iterator () {
  167.         // Return an iterator that visits all elements of this list, in left-to-right order.
  168.         return new LRIterator<E>();
  169.     }
  170.  
  171.     // //////////Inner class ////////////
  172.  
  173.     private class LRIterator<E> implements Iterator<E> {
  174.  
  175.         private SLLNode<E> place, curr;
  176.  
  177.         private LRIterator() {
  178.             place = (SLLNode<E>) first;
  179.             curr = null;
  180.         }
  181.  
  182.         public boolean hasNext() {
  183.             return (place != null);
  184.         }
  185.  
  186.         public E next() {
  187.             if (place == null)
  188.                 throw new NoSuchElementException();
  189.             E nextElem = place.element;
  190.             curr = place;
  191.             place = place.succ;
  192.             return nextElem;
  193.         }
  194.  
  195.         public void remove() {
  196.             //Not implemented
  197.         }
  198.     }
  199.  
  200.     public void mirror(){
  201.         if (first != null) {
  202.             //m=nextsucc, p=tmp,q=next
  203.             SLLNode<E> tmp = first;
  204.             SLLNode<E> newsucc = null;
  205.             SLLNode<E> next;
  206.  
  207.             while(tmp != null){
  208.                 next = tmp.succ;
  209.                 tmp.succ = newsucc;
  210.                 newsucc = tmp;
  211.                 tmp = next;
  212.             }
  213.             first = newsucc;
  214.         }
  215.  
  216.     }
  217.  
  218.     public void merge (SLL<E> in){
  219.         if (first != null) {
  220.             SLLNode<E> tmp = first;
  221.             while(tmp.succ != null)
  222.                 tmp = tmp.succ;
  223.             tmp.succ = in.getFirst();
  224.         }
  225.         else{
  226.             first = in.getFirst();
  227.         }
  228.     }
  229.  
  230.     public void setFirst(SLLNode<E> node){
  231.         first = node;
  232.     }
  233. }
  234.  
  235.  class SLLNode<E> {
  236.     protected E element;
  237.     protected SLLNode<E> succ;
  238.  
  239.     public SLLNode(E elem, SLLNode<E> succ) {
  240.         this.element = elem;
  241.         this.succ = succ;
  242.     }
  243.  
  244.     @Override
  245.     public String toString() {
  246.         return element.toString();
  247.     }
  248. }
  249.  
  250.  
  251.  
  252. public class Main {
  253.  
  254.  
  255.      public static void prevrtiLista(SLL<Integer> list){
  256.  
  257.          SLLNode<Integer> p = list.getFirst();
  258.          SLLNode<Integer> pokazuvajKon = null;
  259.          SLLNode<Integer> next;
  260.  
  261.          while( p != null){
  262.  
  263.              next = p.succ;
  264.              p.succ = pokazuvajKon;
  265.              pokazuvajKon = p;
  266.              p = next;
  267.  
  268.          }
  269.         list.setFirst(pokazuvajKon);
  270.  
  271.  
  272.  
  273.          p = list.getFirst();
  274.          while(p != null){
  275.              System.out.print(p.element + " ");
  276.              p = p.succ;
  277.          }
  278.  
  279.  
  280.      }
  281.  
  282.  
  283.  
  284.      public static void srediLista(SLL<Integer> lista, int n){
  285.  
  286.  
  287.          SLLNode<Integer> prev = null;
  288.          SLLNode<Integer> p = lista.getFirst();
  289.  
  290.  
  291.          for(int i = 0; i < lista.length(); i++){
  292.              if(p.element >= n){
  293.                  lista.insertLast(p.element);
  294.                  SLLNode<Integer> temp = p.succ;
  295.                  lista.delete(p);
  296.                  p = temp;
  297.  
  298.                  continue;
  299.              }
  300.              p = p.succ;
  301.          }
  302.  
  303.          p = lista.getFirst();
  304.          while(p != null){
  305.              System.out.print( p.element + " ");
  306.              p = p.succ;
  307.          }
  308.  
  309.      }
  310.  
  311.  
  312.  
  313.     public static void main(String[] args) throws IOException {
  314.  
  315.         System.out.println("Vnesi elementi vo listata:");
  316.         SLL<Integer> list = new SLL<Integer>();
  317.  
  318.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  319.         String line = in.readLine();
  320.         String split[] = line.split(" ");
  321.         for(int i = 0; i < split.length; i++){
  322.             list.insertLast(Integer.parseInt(split[i]));
  323.         }
  324.  
  325.         int n = Integer.parseInt(in.readLine());
  326.  
  327.         srediLista(list, n);
  328.  
  329.  
  330.     }
  331. }
Add Comment
Please, Sign In to add comment