簡體   English   中英

鏈表的trim()方法

[英]trim() method for linked list

我有一個問題,我在互聯網上四處張望,但找不到示例。 但是,在Java中制作一個String trim()方法(除去前導/尾隨空白),我知道這的基本代碼是:

    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;
   }
   }

(如果您不熟悉節點類,那么它基本上只會創建一個單鏈接節點,用作鏈接列表類中數據之間的鏈接)

我從未見過任何示例或任何內容,因此我想向社區詢問。

假如說

  • 通過修剪要刪除的前導和尾隨元素的列表
  • “使用Node類的鏈接列表”下,您的意思是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 ),那么,假設您使用雙鏈表實現它,它將像這樣:

編輯:我不好,您可以迭代到第一個非空元素並直接設置對它的引用,更新算法

  1. 獲取第一個元素
  2. 當獲取的元素為空時,接下來獲取
  3. head引用設置為最后一個提取的元素,將last提取元素的prev引用設置為null
  4. 獲取最后一個元素
  5. 當獲取的元素為空時,獲取先前的元素
  6. 將尾引用設置為最后一個獲取的元素,將最后提取的元素的下一個引用設置為null

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;
    }
}

假設您只想修剪LinkedList中的每個String,為什么不僅僅遍歷每個項目呢?

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM