Queue_LAB.java - /Queue.java is a queue implementation using generic parameterized type E interface Queue<E> public void enqueue(E element public E

Queue_LAB.java - /Queue.java is a queue implementation...

This preview shows page 1 - 3 out of 3 pages.

//Queue.java: is a queue implementation using generic parameterized type E interface Queue<E> { public void enqueue(E element); public E element(); public boolean isEmpty(); public E dequeue(); public int size(); } public class Queue_LAB { public static void main(String[] args) { LinkedQueue queue = new LinkedQueue(); queue.enqueue("GB"); queue.enqueue("DE"); queue.enqueue("FR"); queue.enqueue("ES"); queue.printQueue(); System.out.println("queue.element(): " + queue.element()); System.out.println("queue.dequeue(): " + queue.dequeue()); queue.printQueue(); System.out.println("queue.dequeue(): " + queue.dequeue()); queue.printQueue(); System.out.println("queue.enqueue(\"IE\"): "); queue.enqueue("IE"); queue.printQueue(); System.out.println("queue.dequeue(): " + queue.dequeue()); queue.printQueue(); } } //Exercises class LinkedQueue<E> implements Queue<E> { QNode front, rear; int size; public LinkedQueue() { this.front = this.rear = null; size = 0; } @Override public void enqueue(E element) { // Create a new LL node QNode temp = new QNode(element); // If queue is empty, then new node is front and rear both if (isEmpty()) { this.front = this.rear = temp;
Image of page 1

Subscribe to view the full document.

Image of page 2
Image of page 3

Unformatted text preview: size++; return; } // Add the new node at the end of queue and change rear this.rear.next = temp; this.rear = temp; size++; } @Override public E element() { if (!isEmpty()) { return (E) front.data; } else { throw new java.util.NoSuchElementException(); } } @Override public boolean isEmpty() { return front == null; } @Override public E dequeue() { // If queue is empty, return NULL. if (isEmpty()) { return null; } // Store previous front and move front one node ahead QNode temp = this.front; this.front = this.front.next; size--; // If front becomes NULL, then change rear also as NULL if (this.front == null) { this.rear = null; } return (E) temp.data; } @Override public int size() { return size; } public void printQueue() { if (!isEmpty()) { QNode cur = front; System.out.print(&quot;[&quot;); while (cur != null) { System.out.print(cur.data + &quot; &quot;); cur = cur.next; } System.out.println(&quot;]&quot;); } } } class QNode&lt;E&gt; { E data; QNode&lt;E&gt; next; // constructor to create a new linked list node public QNode(E data) { this.data = data; this.next = null; } }...
View Full Document

  • Spring '13
  • Staff

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes