For a stack: using Deque methods, i.e. push() at a head and pop() query and remove from a head.
For a queue: using Deque methods, i.e. offer() or add() at a tail and poll() query and remove from the head.
* peek() is a query at the head, but not removing it. it is different from pop() or poll();
package String; import java.util.Deque;import java.util.LinkedList;
import java.util.stream.IntStream; public class ReverseStringQMethod { public static void main(String[] args) { String str = "I love this game";
System.out.println(str);
Dequedeque = new LinkedList(); //Used as a stack str.chars().map(c -> (char) c).forEach(c -> deque.push((char) c)); char[] reversed = new char[deque.size()]; IntStream.range(0, reversed.length).forEach(i -> { reversed[i] = deque.pop(); System.out.print(reversed[i]); }); } }
No comments:
Post a Comment