简体   繁体   中英

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.

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.

The wikipedia entry on linked lists might be a good place to start: 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:

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. It allows you to create the list node as showin in the code Listnode<String> l; The benefit of generics is that you can just as easily create a Listnode of integers. 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. You can now also see that the next value is null as indicated by a "\\"

You can also set/change values at will.

在此处输入图片说明

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