public static void main(String[] args) { MyLinkedList L = new MyLinkedList(); System.out.println("Adding four nodes..."); L.add("Sully"); L.add("Mike"); L.add("Randall"); L.add("Boo"); System.out.println(L); System.out.println(); System.out.println("Adding new node at 1..."); L.add(1, "New Node"); System.out.println(L); System.out.println("Adding another new node at 3..."); L.add(3, "Another New Node"); System.out.println(L); System.out.println(); System.out.println("Node at index 3: " + L.get(3)); System.out.println(); System.out.println("Replacing index 3 with a replacement node"); L.set(3, "A Replacement Node"); System.out.println(L); System.out.println(); System.out.println("Removing node at 3..."); L.remove(3); System.out.println(L); System.out.println("Removing node at 0..."); L.remove(0); System.out.println(L); System.out.println(); System.out.println("Finding " + L.get(3) + " : " + L.find(L.get(3))); System.out.println(); System.out.println("Total length of linked list: " + L.size()); }
/** * Creates a <code>File</code> and places it into FAT32, the <code>File</code> list. * Pre-condition: size must be greater than 0 Post-condition: creates a <code>File</code> with * appropriate number of blocks and places it into internal <code>File</code> list (FAT32) * * @param filename the name of the <code>File</code> * @param size bytes in the <code>File</code> * @return successful creation message * @throws InSufficientDiskSpaceException if there are not enough blocks to create <code>File * </code> * @throws DuplicateFileException if a <code>File</code> with given filename already exists */ public String create(String filename, int size) throws InsufficientDiskSpaceException, DuplicateFileException { Iterator<File> findDuplicates = FAT32.iterator(); // Go through file list to find duplicates first while (findDuplicates.hasNext()) { if (findDuplicates.next().getName().equalsIgnoreCase(filename)) { throw new DuplicateFileException(); } } // Check for available space if (size > (freeBlocks.size() * blockSize)) { throw new InsufficientDiskSpaceException("Not enough disk space to create new file.\n"); } else { File file = new File(size, blockSize, filename); Iterator<Block> itr = freeBlocks.iterator(); // Iterate through list of available blocks, adding them to the new file as needed for (int i = 0; i < bytesBlocks(size); i++) { if (itr.hasNext()) { file.add(itr.next()); // After block is added to file, it's removed from available blocks itr.remove(); } } // Add file to file table FAT32.add(file); return filename + " created successfully!\n"; } }
/** * Returns a <code>String</code> representation of this object. * * @see java.lang.Object#toString() */ public String toString() { // Create StringBuilder Object to output FileManager status StringBuilder sb = new StringBuilder("\nFile Manager Status:\n--------------------\n"); // Appending easy details sb.append(String.format("Disk block size: %-5d \n", blockSize)); sb.append(String.format("Number of blocks: %-5d \n", totalBlocks)); sb.append(String.format("Allocated blocks: %-5d \n", (totalBlocks - freeBlocks.size()))); sb.append(String.format("Free blocks: %-5d \n", freeBlocks.size())); // Appending disk block ranges sb.append("\nFree disk blocks:\n-----------------\n"); Iterator<Block> blockItr = freeBlocks.iterator(); if (freeBlocks.isEmpty()) { sb.append("There are no free disk blocks.\n"); } else { // Algorithm to append only integer ranges int prev = freeBlocks.get(0).getNum(); while (blockItr.hasNext()) { Block block = blockItr.next(); if (block.getNum() >= (prev + 2)) { sb.append(prev + "]"); sb.append("[" + block.getNum() + " , "); } else if (block.getNum() == prev) { sb.append("[" + block.getNum() + " , "); } prev = block.getNum(); } sb.append(prev + "]\n"); } // Appending files and their details sb.append("\nFiles:\n------\n"); if (FAT32.size() == 0) { sb.append("There are no files.\n"); } else { Iterator<File> fileItr = FAT32.iterator(); while (fileItr.hasNext()) { sb.append(fileItr.next().toString()); } } return new String(sb); }
public static void reverse(MyLinkedList list) { int size = list.size(); if (size == 0 || size == 1) { return; } ListIterator<Object> iterForward = list.listIterator(0); ListIterator<Object> iterBackward = list.listIterator(size - 1); for (int i = 0; i < size / 2; i++) { Object temp = iterForward.next(); iterForward.set(iterBackward.previous()); iterBackward.set(temp); } }
/** * Extends a <code>File</code> by the given size in bytes. Pre-condition: size must be greater * than 0 Post-condition: <code>File</code> with the given filename will be extended by passed in * amount of bytes, and additional <code>Blocks</code> will be allocated to it as needed * * @param filename the name of the <code>File</code> * @param size number of bytes to increase the <code>File</code> by * @return successful extension message * @throws InSufficientDiskSpaceException if there are not enough blocks to extend <code>File * </code> * @throws NoSuchFileException if the filename does not match any existing <code>File</code> */ public String extend(String filename, int size) throws InsufficientDiskSpaceException, NoSuchFileException { Iterator<File> findFile = FAT32.iterator(); boolean fileFound = false; // Check if file exists while (findFile.hasNext()) { if (findFile.next().getName().equalsIgnoreCase(filename)) { fileFound = true; } } // Throw exception if it doesn't if (fileFound == false) { throw new NoSuchFileException(filename + " does not exist!"); } // Check there is enough space to extend file if (size > (freeBlocks.size() * blockSize)) { throw new InsufficientDiskSpaceException( "Not enough disk space to extend " + filename + " \n"); } else { Iterator<File> itr = FAT32.iterator(); // Iterate through list of files to find filename while (itr.hasNext()) { File file = itr.next(); if (file.getName().equals(filename)) { // Calculate the extended size and how many blocks need to be added int newSize = file.getBytes() + size; int blocksToAdd = bytesBlocks(newSize) - bytesBlocks(file.getBytes()); // Take blocks from available blocks and add them to the file for (int i = 0; i < blocksToAdd; i++) { file.add(freeBlocks.remove(0)); } file.addBytes(size); } } // Throw exception or return appropriate message if (fileFound == false) { throw new NoSuchFileException(filename + " does not exist!"); } else { return filename + " extended successfully!\n"; } } }
public int size() { return (list.size()); }
public boolean isEmpty() { return items.size() == 0; }
public int size() { return items.size(); }
public int size() { return LNode.size(); }
public T peek() { if (LNode.size() == 0) { throw new NoSuchElementException(); } return LNode.get(0); }
public T dequeue() { if (LNode.size() == 0) { throw new NoSuchElementException(); } return LNode.remove(0); }
public boolean isEmpty() { return LNode.size() == 0; }
/** * This is the main method of the tester. Prepare for exceptions. I'm extremely mean on this one. */ public static void main(String[] args) { MyLinkedList<Integer> list = new MyLinkedList<Integer>(); LinkedList<Integer> orig = new LinkedList<Integer>(); final int NUM_TEST_TYPES = 7; for (int i = 0; i < 500; i++) { int div = i % NUM_TEST_TYPES; if (div == 0 || div == 1) { Integer[] to_add = new Integer[5]; for (int j = 0; j < 5; j++) to_add[j] = new Integer( (i * (int) (Math.sin(i * i + j) * 1000) * i + i * i) / ((int) Math.cos(i * i * i - j * i) * 10000 + 1 + i)); if (div == 0) { for (int j = 0; j < 5; j++) { System.out.println("Adding " + to_add[j] + " to beginning with addFirst()."); list.addFirst(to_add[j]); orig.addFirst(to_add[j]); } } else { for (int j = 0; j < 5; j++) { System.out.println("Adding " + to_add[j] + " to end with addLast()."); list.addLast(to_add[j]); orig.addLast(to_add[j]); } } } else if ((div == 2) || (div == 3)) { if (div == 2) { System.out.println("Removing the last element of the list with removeLast()."); list.removeLast(); orig.removeLast(); } else { System.out.println("Removing the first element of the list with removeFirst()."); list.removeFirst(); orig.removeFirst(); } } else if (div == 4) { System.out.println("Testing the remove() function by itself."); int origSize = orig.size(); for (int j = origSize - 1; j > (origSize / 2); j--) { System.out.println("Remove(" + j + ")"); list.remove(j); orig.remove(j); System.out.println("Orig: " + orig.toString()); System.out.println("Your List: " + list.toString()); if (!list.toString().equals(orig.toString())) throw new RuntimeException("Remove() function failed at index " + j); if (list.size() != orig.size()) throw new RuntimeException( "Your list was the wrong size after removing node at index " + j); } } else if (div == 5) { int indToAdd = (int) (Math.random() * 100 * i) % orig.size(), toAdd = (int) (Math.random() * 100); System.out.println("Testing arbitrary add() method at index " + indToAdd); list.add(indToAdd, toAdd); orig.add(indToAdd, toAdd); } else if (div == (NUM_TEST_TYPES - 1)) { System.out.println("Testing the remove() function in conjunction with iterators."); Iterator<Integer> it = list.iterator(); for (int j = orig.size() - 1; j > 0; j--) { System.out.println("Iterator.remove(" + j + ")"); it.next(); it.remove(); orig.removeFirst(); System.out.println("Orig: " + orig.toString()); System.out.println("Your List: " + list.toString()); if (!list.toString().equals(orig.toString())) throw new RuntimeException("Iterator.remove() function failed at index " + j); if (list.size() != orig.size()) throw new RuntimeException( "Your list was the wrong size after removing node at index " + j); } } System.out.println("Orig: " + orig.toString()); System.out.println("Your List: " + list.toString()); int listSum = 0, origSum = 0; if (list.size() != orig.size()) throw new RuntimeException( "Your list is the wrong size (should have been " + orig.size() + ")."); for (Iterator<Integer> it = list.iterator(); it.hasNext(); ) listSum += it.next(); for (Integer j : orig) origSum += j; if (listSum != origSum || !list.toString().equals(orig.toString())) throw new RuntimeException( "Check your iterator and linked list implementation, and also your add*() and remove*() methods."); } System.out.println( "\n\nCONGRATULATIONS! Your LinkedList implementation's iterators and add and remove methods work (or at least, most of them)."); }