【问题标题】:How to create a linked list in java without importing linked lists如何在不导入链表的情况下在java中创建链表
【发布时间】:2021-03-12 20:42:28
【问题描述】:

我是一名大学计算机科学专业的学生,​​我在这个项目中遇到了麻烦,我想在不导入链表的情况下使用节点创建链表,以及对列表执行一些方法。在编码方面我是初学者,所以假设我什么都不知道,因为可能就是这种情况哈哈。

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


    }
}

感谢您提前提供的任何帮助或指示。我们正在使用一个单独的节点、帮助程序和驱动程序类以及一个 .txt 文件来分配到我的列表中。我也被困在 concat 和 substring 方法上,但我想确保我首先得到正确的框架。再次感谢。

【问题讨论】:

  • 你没有说你要解决的问题是什么。
  • 我给出了我的整体问题并挑出了我的主要问题,即使用节点设计链表。但这不是我唯一的问题,只是我最大的问题,抱歉没有澄清。我很难说出我的问题。
  • 这里的问题是 - 这看起来像一个非特定的代码清单和一般的“找出这个问题”之类的问题。我可以找到一堆错误的东西(单独的 concat 方法是......有问题的),但我不想猜测哪个是实际问题,哪个只是一些错误的代码。

标签: java linked-list nodes


【解决方案1】:

如果我正确理解您的问题,您是在询问如何在不导入特定类型的情况下访问它。

当使用简单名称时,需要使用导入来识别引用的类型。要引用类型而不在导入中声明它,您需要使用其完全限定名称。比如

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

无需导入 List 和 ArrayList 即可工作,因为通过声明类所在的包,可以明确引用哪个类。

【讨论】:

  • 我们的任务是使用节点设计我们自己的链表,这能回答您的问题吗?我的教授不希望我们使用 java 中已经存在的东西,而是使用节点来创建链表而不使用 java 已经给我们的链表
  • @Premsanity 是的,可以回答我的问题,在这种情况下,我的回答当然对您没有帮助。请更新您的问题,使其更清楚,以免其他人遇到同样的误解。
【解决方案2】:

我稍后会尝试编写代码,但我发现这本书可能对你有所帮助。

https://cin.ufpe.br/~grm/downloads/Data_Structures_and_Algorithms_in_Java.pdf 我从Pearson公司买了一本关于DATA STRUCTURE的书,确实是一本好书,但是我记不太清了,大概是这样的,我匆匆忙忙的:

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

【讨论】:

  • 这绝对有帮助,我会看看这本书并使用你的代码作为参考,谢谢!
  • 嘿,@Premsanty,如果你看一下 PDF,你可以在第 152 页找到一个已经用 java 制作的类(嗯......我猜),有一些解释和图像有助于更好地理解,如果您认为答案符合问题,请点击左侧的按钮(正确的符号)。 :)
猜你喜欢
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多