Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Dilarang mengimpor library lain */
- import java.util.Scanner;
- public class Solution {
- public static void main(String[] args) {
- /* Masukkan kode di sini. Selamat mengerjakan! */
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- LinkedList<String> kereta = new LinkedList<>();
- for (int i = 0; i < n; i++) {
- String perintah = sc.next();
- if (perintah.equals("TAMBAH")) {
- String gerbong = sc.next();
- String posisi = sc.next();
- if (posisi.equals("AWAL")) {
- kereta.addFirst(gerbong);
- }
- else {
- kereta.addLast(gerbong);
- }
- }
- else {
- System.out.println(kereta);
- }
- }
- }
- }
- /* Jangan ubah kode di bawah ini */
- class LinkedList<T> {
- private Node<T> head, tail;
- private int size = 0;
- public LinkedList() {
- this.head = this.tail = null;
- }
- public boolean isEmpty() {
- return size == 0;
- }
- public int size() {
- return this.size;
- }
- public void addFirst(T data) {
- Node<T> node = new Node<>(data);
- if (isEmpty()) {
- this.head = this.tail = node;
- }
- else {
- node.next = this.head;
- this.head = node;
- }
- this.size++;
- }
- public void addLast(T data) {
- Node<T> node = new Node<>(data);
- if (isEmpty()) {
- this.head = this.tail = node;
- }
- else {
- this.tail.next = node;
- this.tail = node;
- }
- this.size++;
- }
- public T getFirst() {
- return this.head.data;
- }
- public T getLast() {
- return this.tail.data;
- }
- @Override
- public String toString() {
- StringBuilder s = new StringBuilder();
- Node<T> temp = this.head;
- while (temp != null) {
- s.append(temp.data);
- if (temp.next != null) s.append("-");
- temp = temp.next;
- }
- return s.toString();
- }
- }
- class Node<T> {
- public T data;
- public Node<T> next;
- public Node() {
- this(null);
- }
- public Node(T data) {
- this.data = data;
- this.next = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement