[英]invalid method declaration; return type required for a linked list
我在開發鏈接列表類以模仿Java中的標准字符串和字符串生成器類時遇到了麻煩。
我正在嘗試學習如何使用和操作鏈表,並且我想創建一個名為LString
的類,該類是由字符的鏈表而不是數組構成的字符串對象。
到目前為止,這是我對設置鏈接列表類的理解:
public class LString {
//Fields
node front;
//node tail;?
int size;
// Node class
private class node {
char data;
node next;
//constructors
//default
public node (){
}
//data
public node (char newData){
this.data = newData;
}
//data + next
public node (char newData, node newNext){
this.data = newData;
this.next = newNext;
}
// Constructors
public LString(){
this.size = 0;
this.front = null;
}
//Methods
//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 (int data){
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, int 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.java:41: error: invalid method declaration; return type required
public LString(){
通過添加如下內容,我已經看到了針對此問題的其他解決方案:
public static void main(String[] args) {
LString lstring = new LString();
}
但這對我沒有用。 任何幫助表示贊賞。
您需要關閉node
內部類的括號。 在給定的代碼中, public LString()
函數在node
類中定義,因此它應該具有返回類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.