У меня вопрос, я огляделся по интернету, но не смог найти пример. Но сделать облицовку String() метод в Java (удаление свинца/завершающие пробельные), я знаю, что базовый код для этого:метод 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;
}
}
(Если вы не знакомы с классом узла, в основном он только создает односвязанны узел для использования в качестве ссылок между данными в вашем списке связанных классов)
Я никогда не видел примера или чего-то еще, поэтому я думал, что попрошу сообщество.
Эти данные в узле будут иметь тип String, и вы вызовете метод trim на этой строке. – SudoRahul
Можете ли вы привести пример о том, что вы ожидаете? –