יום שני, 28 בנובמבר 2011

Queues & Heaps


QUEUES & HEAPS
Queues

7_1
Yes it works in the FIFO behavior.
7_2
Using implementation of LinkedLists is ideal because they are very flexible at adding new objects at the front and the back, in retrospect I am not sure why you couldn't also use ArrayLists. I am not sure how to calculate the BigO here.
7_3
The add() method adds to the beginning whilst the addLast() adds to the end. I believe addLast() was used because in the case of Queues, adding means adding to the end.
7_4
I had to look at the API.
import java.util.ArrayList;
public class ArrayListQueue implements Queue 
private ArrayList list;

public void ListQueue() 
    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
Ease - Almost the same ease, almost all the methods were still one liners.
Efficiency - Not a change at all, ArrayLists still get the job done and the ease of writing its code is not much harder.

Heaps

Started.

אין תגובות:

הוסף רשומת תגובה