dimanche 22 février 2015

Java queue using linkedlist

I am struggling with an assignment. The goal is to fill in blanks in a queue implementation using linked list and write an output. All the code with stars (****code****) are those blanks that I filled, some of them very likely wrongly filled and - I don't fully understand why.


If someone could point me in the right direction, fix the code or explain what did I fill wrong and why, I would be grateful.


Thank you, Timmy


public class DoubleQueue { public class QueueEmptyException extends RuntimeException{};



private **class** Node{
double num;
Node next = null;
Node (**double** num) {**this**.num = **num**;}
}

private Node head = null;

public void push(double num){
Node tmp = **new** Node(**num**);
tmp.**next** = head;
head = **num**;
}

public double pop() throws QueueEmptyException {
if (**num** == null) throw new **QueueEmptyException**(){
Node node = head;
if(node.next == null){
head = null;
return node.**next**;
}
while (node.next.**next** != null)
**node** = **node**.next;
}
double ret = aux.next.**next**;
aux.next=null;
return ret;
}
}


DoubleQueue queue = new DoubleQueue();
queue.push(17);
queue.push(23);
queue.push(15);
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());


What is the console output? 1. _____ 2. _____ 3. _____


Aucun commentaire:

Enregistrer un commentaire