Sunday, 27 November 2011

Queues, Heaps and Priority Queues

7.1 It returned true, makes sense because it's constructor doesn't fill it up with anything, and I haven't either. Then after I enqueue "one" "two" and "three" and dequeue once I get two when I peekFront(). This is all good because I dequeued one. isEmpty() returns false as it should.

Not clear on what FIFO is. Google search resulted in first in, first out? as in when I called enqueue on "one" first, and then when I dequeued it, it was the first out, since it was the first in? I guess that makes sense.

7.2 It makes it efficient because of what I just wrote up there. The FIFO nature of both LinkedLists and Queues make them a perfect match. I'm unsure of how to calculate BigO time, but I'm pretty sure it's something along the lines of log1 or something so close to zero, because it's not even BigO(n) as it doesn't loop or anything, all the methods are one liners which just return results which are already one step away from being found.

7.3 LinkedList probably has two different add methods, one for the beginning and one for the end. add() most likely adds an object to the beginning of the LinkedList whereas addLast() does the opposite.

7.4 It wasn't harder at all. Quite the same level of simplicity I would say. Still one liner and quick methods. Here's my implementation:

public class ArrayListQueue implements Queue
{

private ArrayList list;

public ArrayListQueue()
{
    list = new ArrayList();
}//constructor

public void enqueue(Object x)
{
    list.add(x);
}//enqueue

public Object dequeue()
{
    return list.remove(0);
}//dequeue

public Object peekFront()
{
    return list.get(0);
}//peekFront

public boolean isEmpty()
{
    return list.size() == 0;
}//isEmpty

}//class ListQueue

7.5 Like I said in 7.4 It's about the same. Efficiency and ease of coding was the same.

7.5 a Nothing much, it just seems to reinforce the idea of first in first out, and how that's a great advantage in many of the business related sectors of our life, where you queue an appointment first, and you therefore become the first to be appointed. The elevator in this simulation reinforces it, but in reality it's not whoever calls the elevator first that gets in, but whoever calls it first on it's way down or up. If you call an elevator from floor 24 before I call it from floor 4, when it's going up from floor 1, I will be the first to enter because it will stop on its way to pick me up. So it's similar, but not distinct.


No comments:

Post a Comment