Poll이나 Push 등을 쓰지않고
addFirst, addLast, removeFirst, removeLast 를 이용하여 스택과 큐를 구현할 수 있다.
import java.util.LinkedList;
public class linkedList {
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList<Integer> list = new LinkedList<Integer>();
for (int i = 1; i <= 9; i++) {
list.add(i);
}
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
list.add(9999);
//add back
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 9999]
list.addFirst(9999);
//add back
// [9999, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list.push(9999);
//add front
// [9999, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list.poll();
//remove front
// [2, 3, 4, 5, 6, 7, 8, 9]
System.out.println(list.get(1));
//2 (just print, check) Not Changed
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
System.out.println(list.peekLast());
//10 (just print, check) Not Changed
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
System.out.println(list.peekFirst()); // ==list.peek();
//1 (just print, check) Not Changed
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
list.removeFirst(); // == list.remove(); , list.poll();
// [2, 3, 4, 5, 6, 7, 8, 9]
list.removeLast();
// [1, 2, 3, 4, 5, 6, 7, 8]
System.out.println(list.toString());
}
}