如何通过传递链表参考作为参数[homework]来反向打印链表

时间:2014-03-27 02:25:41

标签: java

对于此分配,我需要以链接列表作为参数递归打印链接列表,而不是节点。

我还必须使用我教授提供的这个SinglyLinkedList类:

 public class SinglyLinkedList<E> {
  private int length; // # elements in the linked list
  private SLNode<E> head; // access point to the linked list
  private SLNode<E> tail;

  public SinglyLinkedList() {
    this.length = 0;
    this.tail = new SLNode<E> (); // the tail dummy node
    this.head = new SLNode<E> ( null, this.tail ); // the head dummy node
  }

  public int getLength() {
    return this.length;
  }

  public void add( E e ) {
    SLNode<E> newnode = new SLNode<E> ( e, null );
    newnode.setSuccessor( this.head.getSuccessor() );
    this.head.setSuccessor( newnode );
    this.length++;
  }

  public void add( E e, int p ) {
    // verify that index p is valid
    if ( ( p < 0 ) || ( p > this.length ) ) {
      throw new IndexOutOfBoundsException( "index " + p
                                           + " is out of range: 0 to " +
                                           this.length );
    }
    SLNode<E> newnode = new SLNode<E> ( e, null );
    SLNode<E> cursor = this.head;
    for ( int i = 0; i < p; i++ ) {
      cursor = cursor.getSuccessor();
    }
    addAfter( cursor, newnode );
    this.length++;
  }

  public E remove( int p ) {
    if ( ( p < 0 ) || ( p >= this.length ) ) {
      throw new IndexOutOfBoundsException( "index " + p
                                           + " is out of range: 0 to " +
                                           ( this.length - 1 ) );
    }
    SLNode<E> cursor = head; // good for p == 0
    if ( p > 0 ) {
      cursor = find( p - 1 ); // get target's predecessor
    }

    SLNode<E> target = cursor.getSuccessor(); // get the node to remove

    // link target to cursor's successor
    cursor.setSuccessor( target.getSuccessor() );
    target.setSuccessor( null );
    cursor.setElement( null );
    this.length--;
    return target.getElement();
  }

  public E getElementAt( int p ) {
    SLNode<E> node = this.find( p );
    return node.getElement();
  }

  private void addAfter( SLNode<E> p, SLNode<E> newnode ) {
    newnode.setSuccessor( p.getSuccessor() );
    p.setSuccessor( newnode );
  }

  private SLNode<E> find( E target ) {
    SLNode<E> cursor = head.getSuccessor();

    while ( cursor != tail ) {
      if ( cursor.getElement().equals( target ) ) {
        return cursor; // success
      }
      else {
        cursor = cursor.getSuccessor();
      }
    }
    return null; // failure
  }

  private SLNode<E> find( int p ) {
    if ( ( p < 0 ) || ( p >= this.length ) ) {
      throw new IndexOutOfBoundsException();
    }

    SLNode<E> cursor = head.getSuccessor();
    int i = 0;

    while ( i != p ) {
      cursor = cursor.getSuccessor();
      i++;
    }

    return cursor;
  }

}

我无法通过传递对单链接列表而不是节点的引用来弄清楚如何编写该方法。在此先感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

Haven没有深入研究你的代码,但只是打印应该是非常简单的:

在伪代码中:

printLinkedListInReverse(node) {
   if (node has no next) {
       print out node itself
   } else {
       printLinkedListInReverse(node's next)
       print out node itself
   }
}

简而言之,要反向打印,您需要先打印出除自身之外的剩余列表,然后打印节点本身


如果您无法获取内部节点,并且链接列表需要修改,那么方式仍然类似:

printLinkedListInReverse(list) {
   if (list is empty) {
       //do nothing
   } else {
       firstElement = list.remove(0)
       printLinkedListInReverse(list)  // print the remaining list first
       print firstElement
   }
}

答案 1 :(得分:0)

基本情况是列表为空,没有任何内容可供打印。这就是我们首先检查的内容。如果满足基本情况,我们返回时不打印任何东西。否则列表中剩余的元素,所以我们从列表中取出最后一个元素(因为我们反向打印)并将其从列表中弹出。 remove方法返回已删除的元素,以便执行此操作。现在我们使用修改后的列表(减去最后一个元素)进行递归。最终列表将为空,所有元素都将反向打印。

public static <E> void printReverse(final SinglyLinkedList<E> list) {
    if(list.getLength() > 0) {
        System.out.println(list.remove(list.getLength()-1));
        printReverse(list);
    }
}