Tuesday, May 6, 2014

Combining Single Linked List Operations (Object Orientated Implementation)

Note - The code examples used in this post are located at my GitHub account - OO Single Linked List

Over the past few posts, we've described and shown the implementation of the three basic linked list operations. Each post covered an operation (searching, insertion and deletion) with code individually detailing how it was performed without interference from the others.

Now that we know about all three operations and how they work, the last thing to do is to combine them all so we can make a properly functioning single linked list. 

If you were following through the code examples, you would have noticed that the language chosen for the implementation was Java, so it'd be fitting that we also utilise some OO (Object Orientated) concepts when we combine the operations together.


Combining Operations - 

To build a fully functioning linked list we must combine all the operations together into a single place. The process of that isn't overly difficult since the methods that we've implemented individually so far can be all placed into a single class with little to no modification. So long as the operations are all still referencing the same Node object, it shouldn't be much of a hassle.

Object Orientated Implementation - 

One of the main ideas behind OO is the creation of classes for reusability. What we'd ideally like to achieve is the creation of a code "blueprint", a template of a particular piece of code that can be created over and over again. 

Going through the traditional example, imagine an class as a car. Every car has basically the same properties (ie. wheels, number of doors, engine). By defining a class with these properties, we've essentially created a "blueprint" of a car, meaning that with this template we can create a physical car. The physical car itself is otherwise known as an object.




Figure 1 - A class is a template, the physical implementation of a car is an object.

Another important concept here is encapsulation. The idea with encapsulation is to hide data and/or its state from being directly accessed from external sources. In other words, encapsulation would be the concept of hiding your thoughts and feelings (data and state) from being directly accessed by others without your permission. 

These two concepts are important features that we'd like to achieve with our OO implementation of a single linked list and to summarise we'd like to achieve the following -
  1. The creation of a single linked list template, so we can create multiple list objects.
  2. The capability to hide the data and prevent direct access to the items in our list.
The Single Linked List Class - 

Note - Please refer to the code example SingleLinkedList.java

To create our template, we'll have to move all the operations that we've previously created into a single class. By doing this, our three operations are combined into a single place to be accessed, this is going to be our template, every time we want to create a new list we are creating it from this template.

You'll notice that there isn't much difference here with minor changes to the method names and some public accessor methods added to allow for operations to be performed.

The idea here is to hide as much of the inner workings of the linked list within this class such that external classes do not have to worry about the how it works to be able to use it.

Encapsulating The Node Data - 

External classes that use our linked list class do not need to know about how the data is stored. All they should be concerned about is the ability to be able to insert, search and delete items from the list (We've also thrown in a print in there for them). This means that we can hide the Node data and state from the external world and keep that to the class itself, which is why we've created the Node class as a inner private class of our SingleLinkedList

By doing so, there is no way that we can directly access the data contained within our Node, meaning that we must ask the SingleLinkedList to retrieve the data for us. 

Summary -

Combining the three basic operations of a Single Linked List is a pretty straight forward process. Since we've already done the hard part by implementing each one separately, it really is just a matter of putting them all in the same place (in a class)

With a language like Java, the idea is to create "blueprints" (classes) to ensure code reusability and to restrict direct access to data (encapsulation) by external sources. 

Exercises - 

  1. Referring to the source code for the three linked list operations here, combine the methods together and create your own OO Single Linked List.

No comments:

Post a Comment