-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDequeExample.java
More file actions
64 lines (50 loc) · 2.14 KB
/
Copy pathDequeExample.java
File metadata and controls
64 lines (50 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package data.structure;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
/**
* @Author : yion
* @Date : 2017. 6. 16.
* @Description : 큐의 양쪽에 삽입과 삭제가 모두 발생할 수 있는 큐로써 큐와 스택의 성질을 가지고 있는 자료 구조
*/
public class DequeExample {
public static void main(String[] args) {
Deque deque = new LinkedList<>();
// We can add elements to the queue in various ways
deque.add("Element 1 (Tail)"); // add to tail // 4
deque.addFirst("Element 2 (Head)"); // 3
deque.addLast("Element 3 (Tail)"); // 5
deque.push("Element 4 (Head)"); //add to head // 2
deque.offer("Element 5 (Tail)"); // 6
deque.offerFirst("Element 6 (Head)"); // 1
deque.offerLast("Element 7 (Tail)"); // 7
deque.forEach((str) -> System.out.println(str));
// Iterate through the queue elements.
System.out.println("Standard Iterator");
Iterator iterator = deque.iterator();
while (iterator.hasNext())
System.out.println("\t" + iterator.next());
Iterator reverse = deque.descendingIterator();
System.out.println("Reverse Iterator");
while (reverse.hasNext())
System.out.println("\t" + reverse.next());
// Peek returns the head, without deleting
// it from the deque
System.out.println("Peek " + deque.peek());
System.out.println("After peek: " + deque);
// Pop returns the head, and removes it from
// the deque
System.out.println("Pop " + deque.pop());
System.out.println("After pop: " + deque);
// We can check if a specific element exists
// in the deque
System.out.println("Contains element 3: " +
deque.contains("Element 3 (Tail)"));
// We can remove the first / last element.
deque.removeFirst();
deque.removeLast();
System.out.println("Deque after removing " +
"first and last: " + deque);
}
}