Skip to content

Instantly share code, notes, and snippets.

@Jun711
Created February 16, 2018 00:04
Show Gist options
  • Save Jun711/83cb0d6a186ea0e4bd74fa5f1d683153 to your computer and use it in GitHub Desktop.
Save Jun711/83cb0d6a186ea0e4bd74fa5f1d683153 to your computer and use it in GitHub Desktop.
TestDome - Train Composition LinkedList
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