pootduke9000
pootduke9000

Reputation: 11

Function won't reference linked list with nodes I'm creating

Reading a book on Java data structures over the summer, and I'm having a hard time with the fundamentals of linked lists and creating linked lists with nodes.

Specifically, I'm trying to figure out how to pass a file of unsorted integers into a function, sort them, and print the sorted linked list. I know I'm probably making this more complicated than it needs to be, but I just don't understand how, exactly, these nodes are linked, and how to return them.

I figured I could create a list object and use it as a reference to the first node, which in turn would reference the preceding node until end of the list, which is signified with null. This is logic I'm trying to follow from the book, but something isn't clicking.

I'm getting an error that my readFile method is undefined when I call it in main. How is that possible?

import java.util.Scanner;
import java.util.List;

public class Node {
    int value; 
    Node next; 

    /** 
     * Constructor 
     * @param value The element to store in this node, an integer from file
     */
    public Node(int value) {
        this.value = value;
        next = null; 
    }

    public static Node first;
    public static Node last; 

    public void linkedList() {
        first = null; 
        last = null;
    }

    /** 
     * function sorts integers 
     * @param inputFile file of integers from user
     * @return linked list of sorted integers 
     */
    public static Node readFile(Scanner inputFile) {

        while (inputFile.hasNext()) {
            int data = inputFile.nextInt(); 
            if (first == null) {
                first = new Node(data);
                last = first; 
            }
            if (data < first.value) {
                first.next = first; 
                first = new Node(data);
            }
            if (data > first.value) {
                last.next = new Node(data); 
                last = last.next; 
            }
        }
        return first; 
    }

    /**
     * function prints linked list
     */
    public void print() { 
        Node ref = first; 

        while (ref != null) {
            System.out.println(ref.value + " ");
            ref = ref.next;
        }
    }
}

This is my main driver:

    import java.util.Scanner;
    import java.util.List;
    import java.io.*; 

    public class listTesting {
    private static final String FILENAME = "numbers.txt"; 

    public static void main(String[] args) throws IOException {

        Scanner keyboard = new Scanner(System.in); 

        System.out.print("Opening " + FILENAME);
        File file = new File(FILENAME);
        Scanner inputfile = new Scanner(file);

        linkedList list = new linkedList(); 

        // error here stating this method is undefined ??
        list = readFile(inputfile);

        System.out.println("\nThe sorted linked list: ");
        list.print(); 
    }
}

Upvotes: 1

Views: 161

Answers (1)

Ivan Makhnyk
Ivan Makhnyk

Reputation: 106

In class listTesting is no any linking to Node class. For this line

list = readFile(inputfile);

compiler tries to find static method in listTesting class.

Next item that is not clear for me:

linkedList list = new linkedList(); 

// error here stating this method is undefined ??
list = readFile(inputfile);

You create list variable with default constructor, but just after is changing it with result from readFile method. Also in Node class readFile returns Node class (not linkedList).

I see that you are at the very beginning in Java, so please read book on Java more carefully (even try to repeat Java basics before data structures).

Upvotes: 0

Related Questions