Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.47 KB | None | 0 0
  1. public class DoublyLinkedList
  2. {
  3.     private Element first, last;
  4.     private int size;
  5.  
  6.     public DoublyLinkedList()
  7.     {
  8.         first = last = null;
  9.         size = 0;
  10.     }
  11.  
  12.     public int size()
  13.     {
  14.         return size;
  15.     }
  16.    
  17.     public boolean isEmpty()
  18.     {
  19.         return size == 0;
  20.     }
  21.  
  22.     public void add( Object content )
  23.     {
  24.         Element e = new Element( content );
  25.         if ( isEmpty() )
  26.         {
  27.             first = last = e;
  28.         }
  29.         else
  30.         {
  31.             last.connectAsSucc( e );
  32.             last = e;
  33.         }
  34.         size++;
  35.     }
  36.  
  37.     public void addFirst( Object content )
  38.     {
  39.         Element e = new Element( content );
  40.         if ( isEmpty() )
  41.         {
  42.             first = last = e;
  43.         }
  44.         else
  45.         {
  46.             first.connectAsPred( e );
  47.             first = e;
  48.         }
  49.         size++;
  50.     }
  51.  
  52.     public Object getFirst()
  53.     {
  54.         if ( !isEmpty() )
  55.         {
  56.             return first.getContent();
  57.         }
  58.         else
  59.         {
  60.             throw new RuntimeException();
  61.         }
  62.     }
  63.  
  64.     public Object get( int index )
  65.     {
  66.         if ( index >= 0 && index < size )
  67.         {
  68.             Element current = first;
  69.             for ( int i = 0; i < index; i++ )
  70.             {
  71.                 current = current.getSucc();
  72.             }
  73.             return current.getContent();
  74.         }
  75.         else
  76.         {
  77.             throw new RuntimeException();
  78.         }
  79.     }
  80.  
  81.     public Object removeFirst()
  82.     {
  83.         if ( !isEmpty() )
  84.         {
  85.             Object result = first.getContent();
  86.             if ( first.hasSucc() )
  87.             {
  88.                 first = first.getSucc();
  89.                 first.disconnectPred();
  90.             }
  91.             else
  92.             {
  93.                 first = last = null;
  94.             }
  95.             size--;
  96.             return result;
  97.         }
  98.         else
  99.         {
  100.             throw new RuntimeException();
  101.         }
  102.     }
  103.  
  104.     public void showAll()
  105.     {
  106.         Element current = first;
  107.         while ( current != null )
  108.         {
  109.             System.out.print( current.getContent() );  // impliziter Aufruf von toString, falls != null
  110.             if ( current != last )
  111.             {
  112.                 System.out.print(", ");
  113.             }
  114.             current = current.getSucc();
  115.         }
  116.         System.out.println();
  117.     }
  118.  
  119.  
  120.    
  121.     // zeigt alle Attribute einer Liste und ihrer Elemente an,
  122.     // insbesondere auch die Werte von Referenzen, so dass die
  123.     // Verkettung kontrolliert werden kann
  124.     public void inspect()
  125.     {
  126.         System.out.println( "list\nsize = " + size() );
  127.         if ( first != null ) {
  128.             System.out.println( "first = " + first.toString().substring(first.toString().indexOf('@')) );
  129.         } else {
  130.             System.out.println( "first = null" );
  131.         }
  132.         if ( last != null ) {
  133.             System.out.println( "last = " + last.toString().substring(last.toString().indexOf('@')) );
  134.         } else {
  135.             System.out.println( "last = null" );
  136.         }
  137.         System.out.println( "\nelements" );
  138.         if (last != null) {
  139.             Element current = first;
  140.             int i = 0;
  141.             while (current != null) {
  142.                 inspectElement( current, i );
  143.                 current = current.getSucc();
  144.                 i++;
  145.             }
  146.         } else {
  147.             System.out.println("none");
  148.         }
  149.         System.out.println();
  150.     }
  151.    
  152.     // zeigt alle Attribute eines Elements an
  153.     private void inspectElement( Element e, int index )
  154.     {
  155.         String info = "Element " + index + ": "
  156.                       + e.toString().substring(e.toString().indexOf('@'));
  157.         info += " [ content = ";
  158.         if ( e.getContent() != null ) {
  159.             info += e.getContent().toString();
  160.         } else {
  161.             info += "null";
  162.         }
  163.         info += ", pred = ";
  164.         if ( e.getPred() != null ) {
  165.             info += e.getPred().toString().substring(e.getPred().toString().indexOf('@'));
  166.         } else {
  167.             info += "null";
  168.         }
  169.         info += ", succ = ";
  170.         if ( e.getSucc() != null ) {
  171.             info += e.getSucc().toString().substring(e.getSucc().toString().indexOf('@'));
  172.         } else {
  173.             info += "null";
  174.         }
  175.        
  176.         info += " ]";
  177.         System.out.println( info );
  178.     }
  179.    
  180.     public void clear()
  181.     {
  182.         while ( !isEmpty() ) removeFirst();
  183.     }
  184.    
  185.     public Object getLast()
  186.     {
  187.         if ( !isEmpty() )
  188.         {
  189.             return last.getContent();
  190.         }
  191.         else
  192.         {
  193.             throw new IllegalStateException();
  194.         }
  195.     }
  196.    
  197.     public boolean contains( Object o )
  198.     {
  199.         Element current = first;
  200.         for ( int i = 0; i < size; i++)
  201.         {
  202.             if( current.getContent().equals( o ) ) return true; // über getContent() Element als Object aufrufen!
  203.             if( current.hasSucc() ) current = current.getSucc();
  204.         }
  205.         return false;
  206.     }
  207.    
  208.     public int count( Object o )
  209.     {
  210.         Element current = first;
  211.         int counter = 0;
  212.         for ( int i = 0; i < size; i++)
  213.         {
  214.             if( current.getContent().equals( o ) ) counter++; // über getContent() Element als Object aufrufen!
  215.             if( current.hasSucc() ) current = current.getSucc();
  216.         }
  217.         return counter;
  218.     }
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement