Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package queueopgaver;
- import java.util.NoSuchElementException;
- /**
- * An implementation of a queue as a list.
- */
- public class QueueList implements QueueI {
- private Node head;
- private Node tail;
- /**
- * Constructs an empty queue.
- */
- public QueueList() {
- head = null;
- }
- /**
- * Checks whether this queue is empty.
- *
- * @return true if this queue is empty
- */
- @Override
- public boolean isEmpty() {
- return head == null;
- }
- /**
- * Adds an element to the tail of this queue.
- *
- * @param newElement the element to add
- */
- @Override
- public void enqueue(Object newElement) {
- Node newNode = new Node();
- newNode.data = newElement;
- if (head == null) {
- head = newNode;
- tail = newNode;
- } else {
- tail.next = newNode;
- tail = newNode;
- }
- }
- /**
- * Removes an element from the head of this queue.
- *
- * @return the removed element
- */
- @Override
- public Object dequeue() { // TODO: SOMETHING IS WRONG WITH THIS METHOD!!!
- if (head == null) {
- throw new NoSuchElementException();
- }
- Object NewElement = head.data;
- head = head.next;
- return NewElement;
- }
- /**
- * Returns the head of this queue. The queue is unchanged.
- *
- * @return the head element
- */
- @Override
- public Object getFront() {
- if (head == null) {
- throw new NoSuchElementException();
- }
- return head.data;
- }
- /**
- * The number of elements on the queue.
- *
- * @return the number of elements in the queue
- */
- @Override
- public int size() {
- int count = 0;
- Node temp = head;
- while (temp != null) {
- count++;
- temp = temp.next;
- }
- return count;
- }
- class Node {
- public Object data;
- public Node next;
- }
- }
Add Comment
Please, Sign In to add comment