Skip to content

trigologiaa/queue-in-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Queue - Generic Queue Implementation in Java

A fully featured generic Queue implementation in Java using a singly linked list.

This queue follows the FIFO (First In, First Out) principle and supports common operations as well as useful utility methods for flexibility and convenience.


Table of Contents


Features

  • Generic: works with any type (Queue<T>)
  • Core queue operations:
    • enqueue(T item) — add an element to the rear of the queue
    • dequeue() — remove and return the front element
    • peek() — return (without removing) the front element
    • peekLast() — return (without removing) the rear element
    • isEmpty() — check if the queue is empty
    • size() — number of elements in the queue
  • Iterable: supports iteration from front to rear with enhanced for-loops
  • Utility methods:
    • clear() — empties the queue
    • contains(T item) — checks if the queue contains a given element (supports null)
    • remove(T item) — removes the first occurrence of a specified element
    • offer(T item) — adds an element (alias for enqueue)
    • poll() — removes and returns the front element or returns null if empty
    • toArray() — returns an array of elements in FIFO order
    • clone() — creates a shallow copy of the queue
    • reverse() — reverses the order of elements in the queue
  • Properly overrides equals(Object) and hashCode()
  • Throws NoSuchElementException when dequeuing or peeking from an empty queue
  • Well documented with Javadoc comments

Usage

Queue<Integer> queue = new Queue<>();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);

System.out.println(queue.peek());  // Output: 10

while (!queue.isEmpty()) {
    System.out.println(queue.dequeue());  // Dequeues and prints: 10, 20, 30
}

Running Tests

The implementation comes with comprehensive unit tests using JUnit 5.

To run the tests:

  1. Ensure you have JUnit 5 set up in your build system (Maven, Gradle, or your IDE).
  2. Run QueueTest.java as a JUnit test suite.
  3. Tests cover normal operations, edge cases (empty queue, null elements), and utility methods.

Design Notes

  • Internally implemented with a singly linked list (Node<T>) for dynamic size and efficient enqueue/dequeue.
  • Iterator returns elements from front to rear.
  • Supports null elements safely in methods like contains(), equals(), and hashCode().
  • clone() creates a shallow copy preserving element order.
  • reverse() modifies the queue in-place reversing the element order.

Example

Queue<String> names = new Queue<>();
names.enqueue("Alice");
names.enqueue("Bob");
names.enqueue("Charlie");

names.reverse();

for (String name : names) {
    System.out.println(name);
}
// Prints:
// Charlie
// Bob
// Alice

Author

trigologiaa


License

This project is released under the MIT License. Feel free to use, modify, and distribute.


Contact

For questions or contributions, please open an issue or contact the author.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages