Coding conventions for Linked Lists - java

I have been implementing this interview question in Java . A fairly simple problem with an additional constraint of size :
Find the Nth Node from the end of a Linked List where the size of the Linked List is unknown?
I am not concerned with the solution to this problem,because I have already figured that out.
Instead, I want to know whether my implementation maintains the coding conventions which experienced coders maintain while coding a problem related to Linked Lists and it's implementation?.Here is my implementation of the above problem:
import java.io.*;
class NthNodeFromEnd<AnyType>
{
private Node<AnyType> head;
private Node<AnyType> pointer;
private class Node<AnyType>
{
protected AnyType item;
protected Node<AnyType> next;
}
void push(AnyType item)
{
if(isEmpty())
{
head = new Node<AnyType>();
head.item = item;
head.next = null;
pointer = head;
}
else
{
Node<AnyType> newNode = new Node<AnyType>();
newNode.item = item;
newNode.next = null;
pointer.next = newNode;
pointer = pointer.next;
}
}
boolean isEmpty()
{
return head == null;
}
AnyType printNthLastNode(int n)
{
Node<AnyType> ptr1 = head;
Node<AnyType> ptr2 = head;
for(int i =0;i<n;i++)
{
ptr1 = ptr1.next;
}
while(ptr1!=null)
{
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
return ptr2.item;
}
public static void main(String args[])
{
NthNodeFromEnd<Integer> obj = new NthNodeFromEnd<Integer>();
obj.push(1);
obj.push(2);
obj.push(3);
obj.push(4);
obj.push(5);
obj.push(6);
obj.push(7);
System.out.println("The nth item is = "+obj.printNthLastNode(5));
}
}
P.S. - I am aware of the fact that there is an inbuilt implementation of Linked List in Java, but I don't want to use that.I want to know whether this implementation of the problem is good enough or is there a better way to tackle Linked List related problems?

Regarding the code conventions:
generic types are typically defined as a single uppercase letter: E or T, but not AnyType, which looks like a concrete type.
operators should be surrounded by spaces, semi-colons followed by a space, etc. For example, for(int i =0;i<n;i++) should be for (int i = 0; i < n; i++)
A method printNthLastNode() should print the nth last node, not return it. A method returning it should be named getNthLastNode() or findNthLastNode(). BTW, this method doesn't return a node, but a value stored in the list.
methods should generally not be package-private. They should be public or private generally.
the usual convention in Java is to have opening curly braces at the end of the line, and not at the beginning of the next line.
your method printNthLastNode() will fail with a NPE if the list is empty or not large enough. A better exception type should be used to signal this problem.
the class should not import java.io.*, since it doesn't use any class from java.io. packages should generally not be imported. Classes should.
String[] args is more readable than String args[], and is more conventional.
the Node class should be static: it doesn't use any instance member of its enclosing type.
That said, the interviewer should see, with the code posted, that you understand how a linked list works and how pointers work, as well as generic types.

Related

How can I inverse a list of elements if I can only access to an element and his next one?

I have been given a "class" called LinkedList which has only one atributte
"Node first" which refers to the first element of a list. The way to access the other ones is that class "Node" has access to an element 'x' and his following one:
public class LinkedList<T> {
private static class Node<E> {
E elem;
Node<E> next;
Node (E elem) {
this.elem = elem;
this.next = null;
}
}
private Node<T> first;
So that, I have been ordered to do a method called "reverse" from class "LinkedList" which has to reverse the list. However, the difficulty of the exercise is that I can only re-link the attributte "first", I mean I cannot create auxiliar data structures and that type of help.
I have done this in order to achieve the last element of the list, but I dont know how to continue:
public void reverse () {
Node<T> aux = first.next;
while (aux.next != null) {
first.elem = aux.elem;
aux = aux.next;
}
first.elem = aux.elem;
}
It seems quite a theoretical question to me, so you might want to check Geek for Geeks first. Most of these questions that are used in programming courses are well explained there, including coding examples.
In this case, a well-known solution is to use three pointers: a current number, the previous number , and the next number; to keep track of nodes to update reverse links.
Check here: https://www.geeksforgeeks.org/reverse-a-linked-list/

Update value of a reference in recursive method - best practices

I am trying to see how I can update a reference variable in Java methods in a better way. I know that Java references are pass by value - in other words, if I change the value of the reference in a method, it wont retain it in the caller method. But at the same time, I am trying to see how I can deal with these situations better. In most cases we need to return value from a method in recursion, say its base case, just like below where I resort to maintaining a static variable to hold the new head of a linked list that is being reversed. What are the more sensible options that I can use here?.
public static LinkedList _head = null;
public static LinkedList reverseLinkedList(LinkedList head)
{
reverseLinkedListInternal( head );
return _head;
}
public static LinkedList reverseLinkedListInternal( LinkedList node )
{
if( node.next == null )
{
_head = node;
return node;
}
LinkedList tmp = reverseLinkedListInternal( node.next );
tmp.next = node;
node.next = null;
return node;
}
You just don't need the static variable here. You should be just using the return value, whereas at the moment it's simply being ignored.
This is the kind of solution I would write. Obviously this might not fall into the constraints of your exercise as it is written as if it were a method of a LinkedList, which would make a lot more sense in the real world. However the concept is the same, so hopefully it will help you to spot your mistakes.
public void reverse() {
reverseInternal(head);
}
public Node reverseInternal(Node node) {
if (node.next == null) {
return node;
}
Node reversedTail = reverseInternal(node.next);
reversedTail.next = node;
node.next = null;
return reversedTail;
}

Pointers in LinkedList in Java

I am studying the linked lists,and I'm having a little problem understanding references and pointers in Java (and also have some questions for which I can't find answer on the internet.) Namely,I have a class LinkedList that uses class Node like this:
public class LinkedList {
public Node head;
public LinkedList(int data) {
head = new Node(data);
}
}
and this is my Node class:
public class Node {
public int data;
public Node next;
public Node(int data2) {
data = data2;
}
}
I also have method toStringLL() inside LinkedList which looks like:
public void toStringLL() {
LinkedList l=this;
while(l.head.next != null) {
System.out.print(l.head.data+"->");
l.head = l.head.next;
}
System.out.print(l.head.data);
System.out.println("");
}
I don't understand why this toStringLL() changes my head of linked list,when I am iterating through it with l.head=l.head.next? Shouldn't my head stay the same?I thought that when I write LinkedList l=this,I can freely iterate through l,without affecting this(or am I wrong?) Now when I use Node n=this.head instead of LinkedList l=this,it works,but I have difficulty figuring out why that works and the previous doesn't.. Can someone explain to me the difference between these two?
I don't understand why this toStringLL() changes my head of linked list,when I am iterating through it with l.head=l.head.next?Shouldn't my head stay the same?
When you make the assignment
LinkedList l = this;
you are not creating a new LinkedList object. You are just defining a references to the existing LinkedList object.
Therefore, l.head = l.head.next does exactly the same as this.head = this.head.next. Both change your original list.
On the other hand, when you write
Node n = this.head
you are not changing the head of your List. You are declaring a variable that initially refers to the head of your List, but when you change that variable to refer to other Nodes of your list, the head variable of your list (this.head) remains unchanged.
When you set
l.head=l.head.next;
you change your head.

Optimal ways to Traverse through a LinkedList - Java

The Situation
I have a interview with TripAdvisor tomorrow and I decided for practice to create my own custom LinkedList. I'm trying to figure out the best way to traverse through it.
Primary Question: I have managed to traverse through my Linked List however I believe
there is a better way to do it. How would you traverse through it ?
Bonus Question: How do my overall classes look ? Is there anything I should/should not add ?
It seems to work fine but is it optimal ?
Bonus Question #2: Lastly I was wondering if anyonehad any insight to typical interview questions/concepts that I must know ?
Greatly appreciated.
Here are my Classes
// *********************************Node Class*******************************************
public class Node<T> {
Node<T> link;
T data;
public Node(T data) {
this.data = data;
link = null;
}
public T getData() {
return data;
}
public Node<T> getLink() {
return link;
}
public Node<T> setLink(Node<T> N) {
this.link = N;
return link;
}
public void setData(T newData) {
this.data = newData;
}
}
//****************************************Linked List Class*******************************
public class LinkedList<T> {
Node<T> head;
T data;
public LinkedList(){
head = null;
}
public void add(T data){
Node<T> newNode = new Node<T> (data);
newNode.setLink(head);
head = newNode;
}
//had problems printing out the data in the last node
public void traverse(){
Node<T> pointer;
pointer = head;
while (pointer.getLink()!=null){
System.out.println(pointer.getData());
pointer = pointer.setLink(pointer.getLink());
}
//Fixed problems For last node that doesnt get printed out
System.out.println(pointer.getData());
}
//Again is there a better way to do this ?
//Thanks
}
I would change your traverse function to be more like this:
public void traverse(){
Node<T> pointer = head;
while (pointer != null){
System.out.println(pointer.getData());
pointer = pointer.getLink();
}
}
Also it is common to represent the Node class as a private inner class of LinkedList because it is not typically needed anywhere else.
As far as the interview itself goes, traversal questions are more typical for binary-trees (eg. print out the elements in sorted order). LinkedList questions are more focussed on the remove/insert operations which both require careful attention to the edge cases (what happens when you remove the head for example). A more advanced LinkedList question would ask how to detect a cycle, I would make sure that I knew at least one method of doing this (have a look at the Tortoise and the Hare algorithm).
EDIT:
Algorithm questions will nearly always be from the following list:
String manipulation such as:
Reverse String
Count how many times each letter appears in a given String (use a Map for this)
LinkedList questions such as:
How to remove a node, pay close attention to edge cases such as removing the head
How to reverse a linkedList (make the Tail the Head)
Binary Tree questions such as:
In-order traversal
If there is a BTree balancing question you won't need to implement it, just understand that a completely unbalanced Binary Tree is simply a Linked List.
Understand that searching a balanced Binary Tree is O(log n) compared to a Linked List or a completely unbalanced Binary Tree which is O(n).
You will probably be asked to describe the complexity of the solution you just gave (big-O notation)
See this and this for questions related to Java itself

Assistance with creating java Linked Lists using only nodes

been working on this project for a while now with Java. It was recommended that I use a Linked List or an Array List for my program, which makes perfect sense. However, the professor says we must make and use our own Linked List utilizing Nodes. Despite a bit of research and asking around in class, working with Nodes has got me very confused. I'm sure it's something simple I am missing, but I am at a complete loss right now. Here is the class in which the List is stored (I think). It is titled Aircraft because we are creating a list to store multiple aircraft and some details associated with them (name of flight, speed, altitude, type of plane). I have a Main class (not listed) which the user interacts with - I've got that class pretty much taken care of.
package airTraffic;
public class Aircraft {
public static String name;
public static String type;
public static int speed;
public static int alt;
Aircraft nextCraft;
public Aircraft (String n, String t, int s, int a) {
name = n;
type = t;
speed = s;
alt = a;
}
public Aircraft() {
}
public static void setName(String n) {
name = n;
}
public static String getName (String lookUp) {
return name;
}
public static void removeName () {
//remove the flight - not sure what to place here
}
public static void setType (String t) {
type = t;
}
public static String getType () {
return type;
}
public static void setSpeed (int s) {
speed = s;
}
public static int getSpeed () {
return speed;
}
public static void setAlt(int a) {
alt = a;
}
public static int getAlt () {
return alt;
}
public Aircraft next = null;
//auto generated method from ATControl
public static void add(String s) {
}
//auto generated methods from ATControl - what goes here???
public static void remove() {
}
public Object getNext() {
// TODO Auto-generated method stub
return null;
}
public void setNext(Object next2) {
// TODO Auto-generated method stub
}
}
Below, I have what I believe to be the class in which the Nodes are created and stored. This is where I am very confused and think I have it wrong. I am not sure how to call upon a node to actually add and store data to it. I will also need to be able to get the node (via the flight name) and remove the node (via the flight name)
package airTraffic;
import java.util.*;
import airTraffic.Aircraft;
public class ATControl {
static Main m = new Main ();
Aircraft aircraft = new Aircraft ();
//declare node names for list
public static Aircraft head = new Aircraft ();
public static Aircraft tail = new Aircraft ();
// stores data
private static final int INITIAL_ALLOCATION = 20;
private static int size = INITIAL_ALLOCATION;
// tells list to add nodes
public static void Nodes (String s, int n) {
n = size;
head.next = tail;
tail.next = tail;
Aircraft temp = head;
for (int i= 0; i < size; ++i) {
temp.next = new Aircraft ();
temp = temp.next;
}
temp.next = tail;
}
public static void addNodes (int n) {
n = size;
Aircraft temp = new Aircraft ();
Aircraft current = head;
for (int i = 1; i < n && current.getNext() != null; i++) {
current = (Aircraft) current.getNext();
temp.setNext(current.getNext());
current.setNext (temp);
size++;
}
}
//add plane and details
public static void addToList (Scanner in) {
// should add new aircraft to new node on linked list
System.out.printf("Enter flight number: ");
String add = in.next();
Aircraft.setName (add);
ATControl.addNodes (Integer.parseInt(add));
//type of plane
System.out.printf("Enter type of plane: ");
String plane = in.next();
Aircraft.setType (plane);
//plane speed
System.out.printf("Enter current speed: ");
int speed = in.nextInt();
Aircraft.setSpeed (speed);
ATControl.addNodes (Integer.parseInt(add));
//add Altitude
System.out.printf("Enter current altitude: ");
int alt = in.nextInt();
Aircraft.setAlt(alt);
ATControl.addNodes (Integer.parseInt(add)); // I am fairly certain this is wrong
}
//show flight
public static void showFlight (Scanner in) {
System.out.printf("Enter flight number for details: ");
String lookUp = in.next();
Aircraft.getName(lookUp);
}
// display all flights
public static void displayAll (Scanner in) {
System.out.printf("All flights: " );
}
//remove flight
public static void removeFlight (Scanner in) {
System.out.printf("Enter flight number to be removed: ");
}
}
You are getting close. First of all a linked list is a list of objects, commonly called nodes, each of which has one or more links to other objects. In your case the nodes are Aircraft.
This should help you a bit: Wikipedia:Linked List
Your main problem so far is that you do not have links in your Aircraft class. Since this is a linked list you need to include the reference to the next element in the list. Within the Aircraft class you should have a property called next of type Aircraft that links you to the next Aircraft in your list. This allows you to call myAircraft.next, as you are in your code so far, which will allow you to travel down the list in order. I'll leave you to figure out the rest yourself, this is homework, but feel free to comment if you need any more explanation.
I think you are pretty close - but it's hard to tell exactly what's going on in your ATControl class. Typically the add method on a linked list takes a node (in your case an Aircraft), not a number.
The key to a linked list is that each node has a pointer to the next one in the list. In your Aircraft class, you have: Aircraft next, which will serve as that pointer.
I would suggest implementing the following methods in ATControl:
public static Aircraft getUserInput(Scanner in)
{
Aircraft aircraft = new Aircraft();
// get your values from the user and set them in your new aircraft
return aircraft;
}
public static void add(Aircraft aircraft)
{
// starting at head, walk through the list (repeatedly call next on
// the current Aircraft) until you reach the desired position
Aircraft temp = head;
while (temp != null) // ...
}
public static void remove(String flightNum)
{
// again, the same way you did in add, walk through the list until you find it
if (current.getName().equals(flightNum))
// we found it, so remove it
}
Unless you have a very firm grasp on OOP and reference types, attempting to write a LinkedList implementation is a practice in masochism.
The following is going to be long and possibly painful/humiliating. That's OK, it'll be a good learning experience. I'm putting a lot of effort into this to provide a complete implementation with thorough commenting. I suggest you read the details closely until you fully understand their purpose.
First, fix your Aircraft class. Since you'll need to create multiple instances, static members won't work. If you don't understand why, take some time to brush up on your OOP fundamentals.
List nodes should be designed to store minimal data. In your case it looks like you're most of the way there. There is flight data specific to each node and a reference to the next item in the list. That's all you need.
Without all the extra cruft here's what it looks like:
public class Aircraft {
public String name;
public String type;
public int speed;
public int alt;
Aircraft next;
public Aircraft (String n, String t, int s, int a) {
name = n;
type = t;
speed = s;
alt = a;
next = null;
}
}
Looking good. It's safe to assume that this has all of the necessary functionality built-in.
Feel free to add the following back in if you want:
setName()
getName()
setType()
getType()
setSpeed()
getSpeed()
setAlt()
getAlt()
Note: These will only work if they're not set to static. Unless you plan to pass the Aircraft instance being changed in as one of the arguments every time you call it. Trust me, using instance methods is a lot easier.
Changes:
I removed the Aircraft() constructor. At a minimum, you'll need to initialize an Aircraft node with at least a flight number (or some other unique identifier) or you won't be able to find the Aircraft in the list later.
removeName() is useless. Since the individual nodes are only aware of the next item in the list they are incapable of removing themselves. If you used a doubly-linked-list where each node stores references to both the previous and next nodes then it would be possible but there really is no need. Same goes for the add() and remove()* methods. Additions/removals are handled in the **ATControl class.
There's not much need for getNext() or setNext() either. Since ATControl is used to maintain state of the list (ex size, capacity, etc) you won't want to make nextCraft publicly accessible via getters/setters.
Now for ATControl:
public class ATControl {
private Aircraft head; // stores the start of the chain
private Aircraft tail; // stores the end of the chain
private int size; // stores the length of the chain
public ATControl() {
// ♫ "Started from the bottom now we're herre' ♫
// Seriously, the list should start with nothing
head = null;
tail = null;
size = 0;
}
public void addFlight(String flight, String plane, int speed, int alt) {
// TODO: Implement this
}
public void removeFlight(String name) {
// TODO: Implement this
}
public void displayFlight(String name) {
// TODO: Use a foreach loop to find and display a flight
}
public void displayAll() {
// TODO: Use a foreach loop to display the flights here
}
}
Changes:
I removed the main* member because I don't have the slightest idea how it works here. Either way, to use this you'll need to create a new **ATControl instance.
I removed the inline variable declarations because that's instance members have to be set in the constructor.
The head and tail are initialized to null because no Aircraft have been added yet.
I removed the aircraft member because it will never be used.
Unless you only expect to create one ATControl instance, you shouldn't set head and tail too static either. If either of them are changed by anything but ATControl, it'll screw up the internal state of the list so they should be set to private.
I removed the size constraint because it's not necessary to make this work. You can add it back in later if you want.
I axed Nodes() and addNodes() for two reasons. First, it both violate the SRP (Single Responsibility Principle) because they are responsible for creating a node and a collection of nodes. Second -- I'm assuming it's a bug but -- you were passing in the flight number as the number of nodes you wanted to create. Ex, if the flight number were 1457 you'd be adding 1457 empty nodes to the list.
I renamed addToList() to addFlight() to keep things consistent. I also renamed showFlight() to displayFlight() for consistency. For the sake of simplicity and to make this class useful to more than just command-line inputs I also removed the user input parts.
I know, I know! I'm a merciless butcher but now the code is in a good position to start building in the necessary functionality.
First things first. If you don't know to make a class iterable (ie work as a foreach loop) you're about to find out. I'll need to add some more stuff to ATControl but it'll be fun.
public class ATControl implements Iterable {
private Aircraft head;
private Aircraft tail;
private int size;
public ATControl() {
head = null;
tail = null;
size = 0;
}
public void addFlight(String flight, String plane, int speed, int alt) {
// if the list is not currently empty
if (!isEmpty()) {
// store a reference to the last Aircraft in the list
Aircraft prev = tail;
// create a new aircraft and add it to the end of the list
tail = new Aircraft(flight, plane, speed, alt);
// link the old tail to the new tail
prev.next = tail;
}
// an empty list needs to be handled a little differently
else {
// notice, with no tail there's no tail to update
tail = new Aircraft(flight, plane, speed, alt);
// also, since there's only one item the head and tail are the same
head = tail;
}
size++;
}
// The hard part. Lots of nasty edge cases.
// Creating one of these from scratch will make your head hurt.
// Note: Setting variables to null marks them for the garbage collector.
// SideNote: With a doubly-linked list you can do removals within a foreach loop
public void removeFlight(String flight) {
Node prev = head;
Node curr = head;
// crawl the list looking for a match
while (curr.next != null || curr == tail) {
if (curr.flight.equals(flight)) {
// if there is only one item left, null everything
if (size == 1) { head = null; tail = null; }
// reset the head to start at the second Aircraft
else if (curr.equals(head)) { head = head.next; }
// reset the tail to end at the 2nd-to-last Aircraft
else if (curr.equals(tail)) { tail = prev; tail.next = null; }
// if it's in the middle, re-connect the broken links on either end
else { prev.next = curr.next; }
size--;
break;
}
prev = curr;
curr = prev.next;
}
}
public boolean isEmpty() {
return size == 0; // only returns true if size is 0
// The fun part. The following are necessary to make the list iterable
// Like magic, this class will now be callable as a foreach loop
public Iterator<Aircraft> iterator() { return new ATCIterator(); }
// This iterator code can be reused on any linked-list implementation
// Keep this handy in case you need to implement Iterable in the future
private class ATCIterator implements Iterator<Aircraft> {
private Aircraft current = head;
public Aircraft next() {
if (!hasNext()) { throw new NoSuchElementException(); }
Aircraft aircraft = current;
current = current.next;
return aircraft;
}
public boolean hasNext() { return current != null; }
// inline removals require a doubly linked list. To reconnect the break
// in the chain the node has to be aware of both the previous and next nodes.
public void remove() { throw new UnsupportedOperationException(); }
}
// lets put that foreach loop functionality to good use now.
// Bonus: use this to retrieve the matching Aircraft instance
// Once you have a reference to the Aircraft instance you can do things like
// get/set it's internal values.
public aircraft getFlight(String flight) {
for (Aircraft aircraft : this)
if (this.flight == flight) {
return this;
}
// displays the flight number of the first match
public void displayFlight(String flight) {
for (Aircraft aircraft : this)
if (this.flight == flight) {
System.out.printf("Flight: " + flight);
// Note: you can access the Aircraft details here via the 'this' keyword
return;
}
// crawls the whole list and displays the flight number of every aircraft
public void displayAll() {
for (Aircraft aircraft : this)
System.out.printf("Flight: " + flight);
// Note: you can access the flight details here via the 'this' keyword
}
}
So, you have a wall of code with lots of comments to chomp on. Time for some theory.
What makes a LinkedList a LinkedList anyway?
It's literally just a bunch of object instances that are randomly placed on the heap and linked together via references (or pointers for the C crowd).
Imagine a LinkedList as a Samba Line.
Source: The Traveling Eye Blog
Note: A doubly-linked list is the same except the line can change directions.
Every person is holding on to the person in front of them but they can't see who is behind them. Adding to a list is like adding a person to the front of the line. Technically, the LinkedList I wrote works in the opposite direction with additions being added to the front and removals being cut from the tail but the concept is the same.
Fetching/removing an item from the list is like adding a limbo pole. The first hit gets taken out of the chain and the break is mended by reconnecting the ends of the break.
The benefit to using a LinkedList is that it can be as big or as small as you want. You're free to add/remove nodes however you want.
The downside is, unlike an array there's no way to fetch an item from within the list without first walking the chain of links. Also, the overhead of all those class instances and references starts to get expensive when the list grows very large.
In performance terms it takes O(1) (ie constant time) to add items. O(N) (ie linear time) to fetch/remove items from the list. And, depending on whether the list is single/double and/or jump linked there is a notable memory overhead involved.
There are other data structures like ArrayLists, HashMaps, etc that have better performance or memory characteristics for use cases like yours but they're even more complicated to write/manage.
The easiest way to get all the magical goodness of a high-level data structures without the work is to wrap and extend an existing implementation. For example, you could create a class that uses an ArrayList internally for data storage. You can even make it iterable using the methods I demonstrated above. Except, instead of being written for any generic type it can be customized to work use your Aircraft data type.
Note: If you want to learn how to write data structures I suggest you take an Algorithms I class online (or otherwise).
Here is a link to what the re ponders are referring to when they say a list. Here is a link http://www.algolist.net/Data_structures/Singly-linked_list/Traversal that explains list. However, I'm not sure this is representative in java. The code you wrote looks like you are not sorting but adding all airplanes at the end of the list. Therefore the list is not sorted. When you make temp.next = tail is making the last airplane in the list point to itself rather than null. But you aren't checking for null you are counting the number of planes in the list. I posted a java example, where you have a node class with a node next, you should add node tail as well since your code is using it.
public class Node {
public int item;
public Node next;
public Node tail;
Node() {
item = 0;
next = null;
tail = null ;
}
Add Node(node tail, node new) {
tail.next = new;
tail.tail = new
new.tail =null
}
};
I hope I didn't make it worse. Good luck.
airplane class can extend the node class. Look at the childfactory class in java. It will give you an example of the type of methods you will use in the node class. Rethink your classes. Maybe remove Airplane will be a method in the node class like remove node. Anything that works on the node such as insert or add new, delete, and sort can be added to the node class. You can then reuse this class as you add more classes.
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Categories