简体   繁体   中英

How to create a linked list in java without importing linked lists

i am a CS student in college and i am having trouble with this project where i am supoosed to create a linked list using nodes without importing linked lists, as well as doing a some methods with the list. i am a beginner when it comes to coding, so assume i know nothing, because that is probably the case lol.

import java.io.*;
import java.lang.*;

public class List {
    public int listCount = 0;
    public char[] linkedList;

    public List() throws FileNotFoundException {

    }

    public List(char[] array) throws FileNotFoundException {
        if (array.length == 1) {
            Node head = new Node(array[0]);
        } else if (array.length > 1) {
            Node head = new Node(array[0]);
            Node traverse = head;
            for (int i = 1; i < array.length; i++) {
                while (traverse.nextNode != null) {
                    traverse = traverse.nextNode;
                }
                traverse.nextNode = new Node(array[i]);
                listCount++;
            }
        }

    }

    public List(String w) throws FileNotFoundException {
        char[] array2 = new char[w.length()];

        for (int i = 0; i < w.length(); i++) {
            array2[i] = w.charAt(i);
        }
        List str = new List(array2);
    }


    /* Find the character at a index
    @param int index
    return the character at the chosen index
    */
    public char charAt(int index) throws IndexOutOfBoundsException {
        char results = linkedList[0];
        if (index < linkedList.length && index >= 0) {
            results = linkedList[index];
        }

        return results;
    }

    public String concat(int index1, int index2) {

        return null;
    }

    /* Determine if the list is empty
    return whether the given conditions are true or false
      */
    public boolean isEmpty() {
        for (int i = 0; i < linkedList.length; i++) {
            if (!linkedList.equals(null)) {
                System.out.println("This list is not empty");
                return false;
            }
        }
        System.out.println("List is empty");
        return true;

    }

    /* Determine the size of the list
    return the size of the list
     */
    public int size() {

        return listCount;
    }

    /* Create a new String between 2 index's including the start and end index
    @param beginIndex is the starting point of the new String
    @endIndex is the ending point of new String
    return the new String
     */
    public String subString(int beginIndex, int endIndex) {

        return null;
    }

    public void insert(Object x) throws IndexOutOfBoundsException {
        if (listCount > 100 || listCount < 0) {
            throw new IndexOutOfBoundsException("Bag is too large");
        } else {
            this.linkedList[listCount] = (char) x;
            listCount++;
        }


    }
}

i appreciate any help or pointers ahead of time. we are using a separate node, helper, and driver class as well as a.txt file to assign into my list. i am stuck on the concat and substring methods too, but i want to make sure i am getting the framework correct first. thank you again.

If i understand your question correctly, you are asking how to access a specific type without importing it.

Imports are required to identify which type is referenced when it is used by its simple name. To reference a type without declaring it in the imports you need to use its fully qualified name. For instance

java.util.List<String> someList = new java.util.ArrayList<>();

works without importing List and ArrayList because by declaring the package the class is in it is clear which class is being referenced.

I'll try to do the code later, but here is a book that i found that may help you.

https://cin.ufpe.br/~grm/downloads/Data_Structures_and_Algorithms_in_Java.pdf I bought a book about DATA STRUCTURE from Pearson company, and it's really a good book, but i don't remember much, it's something like this, that i did in a hurry:

public class List {

    private Node head = null;
    private Node foot = null;

    private Node newNode = null;
    private Node auxNode = null;
    
    public List() {
        this.head = new Node();
        this.foot = new Node();
    }
    
    public class Node {
        private int adress;
        private Node nextNode;
    }

    public void add(int value) {

        this.newNode = new Node();
        newNode.adress = value;

        if (head == null) {
            // Head of the list receive the values of the NEW NODE, so the head of the list
            // is not null enymore
            head = newNode;
            head.nextNode = null;

        } else {
            // In this case Head is not null
            /*The auxiliary node will receive the head and the new Node will become the new Head from the list*/
            auxNode = new Node();
            auxNode = head;
            /*
            while(auxNode.nextNode != null  ) {
                
            }
            
            auxNode = head;
            //head of the list is empty, so we can add the new node
            head = newNode;//Here the new node is empty because was transfered to the head
            head.nextNode = auxNode; //The head of the list receive the old node that used to be the head
            
            
            if (head.nextNode == null) {
                
                head.nextNode = newNode;
                
            } else if (head.nextNode != null) {

            }*/

        }
    }

}
```

I hope this help you to get some lead

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