When to use size == 0 or isEmpty() on a linkedlist implementation
I'm reading a book called Fundamentals of Data Structures. In the firs chapter is an implementation of a linkedlist. There is something in the implementation that is perplexing me and I think it's something to do with the philosophy of object-oriented programming. As the linkedlist grows or shrinks an int size datamember is updated. My question is, why does "addFirst()" method directly test the value of size against 0, while "addLast()" method uses the "isEmpty()" method which does exactly the same test? See code below. public void addFirst(E e){ head = new Node<>(e, head); if (size == 0) { tail = head; } size++; } public void addLast(E e){ Node newest = new Node<>(e, null); if(isEmpty()) head = newest; else tail.setNext(newest); tail = newest; size++; }
Take Your Experience to the Next Level
NewDownload our mobile app for a faster and better experience.
Comments
0U
Join the discussion
Sign in to leave a comment