/* 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()); }
// 20 marks /* pre: Have list of available memory blocks (Free) and list of memory blocks detected as being unusable (Corrupt), * each of which could be empty. Each list is sorted in ascending order of memory block ID, with the first point of access * having the smallest memory block ID value. Memory block IDs are unique within each list, but not necessarily unique * across lists. Each memory block ID in the unusable list appears in the list of available memory blocks as well. * post: In ascending order of memory block ID, display those available memory block IDs that are not corrupt. * Determine and return the percentage (a value between 0 and 1) of the total size of the available memory blocks that * are corrupt. * NOTE: Each list may only be processed a MAXIMUM of 1 time. * Make appropriate use of method getNextMemoryBlock (which must also be coded). * The contents of the lists do not have to be retained. */ public double findWastage() { // ADD CODE FOR METHOD findWastage BELOW sortFree(); //Change from descending to ascending so that smallest is first accessible PQueue tempPQueue = new PQueue(); double totalCorrupt = 0; int numberCorrupt = 0; do { Block tempFree = getNextMemoryBlock(Free); Block tempCorrupt = getNextMemoryBlock(Corrupt); if (tempFree != null && tempCorrupt != null) { if (tempFree.MemID == tempCorrupt.MemID) { totalCorrupt += tempCorrupt.Size; numberCorrupt++; } else { tempPQueue.Enqueue(tempFree); tempPQueue.Enqueue(tempCorrupt); } } else if (tempFree == null) { totalCorrupt += tempCorrupt.Size; numberCorrupt++; } else tempPQueue.Enqueue(tempFree); } while (Free.Count() > 0 || Corrupt.Count > 0); int totalAvailable = 0; while (tempPQueue.Count > 0) { Block tempBlock = (Block)tempPQueue.Dequeue(); tempBlock.display(); totalAvailable += tempBlock.Size; } Console.WriteLine("Total available memory = {0}kb",totalAvailable); return numberCorrupt == 0 ? 0 : totalCorrupt / totalAvailable; }