Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DoublyLinkedList
- {
- private Element first, last;
- private int size;
- public DoublyLinkedList()
- {
- first = last = null;
- size = 0;
- }
- public int size()
- {
- return size;
- }
- public boolean isEmpty()
- {
- return size == 0;
- }
- public void add( Object content )
- {
- Element e = new Element( content );
- if ( isEmpty() )
- {
- first = last = e;
- }
- else
- {
- last.connectAsSucc( e );
- last = e;
- }
- size++;
- }
- public void addFirst( Object content )
- {
- Element e = new Element( content );
- if ( isEmpty() )
- {
- first = last = e;
- }
- else
- {
- first.connectAsPred( e );
- first = e;
- }
- size++;
- }
- public Object getFirst()
- {
- if ( !isEmpty() )
- {
- return first.getContent();
- }
- else
- {
- throw new RuntimeException();
- }
- }
- public Object get( int index )
- {
- if ( index >= 0 && index < size )
- {
- Element current = first;
- for ( int i = 0; i < index; i++ )
- {
- current = current.getSucc();
- }
- return current.getContent();
- }
- else
- {
- throw new RuntimeException();
- }
- }
- public Object removeFirst()
- {
- if ( !isEmpty() )
- {
- Object result = first.getContent();
- if ( first.hasSucc() )
- {
- first = first.getSucc();
- first.disconnectPred();
- }
- else
- {
- first = last = null;
- }
- size--;
- return result;
- }
- else
- {
- throw new RuntimeException();
- }
- }
- public void showAll()
- {
- Element current = first;
- while ( current != null )
- {
- System.out.print( current.getContent() ); // impliziter Aufruf von toString, falls != null
- if ( current != last )
- {
- System.out.print(", ");
- }
- current = current.getSucc();
- }
- System.out.println();
- }
- // zeigt alle Attribute einer Liste und ihrer Elemente an,
- // insbesondere auch die Werte von Referenzen, so dass die
- // Verkettung kontrolliert werden kann
- public void inspect()
- {
- System.out.println( "list\nsize = " + size() );
- if ( first != null ) {
- System.out.println( "first = " + first.toString().substring(first.toString().indexOf('@')) );
- } else {
- System.out.println( "first = null" );
- }
- if ( last != null ) {
- System.out.println( "last = " + last.toString().substring(last.toString().indexOf('@')) );
- } else {
- System.out.println( "last = null" );
- }
- System.out.println( "\nelements" );
- if (last != null) {
- Element current = first;
- int i = 0;
- while (current != null) {
- inspectElement( current, i );
- current = current.getSucc();
- i++;
- }
- } else {
- System.out.println("none");
- }
- System.out.println();
- }
- // zeigt alle Attribute eines Elements an
- private void inspectElement( Element e, int index )
- {
- String info = "Element " + index + ": "
- + e.toString().substring(e.toString().indexOf('@'));
- info += " [ content = ";
- if ( e.getContent() != null ) {
- info += e.getContent().toString();
- } else {
- info += "null";
- }
- info += ", pred = ";
- if ( e.getPred() != null ) {
- info += e.getPred().toString().substring(e.getPred().toString().indexOf('@'));
- } else {
- info += "null";
- }
- info += ", succ = ";
- if ( e.getSucc() != null ) {
- info += e.getSucc().toString().substring(e.getSucc().toString().indexOf('@'));
- } else {
- info += "null";
- }
- info += " ]";
- System.out.println( info );
- }
- public void clear()
- {
- while ( !isEmpty() ) removeFirst();
- }
- public Object getLast()
- {
- if ( !isEmpty() )
- {
- return last.getContent();
- }
- else
- {
- throw new IllegalStateException();
- }
- }
- public boolean contains( Object o )
- {
- Element current = first;
- for ( int i = 0; i < size; i++)
- {
- if( current.getContent().equals( o ) ) return true; // über getContent() Element als Object aufrufen!
- if( current.hasSucc() ) current = current.getSucc();
- }
- return false;
- }
- public int count( Object o )
- {
- Element current = first;
- int counter = 0;
- for ( int i = 0; i < size; i++)
- {
- if( current.getContent().equals( o ) ) counter++; // über getContent() Element als Object aufrufen!
- if( current.hasSucc() ) current = current.getSucc();
- }
- return counter;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement