Skip to content

Instantly share code, notes, and snippets.

@rishi93
Created December 16, 2017 06:52
Show Gist options
  • Select an option

  • Save rishi93/e60e59388f72a7a87636f6f0fa0fb556 to your computer and use it in GitHub Desktop.

Select an option

Save rishi93/e60e59388f72a7a87636f6f0fa0fb556 to your computer and use it in GitHub Desktop.
Deque (Java)
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