用于构建字符串的链表类,构造函数有问题,java
我正在努力构造一个用于构建字符串的链表对象。我的类用于构建字符串的链表类,构造函数有问题,java,java,linked-list,Java,Linked List,我正在努力构造一个用于构建字符串的链表对象。我的类LString旨在模拟String或StringBuilder对象。它使用链表而不是数组来形成字符串。但是我不确定如何形成构造函数 以下是我目前的代码: public class LString { // 2. Fields node front; //node tail; int size; // 1. Node class private class node {
LString
旨在模拟String
或StringBuilder
对象。它使用链表而不是数组来形成字符串。但是我不确定如何形成构造函数
以下是我目前的代码:
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);
}
}
}
公共类LString{
//2.字段
节点锋;
//节尾;
整数大小;
//1.节点类
私有类节点{
字符数据;
节点下一步;
//建设者
//1.违约
公共节点(){
}
//2.数据
公共节点(char newData){
this.data=newData;
}
//3.数据+下一步
公共节点(char newData,node newNext){
this.data=newData;
this.next=newNext;
}
}
//3.施工人员
公共电话号码(){
此值为0.size=0;
this.front=null;
}
公共LString(原始字符串){
}
//4.方法
公共整数长度(){
返回此.size;
}
公共整数比较(另一个字符串){
返回0;
}
公共布尔等于(对象其他){
if(other==null | |!(LString的其他实例)){
返回false;
}
否则{
LString otherLString=(LString)other;
返回true;
}
}
公共字符(整型索引){
返回“a”;
}
公共void setCharAt(int索引,char ch){
ch='a';
}
公共LString子字符串(int开始,int结束){
返回null;
}
公共lStr更换(int start、int end、lStr){
返回null;
}
//附加
公共void追加(字符数据){
这个.size++;
if(front==null){
前端=新节点(数据);
返回;
}
节点电流=前端;
while(curr.next!=null){
curr=curr.next;
}
curr.next=新节点(数据);
}
//预编
公共空预结束(字符数据){
/*节点温度=新节点(数据);
温度下一个=前;
前=温度*/
前端=新节点(数据,前端);
大小++;
}
//删除
公共无效删除(整型索引){
//假设索引是有效的
如果(索引==0){
front=front.next;
}否则{
节点电流=前端;
对于(int i=0;i
许多方法都是存根,因此该类将使用另一个测试文件进行编译。但是我不认为我需要包含它来发现问题
谢谢你的帮助。你可以用不同的方式构建你的
LString
构造函数。我能想到的一种方法是接受char[]
并将其存储在内部LinkedList中。您可以在中查看String
构造函数以获得更多想法