public static void main(String[] args) { MyLinkedList L = new MyLinkedList(); L.add("Bob"); L.add("Joe"); L.add("Tom"); }
public boolean add(int index, T value) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException(); } if (index == size) { add(value); } else { if (index == 0) { LNode ad = new LNode(value); ad.setNext(start); start = ad; if (start == null) { end = ad; } } else { LNode current = start; for (int i = 0; i < index - 1; i++) { current = current.getNext(); } LNode ad = new LNode(value); LNode move = current.getNext(); ad.setNext(move); current.setNext(ad); if (index == size) { end = current.getNext(); } } size++; } return true; }
/** * Deletes a <code>File</code> with the given name from the internal <code>File</code> list * Post-condition: <code>File</code> with the passed in filename will be deleted and its <code> * Blocks</code> added back to available <code>Blocks</code> * * @param filename the name of the <code>File</code> * @return successful deletion message * @throws NoSuchFileException if the filename does not match any existing <code>File</code> */ public String delete(String filename) throws NoSuchFileException { Iterator<File> itr = FAT32.iterator(); boolean fileFound = false; // Iterate through list of files to look for filename while (itr.hasNext()) { File file = itr.next(); if (file.getName().equals(filename)) { fileFound = true; // Iterate through file's blocks, adding them back to available blocks for (int i = 0; i < bytesBlocks(file.getBytes()); i++) { freeBlocks.add(file.remove()); } // Remove file itr.remove(); } } // Throw an exception or return appropriate message if (fileFound == false) { throw new NoSuchFileException(filename + " does not exist!"); } else { return filename + " deleted successfully!\n"; } }
public static void main(String[] args) { MyLinkedList list = new MyLinkedList(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); list.add(7); list.add(8); list.printList(); System.out.println(); list.ReverseList(); list.printList(); }
public boolean add(int pos, T value) { LNode newNode = new LNode(value); LNode temp = start; LNode after; int index = 0; if (pos >= 0 && pos <= getSize()) { if (getSize() == 0 || pos == getSize()) { add(value); } else { while (index < pos - 1) { temp = temp.getNext(); index++; } if (pos == 0) { after = temp; start = newNode; // new after.setPrevious(start); start.setNext(after); // start.getNext().setPrevious(); } else { after = temp.getNext(); // new after.setPrevious(newNode); newNode.setPrevious(temp); temp.setNext(newNode); newNode.setNext(after); } size++; } return true; } else { throw new IndexOutOfBoundsException(); } }
/** * 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"; } }
public static void main(String[] args) { MyLinkedList<Integer> l = new MyLinkedList<Integer>(); l.add(4); l.add(8); l.add(9); l.add(7); l.add(12); System.out.println(l); // System.out.println(l.indexOf(9)); System.out.println("element at 3 is: " + l.get(3)); // System.out.println(l.set(2,99)); l.set(2, 99); // System.out.println(l); l.add(3, 23); System.out.println(l); System.out.println("Removed: " + l.remove(4)); l.add(20); System.out.println(l); int j = 0; for (Integer i : l) { System.out.println(i); } MyLinkedList<String> z = new MyLinkedList<String>(); z.add("a"); z.add("b"); z.add("c"); // System.out.println(z); }
public MyLinkedList(int length) { size = 0; if (length != 0) { start = new LNode(item); end = start; while (getSize() < length) { add(item); } } }
/** * Constructor for <code>FileManager</code>. Pre-condition: blocks and size must be greater than * 0. * * @param blocks the number of <code>Blocks</code> in our "disk" * @param size number of bytes per <code>Block</code> */ public FileManager(int blocks, int size) { // Initialize variables totalBlocks = blocks; blockSize = size; freeBlocks = new MyLinkedList<Block>(); FAT32 = new MyLinkedList<File>(); // Populate list of available blocks for (int i = 0; i < totalBlocks; i++) { freeBlocks.add(new Block(i)); } }
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()); }
public static void main(String[] args) { Node n1; n1 = new Node("Tommy"); System.out.println(n1); Node n2 = new Node("Sammy"); System.out.println(n2); n1.setNext(n2); System.out.println(n1.getNext()); n2.setNext(new Node("Clyde")); System.out.println(n1.getNext().getNext()); System.out.println(n2.getNext()); /* example of removing a node n1.setNext(n2.getNext()); // or n1.setNext(n1.getNext().getNext()); System.out.println("After removing second node"); System.out.println(n1); System.out.println(n1.getNext()); System.out.println(n1.getNext().getNext()); */ Node last = n1.getNext().getNext(); last.setNext(n1); // creates a loop by connecting last to first System.out.println("Looping through the list"); System.out.println(n1.getNext()); System.out.println(n1.getNext().getNext()); System.out.println(n1.getNext().getNext().getNext()); System.out.println(n1.getNext().getNext().getNext().getNext()); System.out.println("Testing toString method"); MyLinkedList m1 = new MyLinkedList(); m1.add("A"); m1.add("B"); m1.add("C"); System.out.println(m1); System.out.println("Testing add(int i, String s)"); m1.add(1, "D"); System.out.println(m1); }
public static void main(String[] args) { MyLinkedList<Integer> m = new MyLinkedList<Integer>(); for (int i = 0; i < 10; i++) { m.add(i); } Iterator<Integer> it = m.iterator(); // it.next(); // enhanced for loop for (Integer i : m) { System.out.print(i + " "); } System.out.println(); }
// testing public static void main(String[] args) { if (debug) { MyLinkedList<Integer> test = new MyLinkedList<Integer>(0); for (int i = 0; i < 20; i++) { test.add(new Integer(i)); } System.out.println(test.end.getPrevious().getValue()); System.out.println(test.start.getNext().getPrevious().getValue()); System.out.println(test); for (int i = 0; i < 14; i++) { System.out.println("Removed element is: " + test.remove(0)); System.out.println("First element is: " + test.start.getNext().getPrevious().getValue()); } } }
/** * Truncates 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 truncated by passed in * amount of bytes, and <code>Blocks</code> will be de-allocated to it as needed * * @param filename the name of the <code>File</code> * @param size number of bytes to truncate the <code>File</code> by * @return successful truncation message * @throws NoSuchFileException if the filename does not match any existing <code>File</code> * @throws FileUnderflowException if number of truncating bytes is greater than <code>File</code> * size */ public String truncate(String filename, int size) throws NoSuchFileException, FileUnderflowException { Iterator<File> itr = FAT32.iterator(); boolean fileFound = false; // Iterate through list of files to find filename while (itr.hasNext()) { File file = itr.next(); if (file.getName().equals(filename)) { fileFound = true; // If truncating by total file size, delete the file instead if (file.getBytes() == size) { delete(filename); } else if (file.getBytes() < size) { throw new FileUnderflowException( "Tried to truncate " + filename + " by " + size + " bytes, but it is only " + file.getBytes() + " bytes!\n"); } else { // Calculate the truncated size and how many blocks need to be removed int newSize = file.getBytes() - size; int blocksToRemove = bytesBlocks(file.getBytes()) - bytesBlocks(newSize); // Take blocks from file and add them to available blocks for (int i = 0; i < blocksToRemove; i++) { freeBlocks.add(file.remove()); } file.delBytes(size); } } } // Throw exception or return appropriate message if (fileFound == false) { throw new NoSuchFileException(filename + " does not exist!"); } else { return filename + " truncated successfully!\n"; } }
public boolean add(int index, T value) { if (index > size || index < 0) { throw new IndexOutOfBoundsException(); } else if (index == 0) { LNode current = start; start = new LNode(value); start.setNext(current); size++; } else if (index == size) { add(value); } else { LNode current = start; for (int i = 0; i < index - 1; i++) { current = current.getNext(); } LNode tmp = current.getNext(); current.setNext(new LNode(value)); current.getNext().setNext(tmp); size++; } return true; }
/** * 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)."); }
public void enqueue(T value) { LNode.add(value); }
public static void copy(MyLinkedList dest, MyLinkedList src) { dest.clear(); for (Object o : src) { dest.add(o); } }
public void push(T item) { items.add(0, item); }
/** * Constructor * * <p>O(n) */ public MyLinkedList(E[] elements) { this(); // calls the first constructor for (int i = 0; i < elements.length; ++i) { add(elements[i]); } }