Crazy

Nogu teska zadaca

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