public class PQueue<E extends Comparable<E>> {
private E[] elements;
private int size;
private int head;
private int tail;
Private int count;
}
public void enqueue(E item) {
if(isFull()){
return;
}
count++;
elements[tail] = item;
tail = (tail + 1) % size;
}
public E dequeue() {
if(isEmpty()) return null;
int ct = count-1;
E cur = elements[head];
int index = 0;
for(i=1;ct-->0;i++) {
if(cur.compareTo(elements[head+i)%size])<0)
cur = elements[(head+i)%size];
index = i;
}
}
return remove((head+index%size);
public E remove(int index) {
E item = elements[index];
if(index != tail) {
int i;
for(i-index; (i%size)!=tail; i++){
int j = i%size;
elements[j] = elements[(j+1)%size];
}
}
tail = (tail-1)%size;
count--;'
return item;
}
}
public class Test{
public static void main(string[] argv) {
PQueue<Integer> queue=new PQueue<>(20);
for(int i = 1; i < 12; i++)
queue.enqueue(1);
for(int j = 1; j < 12; j++) {
system.out.print(queue.dequeue()+" ");
}
system.out.println();
}
}
1) Suppose the above code compiles, please write down the execution
results of "java Test"?
2) Given such a queue with n elements, please use the big-Oh notation to estimate the complexity of the enqueue() operations?
3) Given such a queue with n elements, please use the big-Oh notation to estimate the complexity of the dequeue() operations?
We are 12 number of 1s only to the queue.. which means all the data is same.. 1. When The data is dequeued the max element index is removed.. In this case max element will be at index 0, so first index 0 element is removed, then all the remaining elements are shifted towards their left. This process happens for all 12 numbers.. Hence Output: 1 1 1 1 1 1 1 1 1 1 1 1 2. Enqueue is always an O(1) operation, since we only need to directly add the element at the end of the list.. There is no need for traversal. 3. Dequeue is always an O(N) operation, since after removing the element, the remaining items need to be shifted to left.. Hence it is an O(N) operation.
public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private...
public class myLinkedList<E extends Comparable<E>>{ private Node<E> head = null; private Node<E> tail = null; public myLinkedList() { head = null; } public void concatenateList (myLinkedList<E> M) {//attach another linkedList refered by M to the end of this linkedList } public int searchElement (E targetElement){//test if a target element can be found on the list, return the occurrences of the target element ...
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size) { this.size = size; } /** Reference to list head. */ private Node<E> head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E...
e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...
Hello! I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP Task is! Improve the circular array implementation of the bounded queue by growing the elements array when the queue is full. Add assertions to check all preconditions of the methods of the bounded queue implementation. My code is! public class MessageQueue{ public MessageQueue(int capacity){ elements = new Message[capacity];...
Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> { // Data fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The underlying data array */ private E[] theData; /** The current size */ private int size = 0; /** The current capacity */ private int capacity = 0; @SuppressWarnings("unchecked") public KWArrayList() { capacity...
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1. You are given a list of phrases each ending with a pound sign: ‘#’. 2. Create a single String object from this list. 3. Then, split the String of phrases into an array of phrases...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...
Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Node getHeado return head public void setHead(Node head) [ head: this. head Method that creates a Node containing 'item' @param item Value to be added this.headnew Node(item, this.head) * and adds it as the first element in the list *I public void add(int item) Method that finds the node at the given index d replaces its value...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
Expected OUTPUT: Codes Failed at Test D,E,F. Needs to be fixed: public class Vector<T> { private const int DEFAULT_CAPACITY = 10; private T[] data; public int Count { get; private set; } = 0; public int Capacity { get { return data.Length; } } public Vector(int capacity) { data = new T[capacity]; } public Vector() : this(DEFAULT_CAPACITY) { } public T this[int index] { get { if (index >= Count || index < 0) throw new IndexOutOfRangeException(); return...