0% found this document useful (0 votes)
30 views

Queue Code

The document tests different implementations of a queue data structure, including an array-based queue, linked list queue, and stack-based queue. It provides code for a Queue interface and ArrayQueue implementation, along with exercises to create a LinkedQueue implementation and additional queue methods like reverseQueue and displayQueue. The main method tests the different queue implementations by enqueueing and dequeueing elements and printing the contents.

Uploaded by

sahar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Queue Code

The document tests different implementations of a queue data structure, including an array-based queue, linked list queue, and stack-based queue. It provides code for a Queue interface and ArrayQueue implementation, along with exercises to create a LinkedQueue implementation and additional queue methods like reverseQueue and displayQueue. The main method tests the different queue implementations by enqueueing and dequeueing elements and printing the contents.

Uploaded by

sahar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

package testqueue;

import java.util.Stack;

public class TestQueue {

public static void main(String[]


(String[] args) {
// is the main class where we test a String Queue
System.out.println("Array
System. .println("Array Queue: ");
ArrayQueue<String> queue = new ArrayQueue<String>();
queue.enqueue("GB");
queue.enqueue("DE");
queue.enqueue("FR");
queue.enqueue("ES");
queue.printQueue();
System.out.println("queue.element():
System. .println("queue.element(): " + queue.element());
System.out.println("queue.dequeue():
System. .println("queue.dequeue(): " + queue.dequeue());
queue.printQueue();
System.out.println("queue.dequeue():
System. .println("queue.dequeue(): " + queue.dequeue());
queue.printQueue();
System.out.println("queue.enqueue(
System. .println("queue.enqueue(\"IE\"): ");
.println("queue.enqueue(
queue.enqueue("IE");
queue.printQueue();
System.out.println("queue.dequeue():
System. .println("queue.dequeue(): " + queue.dequeue());
queue.printQueue();
System.out.println();
System. .println();

System.out.println("Linked
System. .println("Linked Queue: ");
LinkedQueue<String> qu = new LinkedQueue<String>();
qu.enqueue("GB");
qu.enqueue("DE");
qu.enqueue("FR");
qu.enqueue("ES");
qu.printQueue();
System.out.println("queue.element():
System. .println("queue.element(): " + qu.element());
System.out.println("queue.dequeue():
System. .println("queue.dequeue(): " + qu.dequeue());
qu.printQueue();
System.out.println("queue.dequeue():
System. .println("queue.dequeue(): " + qu.dequeue());
qu.printQueue();
System.out.println("queue.enqueue(
System. .println("queue.enqueue(\"IE\"): ");
.println("queue.enqueue(
qu.enqueue("IE");
qu.printQueue();
System.out.println("queue.dequeue():
System. .println("queue.dequeue(): " + qu.dequeue());
qu.printQueue();
/* 3. Write displayQueue(Queue queue) method, which displays the items of a given queue
without emptying the queue. */
System.out.println("displays
System. .println("displays the items of a given queue without emptying the queue: ");
qu.displayQueue(qu);
System.out.println();
System. .println();

// reverse Queueu
System.out.println("Reverse
System. .println("Reverse Queue: " + qu.reverseQueue(qu));

System.out.println("Stack
System. .println("Stack queue: ");
StackQueue q = new StackQueue();
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
System.out.println(q.deQueue());
System. .println(q.deQueue());
System.out.println(q.deQueue());
System. .println(q.deQueue());
System.out.println(q.deQueue());
System. .println(q.deQueue());
System.out.println();
System. .println();

/* 1. Queues provide FIFO (First-in-first-out) access where the first item in the set is accessed first.
Regardless of the data structure we use to implement a queue, queues have two main
operations: enqueue() and dequeue() . The following code implement a queue using an array.
Apply and test the code.
*/
//Queue.java: is a queue implementation using generic parameterized type E
interface Queue<E> {

public void enqueue(E


(E element);

public E element();

public boolean isEmpty();

public E dequeue();

public int size();


}
// ArrayQueue.java: An ArrayQueue Implementation

class ArrayQueue<E> implements Queue<E> {

private E[] elements;


private int front;
private int back;
private static final int INITIAL_CAPACITY = 4;
private int count;

public ArrayQueue() {
elements = (E[]) new Object[INITIAL_CAPACITY ];
}

public ArrayQueue(int capacity) {


elements = (E[]) new Object[capacity];
}

public void enqueue(E


(E element) {
if (size() == elements.length - 1) {
resize();
}
elements[back] = element;
// or back = (back +1 ) % elements.length;
if (back < elements.length - 1) {
++back;
} else {
back = 0; // wrap
}
count++;
}

public E element() {
if (size() == 0) {
throw new java.util.NoSuchElementException();
}
return elements[front];
}

public boolean isEmpty() {


return (size() == 0);
}

public E dequeue() {
if (size() == 0) {
throw new java.util.NoSuchElementException();
}
E element = elements[front];
elements[front] = null;
// or front = (front +1) % elements.length;
++front;
if (front == back) { // queue is empty
front = back = 0;
}
if (front == elements.length) { // wrap
front = 0;
}
count--;
return element;
}

public int size() {


// or return count;
if (front <= back) {
return back - front;
} else {
return back - front + elements.length;
}
}

private void resize() {


int size = size();
int len = elements.length;
assert size == len; // we can remove it
Object[] a = new Object[2 * len];
// we can replace the following two lines with
/* for (int i =0; i < size() or count(); i++){
a[i] = elements[front];
front = (front+1)% elements.length; } */
System.arraycopy(elements,
System. (elements, front, a, 0, len - front);
System.arraycopy(elements,
System. (elements, 0, a, len - front, back);
elements = (E[]) a;
front = 0;
back = size;
}

public void printQueue() {


System.out.print("[");
System. .print("[");
String str = "";
if (front <= back) {
for (int i = front; i < back; i++) {
str += elements[i] + ",";
}
} else {
for (int i = front; i < elements.length; i++) {
str += elements[i] + ",";
}
if (back != 0) {
for (int i = 0; i < back; i++) {
str += elements[i] + ",";
}
}
}
if (str.length() != 0) {
str = str.substring(0, str.length() - 1);
System.out.print(str
System. .print(str + "]\n");
"]
}
}
}

//////////////////////////////
/*Exercises

1. Using Linked list operations, write LinkedQueue class which implements the

previous Queue
interface using

a linked list. Note. Use the same QueueMain.java, just change the type of the
queue object. */
class LinkedQueue<E> implements Queue<E> {

/*2. Write displayQueue(Queue queue) method, which displays the items of a given queue
without emptying the queue. */
public void displayQueue(LinkedQueue
(LinkedQueue queue) {
LinkedQueue tempQueue = new LinkedQueue();
if (queue.isEmpty()) {
System.out.println("Queue
System. .println("Queue is empty!");
return;
}
while (!queue.isEmpty()) {
Object item = queue.dequeue();
System.out.print(item);
System. .print(item);
tempQueue.enqueue(item);
}
while (!tempQueue.isEmpty()) {
queue.enqueue(tempQueue.dequeue());
}
System.out.println();
System. .println();
}

/*
Challenge Question
1. Write Queue reverseQueue(Queue queue), a static method which returns a queue in which
the order of the elements is reversed. Write a suitable main() to test your code. */
public LinkedQueue reverseQueue(LinkedQueue
(LinkedQueue queue) {
Stack<Object> s = new Stack<Object>();
LinkedQueue tempQueue = new LinkedQueue();
if (queue.isEmpty()) {
System.out.println("Queue
System. .println("Queue is empty!");
return null;
}
while (!queue.isEmpty()) {
s.push(queue.dequeue());
}
while (!s.isEmpty()) {
tempQueue.enqueue(s.pop());
}
return tempQueue;
}
/////////////////////

private SingleLinkedList<E> lnkdQ = new SingleLinkedList<E>();


private Node<E> front;
private Node<E> rare; // check the appendix for the required classes
private int size; // or count

public void enqueue(E


(E element) {
if (size == 0) {
lnkdQ.insertLast(element);
front = rare = lnkdQ.head;
} else {
lnkdQ.insertLast(element);
rare = lnkdQ.tail;
}
++size;
}

public E element() {// front of queue


if (size == 0) {
throw new java.util.EmptyStackException();
}
return front.data;
}

public boolean isEmpty() {


return (size == 0);
}

public E dequeue() {
if (size == 0) {
throw new java.util.EmptyStackException();
}
E element = front.data;
lnkdQ.deleteFirst();
front = lnkdQ.head;
--size;
return element;
}

public int size() {


return size;
}

public void printQueue() {


System.out.print("[");
System. .print("[");
String str = "";
if (!isEmpty()) {
Node<E> current = front;
for (int i = 0; i < size; i++) {
str += current.data + ",";
current = current.next;
}
}
if (str.length() != 0) {
str = str.substring(0, str.length() - 1);

You might also like