Esempio n. 1
0
 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());
 }
Esempio n. 2
0
 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);
 }
Esempio n. 3
0
 public T pop() {
   if (isEmpty()) {
     throw (new NoSuchElementException());
   } else {
     return items.remove(0);
   }
 }
Esempio n. 4
0
  /**
   * 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";
      }
    }
  }
Esempio n. 5
0
  // 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());
      }
    }
  }
Esempio n. 6
0
 public T dequeue() {
   if (LNode.size() == 0) {
     throw new NoSuchElementException();
   }
   return LNode.remove(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).");
 }