VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-implement-queue-in-java-using-array-and-generics/

⇱ How to Implement Queue in Java using Array and Generics? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Implement Queue in Java using Array and Generics?

Last Updated : 14 Feb, 2023

The queue is a linear data structure that follows the FIFO rule (first in first out). We can implement Queue for not only Integers but also Strings, Float, or Characters. There are 5 primary operations in Queue:

  1. enqueue() adds element x to the front of the queue
  2. dequeue() removes the last element of the queue
  3. front() returns the front element
  4. rear() returns the rear element
  5. empty() returns whether the queue is empty or not

Note: Time complexity is of order 1 for all operations

Implementation:

Example


Output
q1 after enqueue of 3 elements:
5->10->20
q1 after dequeue :
10->20

q2 after enqueue of 3 elements:
hello->world->GFG
q2 front = GFG, q2 rear = hello

Created new Float type queue q3...
Checking if queue is empty or not :
true

Time Complexity: O(n) for traversing and rest O(1) for rest other operations
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: