Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to use deque as stack 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 :: arraylist items into string 
Java :: junit vintage engine maven 
Java :: java localdate to calendar 
Java :: java date to string 
Java :: filtering out unique values from a list in java 
Java :: java spring boot json deserialize date inaccurate 
Java :: How to generate all possible IP addresses based on a given set of digits, in Java? 
Java :: get preference value android 
Java :: java date format with timezone 
Java :: priority scheduling in java 
Java :: java remove character from string after 
Java :: java leap year 
Java :: jpa enum as string 
Java :: java Write a program to reverse an array or string 
Java :: how to instance a calendar in java 
Java :: java if or 
Java :: declare array of chars java 
Java :: Send image file to server useing Retrofit 
Java :: open website from android activity 
Java :: java append file 
Java :: java enum length 
Java :: android double click to exit 
Java :: java println 
Java :: dropdown menu with spinner dropdown icon color change in android 
Java :: Count Occurrences in Seven Integers Using Java Single Dimension Arrays 
Java :: java swing menu click event 
Java :: java 2d array length 
Java :: java interfce 
Java :: java reverse string 
Java :: how to loop through arraylist of objects in java 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =