Reputation: 37
As per the definition addLast(), the addLast() method is used to add element to the last of the list. But here in the following code that's not happening.
LinkedList<Integer> demo = new LinkedList<>();
demo.addLast(15);
demo.addFirst(1);
demo.add(10);
System.out.println(demo);
The output is
[1, 15, 10]
But as per the definition the output should have been
[1, 10, 15]
Because the addLast() method should be adding 15 to the last of the list. I'm not sure what exactly is the issue here.
Code snippet : Code snippet
Upvotes: 0
Views: 927
Reputation: 13
addLast adds an item at the end of the list - it doesn't mean it will remain at the end of list when additional items are added.
you start with an empty list.
addLast(15)
will add 15 at the end of this list, so it's now [15]
addFirst(1)
will add 1 at the beginning of the list, so it's now [1, 15]
add(10)
will add 10 at the end of the list, so it's now [1, 15, 10]
Upvotes: 0
Reputation: 186
well as Obvious as this can be the add()
adds at the last of the list and return a boolean indicating that the addition was successful or not.
yet on the other hand the addLast()
method adds on the last but its void method so it wont return you a boolean.
Appends the specified element to the end of this list.
This method is equivalent to add(E).
from the javaDoc of the add()
Appends the specified element to the end of this list.
This method is equivalent to addLast(E).
but one might prefer to use add()
for multiple reasons:
add(int index, E element)
:
which will add a certain element to certain index.and from javaDoc of the add(int index, E element)
Inserts the specified element at the specified position in this list.
Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
and this method will throw an
IndexOutOfBoundsException
if the index is out of range (index < 0 || index > size())
so thats being said all what you are doing is the following:
demo.addLast(15);//your list now looks like this 15-> null
where 15
is the head and the tail in this case.
demo.addFirst(1);/your list now looks like this 1->15->null
now you have 1
as the head/leading element and the 15
as tail/trailing element.
and finally for
demo.add(10);//your list now looks like this 1->15->10->null.
for further informations here is how the addFirst
and addLast
works in the background:
addFirst
will call the linkFirst
private void linkFirst(E e) {
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f);
first = newNode;
if (f == null)
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
addLast()
will call the linkLast
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
I highly suggest that you do an implementation so you could build a better understanding of the whole concept. happy learning.
Upvotes: 1
Reputation: 140
The explanation would be the following:
When you do demo.addLast(15)
considere your list is empty, so you just have [15]
after that you did demo.addFirst(1)
, according the definition it is going to insert at start, so you have [1, 15], at last you did demo.add(10), it insert at final so you have [1, 15, 10]
, when you print it will be [1, 15, 10]
.
Upvotes: 0
Reputation: 178411
addLast()
does add 15 to the end of the list, it transfers it from []
to [15]
. Later you use add()
, which appends another element to the end of the list (after 15
).
From the documentation of add():
public boolean add(E e) Appends the specified element to the end of this list. This method is equivalent to addLast(E).
Upvotes: 1
Reputation: 311073
addLast
adds an item at the end of the list - it doesn't mean it will remain at the end of list when additional items are added.
Let's track the code:
addLast(15)
will add 15 at the end of this list, so it's now [15]addFirst(1)
will add 1 at the beginning of the list, so it's now [1, 15]add(10)
will add 10 at the end of the list, so it's now [1, 15, 10]Upvotes: 2