Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Solution {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);        
  8.         int t = input.nextInt();        
  9.         LinkedList<Integer> list = new LinkedList<>();        
  10.         for (int i = 0; i < t; i++) list.add(input.nextInt());        
  11.         t = input.nextInt();
  12.        
  13.         while (t--> 0) {            
  14.             input.nextLine();
  15.             String s = input.nextLine();
  16.            
  17.             if (s.equals("Insert")) {                
  18.                 int n = input.nextInt();int q = input.nextInt();
  19.                 try {
  20.                     if (n >= list.size()) list.add(q);
  21.                     else list.set(n,q);
  22.                 } catch (Exception e) {
  23.                     list.addLast(q);
  24.                 }
  25.                 continue;
  26.                
  27.             } else if (s.equals("Delete")) {                
  28.                 int n = input.nextInt();
  29.                 try {
  30.                     list.remove(n);
  31.                 } catch (Exception e) {
  32.                     list.pollLast();
  33.                 }
  34.                 continue;
  35.             }            
  36.         }
  37.         for (int i = 0; i < list.size(); i++) System.out.print(list.get(i) +" ");
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement