2015-03-01 3 views
0

Я изо всех сил пытаюсь построить объект Linked List для построения строк. Мой класс LString предназначен для имитации объекта String или StringBuilder. Вместо массивов он использует связанный список для формирования строк. Однако я не уверен, как сформировать конструктор.Связанный класс списка для создания строк, проблема с конструктором, java

Вот мой код до сих пор:

public class LString { 

    // 2. Fields 
    node front; 
    //node tail; 
    int size; 

    // 1. Node class 

    private class node { 

     char data; 
     node next; 

     //constructors 
     //1. default 
     public node(){ 
     } 

     //2. data 
     public node (char newData){ 
      this.data = newData; 
     } 

     //3. data + next 
     public node (char newData, node newNext){ 
      this.data = newData; 
      this.next = newNext; 
     } 


    } 
    // 3. Constructors 
    public LString(){ 
     this.size = 0; 
     this.front = null; 
    } 
    public LString(String original) { 
    } 

    // 4. Methods 
    public int length() { 
     return this.size; 
    } 
    public int compareTo(LString anotherLString) { 
     return 0; 
    } 
    public boolean equals(Object other) { 
     if (other == null || !(other instanceof LString)) { 
     return false; 
     } 
     else { 
     LString otherLString = (LString)other; 
     return true; 
     } 
    } 
    public char charAt(int index) { 
     return 'a'; 
    } 
    public void setCharAt(int index, char ch) { 
     ch = 'a'; 
    } 
    public LString substring(int start, int end) { 
     return null; 
    } 
    public LString replace(int start, int end, LString lStr) { 
     return null; 
    } 

    //append 
    public void append (char data){ 

     this.size++; 

     if (front == null){ 
      front = new node(data); 
      return; 
     } 

     node curr = front; 
     while (curr.next != null){ 
      curr = curr.next; 
     } 

     curr.next = new node(data); 

    } 

    //prepend 
    public void prepend (char data){ 
     /*node temp = new node(data); 
     temp.next = front; 
     front = temp;*/ 

     front = new node(data, front); 
     size++; 
    } 

    //delete 
    public void delete(int index){ 
    //assume that index is valid 
     if (index == 0){ 
      front = front.next; 
     } else { 
      node curr = front; 
      for (int i = 0; i < index - 1; i++){ 
       curr = curr.next; 
      } 
      curr.next = curr.next.next; 
     } 
     size--; 

    } 

    //toString 
    public String toString(){ 
     StringBuilder result = new StringBuilder(); 
     result.append('['); 

     node curr = front; 
     while (curr != null){ 
      result.append(curr.data); 
      if (curr.next != null){ 
       result.append(','); 
      } 
      curr = curr.next; 
     } 

     result.append(']'); 
     return result.toString(); 
    } 

    //add (at an index) 
    public void add(int index, char data){ 
     if (index == 0){ 
       front = new node(data, front); 
     } else { 
       node curr = front; 
       for (int i = 0; i < index - 1; i++){ 
        curr = curr.next; 
       } 
       curr.next = new node(data, curr.next); 
     } 
    } 
} 

Многих метод являются окурками, так что класс будет скомпилирован с другим тестовым файлом. Я не думаю, что мне нужно включить его, чтобы найти проблему.

Спасибо за помощь.

ответ

0

Вы можете построить свой конструктор LString по-разному. Один из способов, я могу думать о том, чтобы принять char[] и сохранить его в вашем внутреннем LinkedList. Вы можете посмотреть String конструкторов в here, чтобы получить больше идей.

Смежные вопросы