Created
December 16, 2017 06:52
-
-
Save rishi93/e60e59388f72a7a87636f6f0fa0fb556 to your computer and use it in GitHub Desktop.
Deque (Java)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Deque; | |
| import java.util.ArrayDeque; | |
| public class Test{ | |
| public static void main(String[] args){ | |
| Deque<Integer> deque = new ArrayDeque<Integer>(); | |
| /* Adds element normally to the end of the list */ | |
| deque.add(1); | |
| deque.add(2); | |
| deque.add(3); | |
| /* Adds element to the head of the list */ | |
| deque.addFirst(4); | |
| deque.addFirst(5); | |
| for(Integer item : deque){ | |
| System.out.print(item + " "); | |
| } | |
| System.out.println(); | |
| /* Removes and returns the head of the list */ | |
| System.out.println(deque.pollFirst()); | |
| /* Removes and returns the tail of the list */ | |
| System.out.println(deque.pollLast()); | |
| /* Returns the head of the list */ | |
| System.out.println(deque.getFirst()); | |
| /* Returns the tail of the list */ | |
| System.out.println(deque.getLast()); | |
| for(Integer item : deque){ | |
| System.out.print(item + " "); | |
| } | |
| System.out.println(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment