
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a Stack and Queue using ArrayDeque in Java
Create a stack using ArrayDeque.
Deque<String> s = new ArrayDeque<String>(); // stack s.push("Bat"); s.push("Mat"); s.push("Cat"); s.push("Rat"); s.push("Hat"); s.push("Fat");
Create a queue using ArrayDeque −
Deque<String> q = new ArrayDeque<String>(); // queue q.add("Bat"); q.add("Mat"); q.add("Cat"); q.add("Rat"); q.add("Hat"); q.add("Fat");
The following is an example.
Example
import java.util.ArrayDeque; import java.util.Deque; public class Demo { public static void main(String args[]) { Deque<String> s = new ArrayDeque<String>(); Deque<String> q = new ArrayDeque<String>(); // stack s.push("Bat"); s.push("Mat"); s.push("Cat"); s.push("Rat"); s.push("Hat"); s.push("Fat"); while (!s.isEmpty()) System.out.print(s.pop() + " "); System.out.print("\n"); // queue q.add("Bat"); q.add("Mat"); q.add("Cat"); q.add("Rat"); q.add("Hat"); q.add("Fat"); while (!q.isEmpty()) System.out.print(q.remove() + " "); } }
Output
Fat Hat Rat Cat Mat Bat Bat Mat Cat Rat Hat Fat
- Related Questions & Answers
- Stack and Queue in Python using queue Module
- Stack and Queue in C#
- How can we Implement a Stack using Queue in Java?
- How can we Implement a Queue using Stack in Java?
- Difference Between Stack and Queue
- Create a queue using LinkedList class in Java
- Check if a queue can be sorted into another queue using a stack in Python
- Difference between Stack and Queue Data Structures
- Differences between stack and queue data structure
- Python Program to Implement Stack using One Queue
- Difference between ArrayBlockingQueue and ArrayDeque
- How to create a Queue from LinkedList in Java?
- Explanation about SAP ABAP Stack and JAVA Stack and role of Java Stack during ECC upgrade
- Queue in Java
- How to create a Stack in C#?
Advertisements