0

Simple Linked List

public class List_manager {
        Entry first;
        Entry last;
        public void add(String el) {
            if (isEmpty()) { first=new Entry(el); last=first; return; }
            new Entry(el,last);
        }

        public String get() {
            Entry temp=first;
            first=first.next;
            return temp.data;
        }

        public boolean isEmpty() {
            return first==null;
        }
        private class Entry {
            String data;
            Entry next;
            public Entry(String data,Entry to) {
                this.data=data;
                to.next=this;
                to=this;
            }
            public Entry(String data) {
                this.data=data;
            }
        }
    }

#The main class#

I added 3 element and list contains only 2... why?

  public class Main {
        public static void main(String[] args) {
            List_manager l=new List_manager();
            l.add("1");
            l.add("2");
            l.add("3");
            System.out.println(l.get());
            System.out.println(l.get()); // Why here output: "3"??
            System.out.println(l.get()); // here is an error occurs
        }
    }

I really don`t get why list contains 2 elements?
Why it ignores 2nd added element?

5
  • If you're trying to create a linked list you'd better get used to seeing NullPointerException. (You'd also better learn how to debug real fast.) Commented Nov 29, 2014 at 2:04
  • I need explanation on my probmlem... Commented Nov 29, 2014 at 2:09
  • You have a "null pointer", because some reference you're using is null. The exception stack points you to the exact line where this is happening, so you just need to examine that line and figure out which reference is null and why. This usually requires debugging -- either using a debugger or inserting System.out.println calls to dump the values coming into the problem statement. Commented Nov 29, 2014 at 2:13
  • And even experienced programmers will have trouble coding linked lists. Getting multiple NullPointerExceptions before the code was thoroughly debugged would be expected. Commented Nov 29, 2014 at 2:14
  • Here is a hint: the line to = this doesn't make sense. You need to account for the next field in the Entry constructor. Commented Nov 29, 2014 at 2:25

2 Answers 2

1

to=this; This sentence have no influence on variable 'last', because veriable 'to' is formal parameter, while variable 'last' is actual parameter. So, when you executed this sentence "to = this;" the value of
variable 'last' was not changed to next.That's mean variable 'last' always pointed to the first element.

my change is : new Entry(el,last); --> last = new Entry(el,last); Things look better.

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

Comments

0

Think about what your get method is doing. You already noticed some aberrant behavior with it.

public String get() {
    Entry temp=first;
    first=first.next;
    return temp.data;
}

What happens the first time I call this?

  • temp gets whatever first is pointing to
  • first is moved to its next element (RED FLAG)
  • temp's data is returned...

One problem is that you're moving your head reference around - this is a bad idea, since it means that you can never access the true first element in your list ever again.

Now on its own, even with this implementation, you should still be able to get the first element.

The above was just a red herring - although you should not be moving your head pointer around. This is the real problem. What happens on subsequent add calls to your list?

public void add(String el) {
    if (isEmpty()) {
        first = new Entry(el);
        last = first;
        return;
    }
    new Entry(el,last);
}

Only the first element inserted and the last element inserted are respected. All other entries after next are overwritten.

I suggest that you use a debugger to figure this one out, as it stems from a misunderstanding of a good approach to do this. You only want to insert things through your tail pointer once you have one element. Doing this through object creation only causes heartache and confusion.

For posterity, I'll leave you with a sample, verbatim implementation I wrote for a singly linked list implementation I did a while back. It describes a more viable approach to inserting into a list.

public void insert(E data) {
    Node<E> candidate = new Node<>(data);

    if(head == null) {
        head = candidate;
        tail = head;
    } else {
        tail.setNext(candidate);
        tail = tail.getNext();
    }
    size = size + 1;
}

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.