DamSi

Untitled

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