Created
February 16, 2018 00:04
-
-
Save Jun711/83cb0d6a186ea0e4bd74fa5f1d683153 to your computer and use it in GitHub Desktop.
TestDome - Train Composition LinkedList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.LinkedList; | |
public class TrainComposition { | |
private LinkedList<Integer> train; | |
public TrainComposition() { | |
this.train = new LinkedList<>(); | |
} | |
public void attachWagonFromLeft(int wagonId) { | |
this.train.addFirst(wagonId); | |
} | |
public void attachWagonFromRight(int wagonId) { | |
this.train.addLast(wagonId); | |
} | |
public int detachWagonFromLeft() { | |
if (!train.isEmpty()) { | |
return this.train.removeFirst(); | |
} | |
return -1; | |
} | |
public int detachWagonFromRight() { | |
if (!train.isEmpty()) { | |
return this.train.removeLast(); | |
} | |
return -1; | |
} | |
public static void main(String[] args) { | |
TrainComposition tree = new TrainComposition(); | |
tree.attachWagonFromLeft(7); | |
tree.attachWagonFromLeft(13); | |
System.out.println(tree.detachWagonFromRight()); // 7 | |
System.out.println(tree.detachWagonFromLeft()); // 13 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment