Java链表 - 发布w 3方法

时间:2014-03-23 22:02:04

标签: java list linked-list

我目前正在处理一个链表项目,但我对我的最后3个方法,我的removeWord(),concatenate()和doubleChar()感到困惑。我想知道是否有人可以给我一些提示我的内容我做错了。

class Node {
private char letter;
private Node next;
public Node(char ch, Node link) {
letter = ch;
next = link;
 }
public void setLetter(char letter) {
this.letter=letter;
 }
 public char getLetter() {

 return letter; 
 }
 public void setNext(Node next) {
 this.next=next;
 }
 public Node getNext() {

 return next;
 }
 }


 class Word {
 // instance variable pointing to the head of the linked list
 private Node head;

 // default constructor
 public Word() {
 head = null;
 }
 // copy constructor
 public Word(Word w) {
 this.head = copy(w.head);
 }
 private static Node copy(Node l){
 Node newL;
 if( l ==null){
    newL=null;

 }else{
    newL = new Node(l.getLetter(),copy(l.getNext()));
 }
 return newL;
 }

  // constructor from a String
  public Word( String s ) {
  Node pt;
  head = null;
  for( int i = s.length()-1; i >=0; i--){
  pt = new Node(s.charAt(i),head);
  head = pt;
  }

  }
 // for output purposes -- override Object version
 // no spaces between the characters, no linefeeds/returns

  @Override
  public String toString() {
  //s.charAt
  return toString(head);
 // return toString(head);
}

  private static String toString(Node L){
   String Word="";
   if (L == null){
    // do nothing 
  }
  else{
    Word = L.getLetter() + toString(L.getNext());
   // return the letter
   }
    return Word;
  }

  // remove the first occurrence of the Word w from this Word
  public void removeWord( Word w ) {
   head = removeWord(head,w);
  }


   private static Node removeWord(Node L, Word w)
  {

   if(L == null)
 {
    // do nothing

  }else if(L==w.head){
    L = L.getNext();

   }
 else {
    // remove the word

  L.setNext(removeWord(L.getNext(),w));
  }
    return L;

  }

  // concatenate a copy of s to the end of this Word
  public void concatenate( Word s ) {
  this.head = concatenate(head,s);

 }
 private static Node concatenate(Node L, Word s){  
 if( L==null){
  L = null;
 }
 else{
 L = new Node(L.getLetter(),concatenate(L.getNext(),s));
 L.setNext(concatenate(L.getNext(),s));// add to the end of the list
 } 
 return L;

  }

  // make a copy of every occurrence of ch in this Word
  // for example, if this Word is abbcbccb, doubleChar ( 'b' ) should
  // change the Word to abbbbcbbccbb
  public void doubleChar( char ch ) {
  head = doubleChar(head,ch);
  }
  public static Node doubleChar(Node L, char ch){
  if(L == null)
  {

  }

  else if(L.getLetter()==ch){

  }
  else{
    //double 
    L.setNext(doubleChar(L.getNext(),ch));
   }
    return L;
   }

0 个答案:

没有答案