• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Use of this key word and logic in LinkedListNode class

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyne explain how line 22 and 25 works ? Besides, I didn't get
this==last logic
Full url: LinkedListNode

 
Marshal
Posts: 74020
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are the methods in that class marked public? Why is the class a top‑level public class? I would make the node a nested class in the linked list class, because it is probably unnecessary to call the node's method from any other code. All you achieve by making things public is exposing implementation details of the list. And making fields public is even worse.
Do you have a diagram of a linked list with arrows showing the relationships between nodes? If you use previousXXX(), you have a doubly‑linked list, which is the usual kind used as a list. Your diagram should have boxes/circles on, joined together by arrows, like ←previous and next→. What the method you quoted does is to set the previous reference to a new node. That new node should have a next reference, which should point to the node you called that method on. When you call a method reference and wish to refer to the current object, you write this.

[edit]Lots of spelling corrections and correct the ‑ tag to hyphen.
 
Sheriff
Posts: 26775
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From its name one might guess that the variable "last" was supposed to represent the last node in the linked list. That would make sense if it were declared static, since "last" is an attribute of the list and not an attribute of any node.

And, incredibly, it looks like that's what it is supposed to be. Only every node has its own link to the last node in the list and attempts to maintain it -- at least I think that's what it is trying to do there in the setNext() method. There's no similar code to deal with the first node in the linked list, which makes me wonder what is the point of trying to keep track of the last node.

Campbell has already pointed out the many faults of that code, to which I would add line 41's clumsy way of converting an integer to the corresponding String. (Better: "return String.valueOf(data)")

There's also a rule for linked lists that you should be able to traverse the list from start to finish while passing through each node exactly once. This code allows you to add a node without checking that the rule is followed; if you're going to write a constructor which allows you to specify both the next and previous nodes of a new node, it should at least check that the nodes you designated as "next" and "previous" are indeed adjacent in the list and the "previous" node is already the previous node of the "next" node. But it's better to not write such a constructor.

But it's still worthwhile asking questions like yours even when the code you're looking at is flawed. The way to understand how code about linked lists works is to draw pictures. A node looks like a box with an optional arrow pointing to the previous node and another optional arrow pointing to the next node. So work your way through some sample code which creates nodes and draw the corresponding pictures with boxes and arrows.
 
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think this is OP's code. From what's in the GitHub repo, that code is from Cracking the Code Interview, 6th Edition by Gayle Laakmann McDowell.

I didn't get the this==last logic


You can read that line of code quite literally as "if this node is the last node"—"this" refers to the current LinkedListNode object, the one on which the setNext() method is being invoked. The last variable is meant to keep track of the last node in the linked list, otherwise known as the "tail" of the list as opposed to the first node in the list which is also known as the "head" of the list.
 
Campbell Ritchie
Marshal
Posts: 74020
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . Campbell has already pointed out the many faults of that code . . .

Obviously not all of them. You are correct that the “last” node is an attribute of the whole List, not the node.

But it's still worthwhile asking questions like yours even when the code you're looking at is flawed. . . .

Agree; I think you can learn more from seeing how had code goes wrong than from beautiful code which runs correctly first time.

I see I introduced some spelling and formatting errors in my post; I shall go back and correct them. ‑ is the HTML tag for a non‑breaking hyphen.
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We've seen this design before. Yes, it's weird because the class ostensibly represents a list node but in actual use, a node can be a reference for the entire list itself. For anyone who thinks in a more object-oriented way, this design is very confusing and counterintuitive, in my opinion.

There's an example of how it might be used.
 
Junilu Lacar
Marshal
Posts: 16591
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this code comes from a book that's about code interviews, I'd imagine some of the goofiness in the design is intentional and meant to see if the candidate could discern and correctly articulate problems in the code.
 
Saloon Keeper
Posts: 13268
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a more pessimistic outlook. Many people in the industry are bad at application design. Some of those write books.
 
Campbell Ritchie
Marshal
Posts: 74020
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The OO way I would write a linked list would look something like this:-
  • 1: Public top‑level class called LinkedList<E> or similar
  • 2: Private (static) nested class called Node<E>.
  • 3: List has references to the first and last Nodes and constructor creating them both pointing to null  = empty List, and size attribute.
  • 4: Node has three fields: Node previous, Node next, E value. The “end” Nodes have next/previous fields pointing to null.
  • The procedural theoretical programming design for a linked list is different:-
  • 1: Node with pointers to previous, next and value.
  • 2: You iterate the list until you encounter next == null.
  • Such a linked list has no distinction between any of the nodes in its tail (=everything except the first (=“head”) node), and it is often only possible to reach the last node by traversing the entire list until next == null. There is also no distinction betwen the “head” node and the whole list, so there isn't a list data structure as such.

    And woe betide anybody who gets the same node into a linked list twice. That will mean there is no last node and iterating the entire list will take an infinite duration
     
    Junilu Lacar
    Marshal
    Posts: 16591
    277
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:I have a more pessimistic outlook. Many people in the industry are bad at application design. Some of those write books.


    Looking at more of the examples that use LinkedListNode, I'd lean the same way with this one. I also see that the book tries to cover many different programming languages. I suspect the author is just trying to repurpose code from a more procedural style language, not bothering to make adjustments to make it more object-oriented. This is what you get when you do that.

    @OP: You'd be wise to take the Java code in that book with a grain of salt. Those are not good examples of well-designed Java programs.
     
    The fastest and most reliable components of any system are those that are not there. Tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic