Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your solution to this assignment will take the form of a short report in a single document. It MUST be in PDF format. The purpose

Your solution to this assignment will take the form of a short report in a single document. It MUST be in PDF format.

The purpose of this assignment is to analyse two Queue implementations theoretically and experimentally.

As we have discussed in lectures, linked lists and arrays have different strengths and weaknesses in their performance. In this assignment, we will analyse how they affect the corresponding Queue implementations.

Part A (8 marks):

Write a program that operates as follows:

The user is asked to specify a number of elements

In a loop, this number of Strings is added to a Queue; each String should be different (for example, you could include a different number in each one)

All of the Strings are then removed from the Queue

The program displays how long it took to enqueue the items and dequeue them. (Tip: you can use Javas System.nanoTime() to get the current time in nanoseconds; it returns a long, so if you subtract one from another, you get the number of elapsed seconds.)

/////////////////////////LLQueue.java///////////////////////////////

public class LLQueue implements Queue{

private SLinkedList queue;

public LLQueue() {

queue = new SLinkedList();

}

@Override

public void enqueue(Object n) {

queue.gotoTail();

queue.insertNext(n);

}

@Override

public Object dequeue() {

queue.gotoHead();

Object o = queue.getCurr();

queue.deleteHead();

return o;

}

@Override

public boolean isEmpty() {

return queue.isEmpty();

}

@Override

public boolean isFull() {

return false;

}

@Override

public Object front() {

queue.gotoHead();

return queue.getCurr();

}

}

/////////////////////Test.java//////////////////////////////

public class Test {

public static void main(String []args) {

LLQueue l = new LLQueue();

System.out.println("IsEmpty: "+l.isEmpty());

l.enqueue("one");

l.enqueue("two");

l.enqueue("three");

l.enqueue("four");

l.enqueue("five");

l.enqueue("six");

System.out.println("After Inserting elements");

System.out.println("IsEmpty: "+l.isEmpty());

while(!l.isEmpty()){

String s = (String)l.dequeue();

System.out.println(s);

}

System.out.println("After Deleting elements");

System.out.println("IsEmpty: "+l.isEmpty());

}

}

import javax.swing.JOptionPane; /** * : Array implementation of Queue ADT */ public class ArrayQueue implements Queue { protected Object Q[]; // array used to implement the queue protected int rear = -1; // index for the rear of the queue protected int capacity; // The actual capacity of the queue array public static final int CAPACITY = 1000; // default array capacity public ArrayQueue() { // default constructor: creates queue with default capacity this(CAPACITY); } public ArrayQueue(int cap) { // this constructor allows you to specify capacity capacity = (cap > 0) ? cap : CAPACITY; Q = new Object[capacity]; } public void enqueue(Object n) { if (isFull()) { JOptionPane.showMessageDialog(null, "Cannot enqueue object; queue is full."); return; } rear++; Q[rear] = n; } public Object dequeue() { // Can't do anything if it's empty if (isEmpty()) return null; Object toReturn = Q[0]; // shuffle all other objects towards 0 int i = 1; while (i <= rear) { Q[i-1] = Q[i]; i++; } rear--; return toReturn; } public boolean isEmpty() { return (rear < 0); } public boolean isFull() { return (rear == capacity-1); } public Object front() { if (isEmpty()) return null; return Q[0]; } } 
/* * THIS INTERFACE CANNOT BE CHANGED: Abstract Queue interface */ public interface Queue { // most important methods public void enqueue(Object n); // add an object at the rear of the queue public Object dequeue(); // remove an object from the front of the queue // others public boolean isEmpty(); // true if queue is empty public boolean isFull(); // true if queue is full (if it has limited storage) public Object front(); // examine front object on queue without removing it } 

if there is anything missing let me know

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Write short notes on Interviews.

Answered: 1 week ago

Question

Define induction and what are its objectives ?

Answered: 1 week ago