public MemoryManager(string FileName1, string FileName2) { Used = new DoublyLinkedList(); Free = new SinglyLinkedList(); Corrupt = new Stack(); // initialising Used and Free lists readData(FileName1, FileName2); }
/* pre: Have list of available (Free) memory blocks, which could be empty. * post: Uses a priority queue to sort the list in ascending order of memory block ID. */ public void sortFree() { PQueue sortQ = new PQueue(); for (Node cur = Free.getFirst(); cur != null; cur = cur.next()) sortQ.Enqueue(cur.value()); Free = new SinglyLinkedList(); while (sortQ.Count > 0) Free.addLast(sortQ.Dequeue()); }
// 10 marks /* pre: Have list of available (Free) memory blocks, which could be empty. * post: EFFICIENTLY find the smallest memory block available that will suite the specified size (does not have to be the exact same * size). * Return a link to the node containing the appropriately sized memory block. If there is no appropriately sized memory * block, return null. * The list of available (Free) memory blocks should retain its ordering. * Wherever possible, use MUST be made of existing methods. */ public Node findMemory(int Size) { // ADD CODE FOR METHOD findMemory BELOW SinglyLinkedList temp = new SinglyLinkedList(); Node cur = Free.getFirst(), prev = null; do { if (Free.Count() == 0) return null; if (prev != null && ((Block)cur.value()).Size > Size) return prev; prev = cur; cur = cur.next(); } while (cur != null); return null; }