/* Demonstration of Queue author: course: 15-111 Java Programming section: date: */ import java.io.*; import java.util.*; public class Queue { private Node front; private Node back; private int numWaiting; // deafult constructor creates an empty queue public Queue(){ front=back=null; numWaiting = 0; } // add the element n to the queue public void enqueue(int n) { Node newNode = new Node(n,null); numWaiting++; if (numWaiting == 0) { front = back = newNode;} else { back.next = newNode; back = newNode; } } // remove an element from the queue public void dequeue() { if (!isEmpty()) { front = front.next;} } // print the queue from front to back public void print(){ Node ptr = front; while (ptr != null) { System.out.println(ptr.value + " "); ptr = ptr.next; } } // returns true if the queue is empty public boolean isEmpty(){ if (numWaiting == 0) return true; else return false; } // count the number of nodes in the queue public int size(){ return numWaiting; } // count all the odd value nodes in the queue public int oddCount(){ int count = 0; Node ptr = front; while (ptr != null) { if (ptr.value%2 == 1) count++; ptr = ptr.next; } } private class Node{ public int value; public Node next; public Node(int n, Node link){ value = n; next = link; } } }