0

folks, the implementation of the LinkedList causes me a little of misunderstanding. I know how to implement all of the methods of the LinkedList but I do not understand why the class Link contains its own object inside itself (inside the class Link). Could smb please explain why class Link contains its own object inside it? Is there any analogy? Thanks in advance!

Class Link

package LinkedList;

public class Link {

    public String data;
    public Link next;

    public Link(String data){
        this.data = data;
        this.next = null;
    }

    public void display(){
        System.out.println(data);
    }
}

Class `LinkedList`

    class LinkedList{

        public Link firstLink;

        public LinkedList(){
            this.firstLink = null;
        }

        public void insert(String data){
            Link myLink = new Link(data);
            myLink.next = firstLink;
            firstLink = myLink;
        }

        public Link deleteFirst(){
            Link deletedLink = firstLink;
            firstLink = firstLink.next;
            return deletedLink;
        }

        public boolean search(String data){
            Link current = firstLink;
            while(current != null){
                if(current.data.equals(data)){
                    return true;
                }
                else{
                    current = current.next;
                }
            }
            return false;
        }

        public void displayList(){
            Link current = firstLink;
            while(current != null){
                current.display();
                current = current.next;
            }
        }

    }

Class LinkedTester

package LinkedList;

public class LinkTester {

    public static void main(String[] args){
        LinkedList theLinkedList = new LinkedList();
        theLinkedList.insert("A");
        theLinkedList.insert("B");
        theLinkedList.insert("C");
        theLinkedList.insert("D");
        theLinkedList.insert("E");

        theLinkedList.displayList();
        System.out.println();
        System.out.println(theLinkedList.search("A"));
    }
}
2
  • That's the basic idea of a linked list. Have a look at this Wiki-Article. The inner list is not an object, but a reference! Commented Jul 27, 2015 at 22:54
  • Think of the chain like a line of elephants, with each elephant holding the tail of the one ahead of it. You ask an Elephant what he's holding onto, and he answers, "the next Elephant." Same idea, but with Links instead of Elephants. Commented Jul 27, 2015 at 23:10

1 Answer 1

2

Well, you need to understand that when you declare

public link next;

next is not an object of the class link, it is a variable that can point to the object of class link and when you do next = new link(); that is when an object is created and next starts pointing to an object.

So, what this actually means is, your class list does not contain an object of it own type, but contains a variable that can point to an object of the list type, which is the basic concept of Linked List (One node pointing to another node, forming a continuous chain)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.