0

In the following code, I am trying to understand one thing in the insertFirst() method that

Why is the last statement first =newLink; and not first.next=new Link; Will it be wrong? Isn't there a "next" in first?

I know this code is right and I know that a node needs to be inserted at the beginning and all, but I need help understanding just one statement.

Is first =newLink; and first.next=new Link; not the same thing?

public class LinkedList {

    private Link first;

    public LinkedList()
    {
        first = null;
    }

    public boolean isEmtpy()
    {
        return(first==null);
    }

    public void insertFirst(int id, int dd)
    {
        Link newLink=new Link(id,dd);
        newLink.next=first;
        first =newLink;
    }


}
1
  • remember that first was already null. it doesn't have a next. Commented Mar 29, 2011 at 18:40

5 Answers 5

3

No, it's right: the list inserts new links at the beginning. The old "first" becomes the new link's "next", and the new link is the new "first".

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

3 Comments

I know this code is right and I know that a node needs to be inserted at the beginning and all, but I need help understanding just one statement. Is first =newLink; and first.next=new Link; not the same thing?
No. Think of each Link as a box which holds two things: a number, and a pointer to another box "next". The list itself has a pointer named "first" which points to the front of the list, and each node then points to its neighbor. If we did "newLink.next = first.next; first.next=newLink" that would mean that first is pointing to a box (call it "A") and then A is pointing to the new link, and the new link is pointing to what "first" was originally pointing to. On the other hand, "newLink.next = first; first = newLink" means that first is pointing to the new link, and newLink is pointing to A.
"first" is a pointer to the first link -- the first box. Only the boxes have numbers in them.
2

Why is the last statement first =newLink; and not first.next=new Link;

Because you're inserting a new first element and the "next" element is the old first element, which was set on the previous line.

Is first =newLink; and first.next=new Link; not the same thing?

No. first is the first and first.next is the second.

1 Comment

Thanks that is the answer I was looking for. Just about that one statement mainly.
0

This is because you want to put new element to the beginning, so you must set new element to the head of list and this element should point on "old-head", and then you have:

new_elemnt->old_head->...

Comments

0

LinkedList::first is not a guard element. It really points to the first element of the list. If LinkedList::first == null, then the list is empty. If Link::next == null, then it's the last element (the null is called a guard element in this case).

Comments

0

Simple example of SingleLinkedList in Java

    package com.ds;

    public class SingleLinkedList {


        private Node head;

        public static void main(String[] args) {
            SingleLinkedList linkedList = new SingleLinkedList();
            linkedList.insert(5);
            linkedList.insert(15);
            linkedList.insert(45);
            linkedList.insert(55);
            linkedList.insert(58);
            linkedList.insert(25);

            // Print value of Single Linked list.
            linkedList.print();
            // delete node from tail side.
            linkedList.delete();
            linkedList.delete();
            linkedList.delete();
            linkedList.delete();
            linkedList.delete();
            /*linkedList.delete();
            linkedList.delete();
            linkedList.delete();
            linkedList.delete();*/
            linkedList.print();

        }

        SingleLinkedList() {
            head = null;
        }

        void insert(int val) {
            Node temp = new Node();
            temp.data = val;
            temp.next = null;
            if (head == null) {
                head = temp;
            } else {
                Node k = head;
                while (k.next != null) {
                    k = k.next;
                }
                k.next = temp;
            }
        }

        // delete from tail.
        void delete() {
            // if it's first node
            if (head == null || head.next == null) {
                head = null;
            } else {
                Node n = head;
                Node t = head;
                while (n.next != null) {
                    t = n;
                    n = n.next;
                }
                t.next = null;
            }

        }

        void print() {
            Node k = head;
            while (k != null) {
                System.out.println(k.data);
                k = k.next;
            }
        }

       Node reverse() {
            Node h = head;
            Node p = null;
            Node t = null;
            while (h != null) {
                t = h.next;
                h.next = p;
                p = h;
                h = t;
            }
            return p;
        }

        class Node {
            private int data;
            private Node next;
        }
    }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.