Quick Index
Overview


We'll go over getting the first and last data items in the list.

When reading the examples pretend to "be the linked list".

Examples


Return Data (Not Node)
Here is a sketch of our node

Note:
  • Assume "message sender" is the sender of the method "first", "last", etc
  • The message sender will expect to get data (not a node) -- in this case the data is a City object
  • Our nodes are private -- the message sender doesn't need to know about them

It is easy for us to return data (not a node) -- we can simply use: "aNode.getData()".
first
From the previous sketch we know how to get data from a node.

How would we get the first element (data)? [For the example shown, we would expect first to be the City object "BP"]



//Assume we are coding inside //the LinkedList class (our context) let node = this.firstNode; let data = (node != null) ? node.getData() : null;
last
How would we get the last data (element)? [For the example shown, we would expect last to be the City object "NH"]



//Assume we are coding inside //the LinkedList class (our context) let node = this.lastNode; let data = (node != null) ? node.getData() : null;