简体   繁体   English

方法声明无效; 链表所需的返回类型

[英]invalid method declaration; return type required for a linked list

I'm having trouble developing a linked list class to mimic standard string and string builder classes in Java. 我在开发链接列表类以模仿Java中的标准字符串和字符串生成器类时遇到了麻烦。

I'm trying to learn how to use and manipulate Linked Lists, and I want to make a class called LString that is a string object made from a linked list of characters, instead of arrays. 我正在尝试学习如何使用和操作链表,并且我想创建一个名为LString的类,该类是由字符的链表而不是数组构成的字符串对象。

So far, this is how I understand to set up a Linked List class: 到目前为止,这是我对设置链接列表类的理解:

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

I am receiving this error message: 我收到此错误消息:

LString.java:41: error: invalid method declaration; return type required
public LString(){

I have seen other solutions to this problem by adding something like this: 通过添加如下内容,我已经看到了针对此问题的其他解决方案:

public static void main(String[] args) {
  LString lstring = new LString();
}

but that didn't work for me. 但这对我没有用。 Any help is appreciated. 任何帮助表示赞赏。

You need to close the bracket of node inner class. 您需要关闭node内部类的括号。 In the given code, public LString() function is defined inside node class so it should have return type. 在给定的代码中, public LString()函数在node类中定义,因此它应该具有返回类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM