Advertisement
Yesver08

LinkedListI.java

Sep 28th, 2022
979
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. /* Dilarang mengimpor library lain */
  2. import java.util.Scanner;
  3.  
  4. public class Solution {
  5.  
  6.     public static void main(String[] args) {
  7.         /* Masukkan kode di sini. Selamat mengerjakan! */
  8.         Scanner sc = new Scanner(System.in);
  9.        
  10.         int n = sc.nextInt();
  11.         LinkedList<String> kereta = new LinkedList<>();
  12.        
  13.         for (int i = 0; i < n; i++) {
  14.            
  15.             String perintah = sc.next();
  16.             if (perintah.equals("TAMBAH")) {
  17.                 String gerbong = sc.next();
  18.                 String posisi = sc.next();
  19.                
  20.                 if (posisi.equals("AWAL")) {
  21.                     kereta.addFirst(gerbong);
  22.                 }
  23.                 else {
  24.                     kereta.addLast(gerbong);
  25.                 }
  26.             }
  27.             else {
  28.                 System.out.println(kereta);
  29.             }
  30.            
  31.         }
  32.     }
  33. }
  34.  
  35.  
  36.  
  37. /* Jangan ubah kode di bawah ini */
  38.  
  39. class LinkedList<T> {
  40.     private Node<T> head, tail;
  41.     private int size = 0;
  42.  
  43.     public LinkedList() {
  44.         this.head = this.tail = null;
  45.     }
  46.  
  47.     public boolean isEmpty() {
  48.         return size == 0;
  49.     }
  50.  
  51.     public int size() {
  52.         return this.size;
  53.     }
  54.  
  55.     public void addFirst(T data) {
  56.         Node<T> node = new Node<>(data);
  57.         if (isEmpty()) {
  58.             this.head = this.tail = node;
  59.         }
  60.         else {
  61.             node.next = this.head;
  62.             this.head = node;
  63.         }
  64.         this.size++;
  65.     }
  66.  
  67.     public void addLast(T data) {
  68.         Node<T> node = new Node<>(data);
  69.         if (isEmpty()) {
  70.             this.head = this.tail = node;
  71.         }
  72.         else {
  73.             this.tail.next = node;
  74.             this.tail = node;
  75.         }
  76.         this.size++;
  77.     }
  78.    
  79.     public T getFirst() {
  80.         return this.head.data;
  81.     }
  82.  
  83.     public T getLast() {
  84.         return this.tail.data;
  85.     }
  86.  
  87.     @Override
  88.     public String toString() {
  89.         StringBuilder s = new StringBuilder();
  90.         Node<T> temp = this.head;
  91.         while (temp != null) {
  92.             s.append(temp.data);
  93.             if (temp.next != null) s.append("-");
  94.             temp = temp.next;
  95.         }
  96.         return s.toString();
  97.     }
  98. }
  99.  
  100. class Node<T> {
  101.     public T data;
  102.     public Node<T> next;
  103.  
  104.     public Node() {
  105.         this(null);
  106.     }
  107.  
  108.     public Node(T data) {
  109.         this.data = data;
  110.         this.next = null;
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement