DamSi

Untitled

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