Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to use deque as stack or queue in java?

/*
	This implementation shows how to use a Deque
	object as a stack and then how to use it
	as a queue.

	Time complexity of methods used: O(1)
*/
import java.util.Deque;
import java.util.ArrayDeque;
public class DequeDemo {
	public static void main(String[] args) {
		Deque<Integer> deque = new ArrayDeque<>();
		// Deque as stack
		deque.push(4); // Insert a new top element
		deque.push(3);
		// Pop top element from stack
		System.out.println(deque.getFirst()); // 3
		deque.pop();
		System.out.println(deque.pop()); // 4

		// Deque as Queue
		deque.add(5); // Enqueue an element
		deque.add(2);
		System.out.println(deque.getFirst()); // 5
		// Dequeue the front element of queue
   		deque.removeFirst();
		System.out.println(deque.removeFirst()); // 2
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: remove last <br/ from string java 
Java :: java exception message 
Java :: string to char in java 
Java :: java remote debug 
Java :: how to print an array in java 
Java :: java empty array 
Java :: java length of matrix 
Java :: running sum of 1d array leetcode 
Java :: filter arraylist java 
Java :: java csv line split 
Java :: cls java 
Java :: timestamp to date java 
Java :: on selected item changed listview javafx 
Java :: radio button java swing 
Java :: android remove focus from an element 
Java :: spinner get selected index 
Java :: java for each loop 
Java :: spring boot base url 
Java :: empty set java 
Java :: java extract zip 
Java :: how to right align in java 
Java :: sort list java 8 
Java :: java mockito any string import 
Java :: exception in thread "main" java.lang.indexoutofboundsexception: index 1 out of bounds for length 1 
Java :: how to check if notification is showing in android 
Java :: spring db properties 
Java :: max and min array number in java 
Java :: java substring after last char 
Java :: palindrome find in java 
Java :: loop through java object 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =