- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 queue using LinkedList class in Java
To create a queue using LinkedList class, try the following −
Queue<String> q = new LinkedList<String>(); q.offer("abc"); q.offer("def"); q.offer("ghi"); q.offer("jkl"); q.offer("mno"); q.offer("pqr"); q.offer("stu"); q.offer("vwx");
After that to print the elements, you need to use check a condition for Queue as shown below −
Object ob; while ((ob = q.poll()) != null) { System.out.println(ob); }
The following is an example −
Example
import java.util.LinkedList; import java.util.Queue; public class Demo { public static void main(String[] args) { Queue<String> q = new LinkedList<String>(); q.offer("abc"); q.offer("def"); q.offer("ghi"); q.offer("jkl"); q.offer("mno"); q.offer("pqr"); q.offer("stu"); q.offer("vwx"); System.out.println("Queue head = " + q.element()); System.out.println("Size = " + q.size()); System.out.println("
Queue elements..."); Object ob; while ((ob = q.poll()) != null) { System.out.println(ob); } } }
Output
Queue head = abc Size = 8 Queue elements... abc def ghi jkl mno pqr stu vwx
- Related Articles
- How to create a Queue from LinkedList in Java?
- Create a Stack and Queue using ArrayDeque in Java
- Create a Date object using the Calendar class in Java
- Create class Objects in Java using new operator
- Iterate through a LinkedList using an Iterator in Java
- How to create a queue in TypeScript using an array?
- How to create a thread by using anonymous class in Java?
- Create an object array from elements of LinkedList in Java
- Iterate through elements of a LinkedList using a ListIterator in Java
- Should we declare it as Queue or Priority Queue while using Priority Queue in Java?
- The Queue Class in Javascript
- LinkedList in Java
- Iterate through a LinkedList in reverse direction using a ListIterator in Java
- Create Objects of a Class in Java
- C# Program to create a LinkedList

Advertisements