Advertisement
mbojmaliev

ReorderList

Jan 23rd, 2018
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.42 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Iterator;
  3. import java.util.NoSuchElementException;
  4. class SLLNode<E> {
  5.     protected E element;
  6.     protected SLLNode<E> succ;
  7.  
  8.     public SLLNode(E elem, SLLNode<E> succ) {
  9.         this.element = elem;
  10.         this.succ = succ;
  11.     }
  12.  
  13.     @Override
  14.     public String toString() {
  15.         return element.toString();
  16.     }
  17. }
  18.  
  19. class SLL<E> {
  20.     private SLLNode<E> first;
  21.  
  22.     public SLL() {
  23.         // Construct an empty SLL
  24.         this.first = null;
  25.     }
  26.  
  27.     public void deleteList() {
  28.         first = null;
  29.     }
  30.  
  31.     public int length() {
  32.         int ret;
  33.         if (first != null) {
  34.             SLLNode<E> tmp = first;
  35.             ret = 1;
  36.             while (tmp.succ != null) {
  37.                 tmp = tmp.succ;
  38.                 ret++;
  39.             }
  40.             return ret;
  41.         } else
  42.             return 0;
  43.  
  44.     }
  45.  
  46.     @Override
  47.     public String toString() {
  48.         String ret = new String();
  49.         if (first != null) {
  50.             SLLNode<E> tmp = first;
  51.             ret += tmp + "->";
  52.             while (tmp.succ != null) {
  53.                 tmp = tmp.succ;
  54.                 ret += tmp;
  55.                 if(tmp.succ != null)ret+="->";
  56.             }
  57.  
  58.         } else
  59.             ret = "Prazna lista!!!";
  60.         return ret;
  61.     }
  62.  
  63.     public void insertFirst(E o) {
  64.         SLLNode<E> ins = new SLLNode<E>(o, first);
  65.         first = ins;
  66.     }
  67.  
  68.     public void insertAfter(E o, SLLNode<E> node) {
  69.         if (node != null) {
  70.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  71.             node.succ = ins;
  72.         } else {
  73.             System.out.println("Dadenot jazol e null");
  74.         }
  75.     }
  76.  
  77.     public void insertBefore(E o, SLLNode<E> before) {
  78.  
  79.         if (first != null) {
  80.             SLLNode<E> tmp = first;
  81.             if(first==before){
  82.                 this.insertFirst(o);
  83.                 return;
  84.             }
  85.             //ako first!=before
  86.             while (tmp.succ != before)
  87.                 tmp = tmp.succ;
  88.             if (tmp.succ == before) {
  89.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  90.                 tmp.succ = ins;
  91.             } else {
  92.                 System.out.println("Elementot ne postoi vo listata");
  93.             }
  94.         } else {
  95.             System.out.println("Listata e prazna");
  96.         }
  97.     }
  98.  
  99.     public void insertLast(E o) {
  100.         if (first != null) {
  101.             SLLNode<E> tmp = first;
  102.             while (tmp.succ != null)
  103.                 tmp = tmp.succ;
  104.             SLLNode<E> ins = new SLLNode<E>(o, null);
  105.             tmp.succ = ins;
  106.         } else {
  107.             insertFirst(o);
  108.         }
  109.     }
  110.  
  111.     public E deleteFirst() {
  112.         if (first != null) {
  113.             SLLNode<E> tmp = first;
  114.             first = first.succ;
  115.             return tmp.element;
  116.         } else {
  117.             System.out.println("Listata e prazna");
  118.             return null;
  119.         }
  120.     }
  121.  
  122.     public E delete(SLLNode<E> node) {
  123.         if (first != null) {
  124.             SLLNode<E> tmp = first;
  125.             if(first ==node){
  126.                 return this.deleteFirst();
  127.             }
  128.             while (tmp.succ != node && tmp.succ.succ != null)
  129.                 tmp = tmp.succ;
  130.             if (tmp.succ == node) {
  131.                 tmp.succ = tmp.succ.succ;
  132.                 return node.element;
  133.             } else {
  134.                 System.out.println("Elementot ne postoi vo listata");
  135.                 return null;
  136.             }
  137.         } else {
  138.             System.out.println("Listata e prazna");
  139.             return null;
  140.         }
  141.  
  142.     }
  143.  
  144.     public SLLNode<E> getFirst() {
  145.         return first;
  146.     }
  147.  
  148.     public SLLNode<E> find(E o) {
  149.         if (first != null) {
  150.             SLLNode<E> tmp = first;
  151.             while (tmp.element != o && tmp.succ != null)
  152.                 tmp = tmp.succ;
  153.             if (tmp.element == o) {
  154.                 return tmp;
  155.             } else {
  156.                 System.out.println("Elementot ne postoi vo listata");
  157.             }
  158.         } else {
  159.             System.out.println("Listata e prazna");
  160.         }
  161.         return first;
  162.     }
  163.  
  164.     public Iterator<E> iterator () {
  165.         // Return an iterator that visits all elements of this list, in left-to-right order.
  166.         return new LRIterator<E>();
  167.     }
  168.  
  169.     // //////////Inner class ////////////
  170.  
  171.     private class LRIterator<E> implements Iterator<E> {
  172.  
  173.         private SLLNode<E> place, curr;
  174.  
  175.         private LRIterator() {
  176.             place = (SLLNode<E>) first;
  177.             curr = null;
  178.         }
  179.  
  180.         public boolean hasNext() {
  181.             return (place != null);
  182.         }
  183.  
  184.         public E next() {
  185.             if (place == null)
  186.                 throw new NoSuchElementException();
  187.             E nextElem = place.element;
  188.             curr = place;
  189.             place = place.succ;
  190.             return nextElem;
  191.         }
  192.  
  193.         public void remove() {
  194.             //Not implemented
  195.         }
  196.     }
  197.  
  198.     public void mirror(){
  199.         if (first != null) {
  200.             //m=nextsucc, p=tmp,q=next
  201.             SLLNode<E> tmp = first;
  202.             SLLNode<E> newsucc = null;
  203.             SLLNode<E> next;
  204.  
  205.             while(tmp != null){
  206.                 next = tmp.succ;
  207.                 tmp.succ = newsucc;
  208.                 newsucc = tmp;
  209.                 tmp = next;
  210.             }
  211.             first = newsucc;
  212.         }
  213.  
  214.     }
  215.  
  216.     public void merge (SLL<E> in){
  217.         if (first != null) {
  218.             SLLNode<E> tmp = first;
  219.             while(tmp.succ != null)
  220.                 tmp = tmp.succ;
  221.             tmp.succ = in.getFirst();
  222.         }
  223.         else{
  224.             first = in.getFirst();
  225.         }
  226.     }
  227. }
  228.  
  229. public class Pero {
  230.  
  231.     public static void main(String[] args){
  232.         Scanner in = new Scanner(System.in);
  233.  
  234.         int n = in.nextInt();
  235.         SLL<Integer> pero = new SLL<>();
  236.         for(int i=0; i<n; i++){
  237.             pero.insertLast(in.nextInt());
  238.         }
  239.         int m1 = in.nextInt();
  240.         int m2 = in.nextInt();
  241.  
  242.         SLLNode<Integer> prv = pero.getFirst();
  243.         for(int i=1;i<m1; i++)prv = prv.succ;
  244.  
  245.         for(int i=0; i<m2-m1; i++){
  246.             SLLNode<Integer> posleden = prv;
  247.             for(int j=0;j<(m2-m1)-i; j++)posleden = posleden.succ;
  248.             pero.insertBefore(posleden.element, prv);
  249.             pero.delete(posleden);
  250.         }
  251.  
  252.         System.out.println(pero);
  253.  
  254.     }
  255.  
  256.  
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement