2013-11-22 45 views
0

我有一个问题,我环顾四周,但无法找到一个例子。但使Java中的字符串修剪()方法(除铅/后空格),我知道这个基本的代码是:修剪()方法的链接列表

public LString trim(){ 
    int i = this.size; 
    int j = 0; 
    int k = this.offset; 
    char[] arrayOfChar = this.data; 
    while ((j < i) && (arrayOfChar[(k + j)] <= ' ')) 
     ++j; 
    while ((j < i) && (arrayOfChar[(k + i - 1)] <= ' ')) 
     --i; 
    return (((j > 0) || (i < this.size)) ? substring(j, i) : this); 
} 

但是,你会怎么写这同样的方法,但应用于链接清单?更具体地说,使用Node类的链表。

这是我所做的....如果这是错误的纠正我......我会包括有关该问题的相关类信息。

public class LString{ 

    private Node front = null; //first val in list 
    private Node back; //last val in list 
    private int size = 0; 
    private int i; 
    private int offset; 

    public LString(){ 
     //construct empty list 
     Node LString = new Node(); 
     front = null; 

    } 
.......//skip down some methods to this one 

    //returns new lstring that is slice of lstring 
    //contains an endIndex as well 
    public LString substring(int beginIndex, int endIndex){ 
     Node current = this.front; 
     int size = 0; 
     while(current != null && size < beginIndex){ 
     size++; 
     current = current.getNext(); 
     } 
     front = new Node(); 
     front.setData(current.getData()); 
     Node ocurrent = front; 

     while(current != null && size < endIndex){ 
     current = current.getNext(); 
     Node curr2 = new Node(); 
     curr2.setData(current.getData()); 

     ocurrent.setNext(curr2); 
     ocurrent = curr2; 
     size++; 
     }  
     ocurrent.setNext(null); //set next val to null to term string 
     return this; 
    } 

    public LString trim(){ 
     String lstr; 
     int i = this.size; 
     int m = this.offset; 
     int k = charAt(m); 
     Node current = front; 
     while(current != null){ 
     current = current.getNext(); 
     if(current.data > '\u0020'){ 
     return this; 
     } else if(current.data < '\u0020'){ 
      LString lstring = new LString(); //this worked!? 
      return lstring; 
      } 
     } 
     return this.substring(k, m+1); 
    } 

............................................ ....................

//My Node class: 


public class Node{ 
    public char data; 
    public Node next; 

    //constructors from page 956 
    public Node() 
    { 
     this('\0',null); //'\0' is null char for java 
    } 

    public Node(char initialData, Node initialNext) 
    { 
     data = initialData; 
     next = initialNext; 
    } 
    } 

(如果你不熟悉的节点类,它基本上只是创建了一个单链接节点作为您的链接使用在你的链表类中的数据之间)

我从来没有见过一个例子或任何东西,所以我想我会问社区。

+0

Node中的数据将是String类型,您将在该String上调用trim方法。 – SudoRahul

+0

你能提供一个关于你期望的例子吗? –

回答

1

假设你只是想修改LinkedList中的每个字符串,为什么不只是迭代每个项目?

LinkedList<String> myNodes = new LinkedList<String>(); 
myNodes.add('This is a node '); 
myNodes.add(' another node ')); 

for (String s : myNodes){ 
    s.trim(); 
} 
+0

我更新了我的代码与我试过的 – AOE

2

假设

  • 通过调整名单要删除开头和结尾是空你的意思java.util.LinkedList
  • “使用节点类链表”元素

你应该记住,在java内部实现LinkedList没有公开(注:java.util.LinkedList.Node具有私有访问修饰符),所有修改都通过迭代器和LinkedList本身的方法执行。

实现将是:

public static void trim (LinkedList list){ 
    if (list == null || list.size() == 0) return; 

    Object element = null; 

    ListIterator i = list.listIterator(); 
    while (i.hasNext() && element == null) { 
     element = i.next(); 
     if (element == null) { 
      i.remove(); 
     } 
    } 

    element = null; 
    i = list.listIterator(list.size()); 
    while (i.hasPrevious() && element == null) { 
     element = i.previous(); 
     if (element == null) { 
      i.remove(); 
     } 
    } 
} 

然而,如果你重新实现通过链表可变字符串作为一个练习 (如果不是作为一个练习,然后停在那儿,并使用StringBuilderStringBuffer),然后,假设你使用双向链表来实现它,它会像这样:

编辑:我的不好,你可以迭代到第一个非空元素并设置引用d irectly它,更新算法

  1. 取第一个元素
  2. 虽然牵强元素为空获取下一个
  3. 设置参考上次读取元素,被设置最后取元素的上一页参考null
  4. 提取最后一个元素
  5. 提取元素为空提取前一个
  6. 设置尾巴参考上次读取元素,被设置最后取元素的下一个参考空

UPDATE随着代码你提供尝试这样的事情(因为你使用的是单链表,它与上面描述的略有不同):

public void trim(){ 
    //early out if empty 
    if (front == null || back==null) return; 

    Node current = front; 

    //looking for the first non-empty element 
    while(current != null && current.data<'\u0020'){ 
     current = current.next; 
    } 

    //left trim 
    this.front = current; 

    //looking for last non-empty element 
    while (current!=null&&current.next!=null&&current.next.data>'\u0020'){ 
     current = current.next; 
    } 

    //right trim 
    this.back = current; 
    if (current!=null){ 
     current.next = null; 
    } 
}