Advertisement
Guest User

MIROR SO NODOVI

a guest
Nov 13th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.37 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. import java.util.Iterator;
  4. import java.util.NoSuchElementException;
  5. import java.util.Scanner;
  6. class SLL<E> {
  7.     public 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.             ){
  185.         if (first != null) {
  186.             //m=nextsucc, p=tmp,q=next
  187.             SLLNode<E> tmp = first;
  188.             SLLNode<E> newsucc = null;
  189.             SLLNode<E> next;
  190.            
  191.             while(tmp != null){
  192.                 next = tmp.succ;
  193.                 tmp.succ = newsucc;
  194.                 newsucc = tmp;
  195.                 tmp = next;
  196.             }
  197.             first = newsucc;
  198.         }
  199.        
  200.     }
  201.    
  202.     public void merge (SLL<E> in){
  203.         if (first != null) {
  204.             SLLNode<E> tmp = first;
  205.             while(tmp.succ != null)
  206.                 tmp = tmp.succ;
  207.             tmp.succ = in.getFirst();
  208.         }
  209.         else{
  210.             first = in.getFirst();
  211.         }
  212.     }
  213. }
  214. class SLLNode<E> {
  215.     public E element;
  216.     public 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. public class JavaKlasaPrevrtiNode {
  229.     public static void main(String[] args) {
  230.         Scanner sc = new Scanner(System.in);
  231.         SLL<Integer> lista = new SLL<>();
  232.         String vnesi = sc.nextLine();
  233.         Integer n = Integer.parseInt(vnesi);
  234.         vnesi = sc.nextLine();
  235.         String [] parts = vnesi.split(" ");
  236.         for(int i=0;i<parts.length;i++) {
  237.             lista.insertLast(Integer.parseInt(parts[i]));
  238.         }
  239. //      vnesi = sc.nextLine();
  240. //      parts = vnesi.split(" ");
  241. //      int m = Integer.parseInt(parts[0]);
  242. //      int N = Integer.parseInt(parts[1]);
  243.        
  244.         prevrtiListaMN(lista);
  245.     }
  246.  
  247.     private static void prevrtiListaMN(SLL<Integer> lista) {
  248.         // TODO Auto-generated method stub
  249.         SLLNode<Integer> dvizi = lista.getFirst();
  250.         SLLNode<Integer> predhoden = null;
  251.         SLLNode<Integer> sleden = null ;
  252.        
  253.        
  254.         while(dvizi != null) {
  255.             sleden = dvizi.succ;
  256.             dvizi.succ=predhoden;
  257.             predhoden = dvizi;
  258.             dvizi=sleden;
  259.             if(predhoden.succ != null) {
  260.                 lista.insertFirst(predhoden.element);
  261.             }
  262.         }
  263.        
  264.         System.out.println(lista);
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement