Esempio n. 1
0
  /**
   * 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";
    }
  }
Esempio n. 2
0
 public static void main(String[] args) {
   // TODO Auto-generated method stub
   int[] a = {1, 2, 3, 4, 5};
   MyLinkedList list = new MyLinkedList();
   list.addAll(a);
   System.out.print(FindKthToTail(list.head, 1).val);
 }
Esempio n. 3
0
  /**
   * 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[]) {
    myList1.insertAtBegin(11);
    myList1.insertAtEnd(12);
    myList1.insertAtEnd(13);
    myList1.insertAtEnd(14);
    myList1.insertAtEnd(15);
    myList1.insertAtEnd(16);
    myList1.insertAtEnd(17);
    System.out.println("Original List1: " + myList1.toString());

    myList2.insertAtEnd(14);
    myList2.insertAtEnd(113);
    myList2.insertAtEnd(124);
    myList2.insertAtEnd(134);
    myList2.insertAtEnd(154);
    myList2.insertAtEnd(164);
    myList2.insertAtEnd(174);
    System.out.println("Original List2: " + myList2.toString());
    Node head = mergeLists(myList1.head, myList2.head);
    while (head.getNext() != null) {
      System.out.print(head.getData() + "-");
      head = head.getNext();
    }

    head = mergeListRecursion(myList1.head, myList2.head);
    while (head.getNext() != null) {
      System.out.print(head.getData() + " - ");
      head = head.getNext();
    }
  }
Esempio n. 5
0
  public static void main(String[] args) {

    MyLinkedList L = new MyLinkedList();

    L.add("Bob");
    L.add("Joe");
    L.add("Tom");
  }
 public void push(int value) {
   MyNode newNode = new MyNode(value);
   if (isEmpty()) {
     list.insertHead(newNode);
   } else {
     list.insertAfter(tail, newNode);
   }
   tail = newNode;
 }
Esempio n. 7
0
  /**
   * ******************************************************
   *
   * @param m string to be converted to char and into linked list
   * @return linked list of char, same as original param string
   *     <p>Converts string to linked list of characters.
   *     *****************************************************
   */
  private MyLinkedList<Character> StringToLinkedList(String m) {

    // temporary message to convert string to
    MyLinkedList<Character> msg = new MyLinkedList<Character>();

    // adds each element to temp linked list
    for (int i = m.length() - 1; i >= 0; i--) {
      msg.addFirst(m.charAt(i));
    }

    return msg;
  }
Esempio n. 8
0
 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();
 }
Esempio n. 9
0
 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);
   }
 }
Esempio n. 10
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. 11
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. 12
0
 public T pop() {
   if (isEmpty()) {
     throw (new NoSuchElementException());
   } else {
     return items.remove(0);
   }
 }
Esempio n. 13
0
 public T peek() {
   if (isEmpty()) {
     throw (new NoSuchElementException());
   } else {
     return items.get(0);
   }
 }
Esempio n. 14
0
  /**
   * 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";
    }
  }
Esempio n. 15
0
  /**
   * 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));
    }
  }
  /** @param args the command line arguments */
  public static void main(String[] args) throws FileNotFoundException {
    // ask the user for the file name
    System.out.print("Please enter the name of the file: ");
    Scanner keyboard = new Scanner(System.in);

    // create the new file object
    File newFile = new File(keyboard.nextLine());

    // link the file to the new Scanner object
    Scanner inputFile = new Scanner(newFile);

    // create a new linked list
    MyLinkedList myList = new MyLinkedList();

    // loop through the file to add each score to the linked list
    while (inputFile.hasNext()) {
      // get the next score
      double x = inputFile.nextDouble();

      // create the node and add to the linkedList
      MyLinkedListNode newNode = new MyLinkedListNode(x);
      myList.appendNode(newNode);
    }

    // loop through the list to find the total
    MyLinkedListNode p1 = myList.getHead();
    double total = 0;
    int count = 0;

    while (p1 != null) {
      total += p1.getNum();
      count++;

      // move along to the next node
      p1 = p1.getNext();
    }

    System.out.println("/nThe total of all the scores is: " + total);
    System.out.printf("The average of all the scores is: %.02f %n", total / count);
  }
Esempio n. 17
0
  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);
  }
Esempio n. 18
0
  public static void main(String[] args) {
    // declare the necessary variables
    MyLinkedList balls = new MyLinkedList();

    // declare a Scanner object to read input
    Scanner sc = new Scanner(System.in);

    // read input and process them accordingly
    int noOfBalls = sc.nextInt();

    // create nodes from 1 .... N and store into list
    for (int i = 1; i <= noOfBalls; i++) {
      balls.addLast(i);
    }

    // get the number of operations the user have and iterate
    int operations = sc.nextInt();

    for (int i = 0; i < operations; i++) {
      String mode = sc.next(); // get the mode of operation
      if (mode.equals("A")) {
        balls.doA(sc.nextInt(), sc.nextInt());
      } else if (mode.equals("B")) {
        balls.doB(sc.nextInt(), sc.nextInt());
      } else if (mode.equals("R")) {
        balls.doR(sc.nextInt());
      }
    }

    // print output
    balls.print();
  }
Esempio n. 19
0
  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();
  }
Esempio n. 20
0
  /**
   * 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);
  }
Esempio n. 21
0
  /** A testing method */
  public static void main(String[] args) {

    MyLinkedList<Integer> aList = new MyLinkedList<Integer>();

    for (int i = 0; i < 10; i++) {
      aList.addNode(i);
    }

    Node<Integer> aNode = aList.head;
    while (aNode != null) {
      System.out.print(aNode.data + " ");
      aNode = aNode.next;
    }
    System.out.println();

    System.out.println("Contains 7 (true): " + aList.contains(7));
    System.out.println("Contains 13 (false): " + aList.contains(13));

    aList.removeLast();

    System.out.println("Contains 9 (false): " + aList.contains(9));

    Node<Integer> bNode = aList.head;
    while (bNode != null) {
      System.out.print(bNode.data + " ");
      bNode = bNode.next;
    }
    System.out.println();

    MyLinkedList<String> bList = new MyLinkedList<String>();
    for (int j = 0; j < 6; j++) {
      bList.addNode("string number " + j);
    }
    Node<String> cNode = bList.head;
    while (cNode != null) {
      System.out.println(cNode.data);
      cNode = cNode.next;
    }
  }
Esempio n. 22
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. 23
0
 public int size() {
   return items.size();
 }
 public int size() {
   return (list.size());
 }
Esempio n. 25
0
 public static void copy(MyLinkedList dest, MyLinkedList src) {
   dest.clear();
   for (Object o : src) {
     dest.add(o);
   }
 }
 public boolean isEmpty() {
   return (list.isEmpty());
 }
 public int pop() {
   return (list.removeHead());
 }
Esempio n. 28
0
 public boolean isEmpty() {
   return items.size() == 0;
 }
Esempio n. 29
0
 public int size() {
   return LNode.size();
 }
Esempio n. 30
0
 public void push(T item) {
   items.add(0, item);
 }