简体   繁体   中英

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.

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.

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. In the given code, public LString() function is defined inside node class so it should have return type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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