简体   繁体   English

我需要创建自己的简单LinkedList

[英]I need to create my own simple LinkedList

I don't understand how to add many components to my List and how to address them. 我不明白如何向List添加许多组件以及如何解决它们。

public class Element {
    public Object object;
    public int index;
    public Element(Object object){
        this.object = object;
    }
}

public class LinkedList {
    private Element element;
    private int nextLink = 0;
    private int prevLink;
    private int index;


    public void add(Object newElement) {
        nextLink++;
        prevLink++;
        this.index = nextLink - prevLink;
        if (nextLink == 0) {
            prevLink=0;
            this.index = 0;
        }
        else {
            prevLink++;
            this.index = nextLink - prevLink;}
            nextLink++;
            System.out.println(element);
            Element element = new Element(newElement);
            this.element = element;
            if (nextLink == 0) {
                prevLink=0;
                this.index = 0;
            }
            else {
                prevLink++;
                this.index = nextLink - prevLink;}
                nextLink++;
                System.out.println(element);
            }

        public void get(int index) {        
        }
        public void remove(int index) { 
        }
    }


    public class Main {
        public static void main(String[] args) {
            LinkedList linked = new LinkedList();
            linked.add("234");
            linked.add("255");
            linked.add("1");
            System.out.println(linked);
            linked.get(1);
        }
    }

I have a big problem, because I have only one element on my list and when I want to add new element, my old element rewriting. 我有一个大问题,因为列表上只有一个元素,并且当我想添加新元素时,我的旧元素会被重写。 I don't know how to correct this problem..:(((((( 我不知道如何解决这个问题。.:((((((

Generally in linked lists an element contains a reference to the next element in the list. 通常,在链接列表中,元素包含对列表中下一个元素的引用。 Your Element class doesn't seem to have any way to reference the next element in the list. 您的Element类似乎没有任何方法可以引用列表中的下一个元素。

The wikipedia entry on linked lists might be a good place to start: http://en.wikipedia.org/wiki/Linked_list 链接列表上的Wikipedia条目可能是一个不错的起点: http : //en.wikipedia.org/wiki/Linked_list

Each Element must contain a link to the next Element. 每个元素必须包含指向下一个元素的链接。 An index is unnecessary and counterproductive. 索引是不必要的并且适得其反。

Start by rewriting your Element class as: 首先将您的Element类重写为:

public class Element {
    public Object object;
    public Element next;
    public Element(Object object){
    this.object = object;
    }
}

I recommend creating something like this. 我建议创建这样的内容。 You need references to the next element. 您需要引用下一个元素。 As seen in the below code. 如以下代码所示。

E is a generic type. E是泛型类型。 It allows you to create the list node as showin in the code Listnode<String> l; 它使您可以按照代码Listnode<String> l;中的Listnode<String> l;创建列表节点Listnode<String> l; The benefit of generics is that you can just as easily create a Listnode of integers. 泛型的好处是您可以轻松地创建整数的Listnode。 Like this. 像这样。 Listnode<Integer> integerList;

public class Listnode<E> {
          //*** fields ***
            private E data;
            private Listnode<E> next;

      //*** constructors **/
    // 2 constructors
    public Listnode(E d) {
        this(d, null);
    }

    public Listnode(E d, Listnode n) {
        data = d;
        next = n;
    }

  //*** methods ***
    // access to fields
    public E getData() {
        return data;
    }

    public Listnode<E> getNext() {
        return next;
    }

    // modify fields
    public void setData(E d) {
        data = d;
    }

    public void setNext(Listnode<E> n) {
        next = n;
    }
}

DO THIS TO ACTUALLY IMPLEMENT IT ALL 这样做实际上实现了全部

http://pages.cs.wisc.edu/~skrentny/cs367-common/readings/Linked-Lists/list-ex1.gif

THIS CREATES A LISTNODE OF STRINGS 这会创建一个字符串的清单

NOW this code creates a new node. 现在,此代码将创建一个新节点。 This node is called 'l' and l points to a "node" that holds the value "ant" as a string. 该节点称为“ l”,l指向一个将值“ ant”保存为字符串的“节点”。 You can now also see that the next value is null as indicated by a "\\" 现在,您还可以看到下一个值为null,如“ \\”所示

You can also set/change values at will. 您也可以随意设置/更改值。

在此处输入图片说明

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

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