I am doing an assignment for school and I’m not sure how to check if my linked list is full
I have
public boolean isFull()
{
return back == null;
}
But I’m initializing both for null at the beginning so if the list is empty, this method would return true too.
I was thinking maybe
public boolean isFull()
{
if(!isEmpty())
{
return back == null:
}
}
What do you think??
The linked list should not have more than 7 items I forgot to mention that.
There’s no such concept as a “full” linked list, unless you’ve got a specific implementation which has an upper bound for size. Generally linked lists can grow as long as you want them to.
EDIT: Okay, now you’ve actually explained the aim – I would personally keep a count with the linked list. Many implementations use this so that you can access the size in O(1) complexity. Normally to find the size of a “vanilla” linked list you start at the head of the list and iterate over it, incrementing a count until you reach the tail. That’s an O(n) operation. Not too bad for 7 elements, but a pain for a million. Keeping a separate count is easy and cheap.