コード例 #1
0
 public void push(int value) {
   MyNode newNode = new MyNode(value);
   if (isEmpty()) {
     list.insertHead(newNode);
   } else {
     list.insertAfter(tail, newNode);
   }
   tail = newNode;
 }
コード例 #2
0
ファイル: MyLinkedList.java プロジェクト: JiaqiGao/MKS22X
 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();
 }
コード例 #3
0
ファイル: MyStack.java プロジェクト: michaelfeinberg/MKS22XHW
 public T peek() {
   if (isEmpty()) {
     throw (new NoSuchElementException());
   } else {
     return items.get(0);
   }
 }
コード例 #4
0
ファイル: MyStack.java プロジェクト: michaelfeinberg/MKS22XHW
 public T pop() {
   if (isEmpty()) {
     throw (new NoSuchElementException());
   } else {
     return items.remove(0);
   }
 }
コード例 #5
0
ファイル: MyLinkedList.java プロジェクト: OOYUKIOO/MKS22X
  // 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());
      }
    }
  }
コード例 #6
0
ファイル: Balls.java プロジェクト: hooitong/NUS-Semester-2
  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();
  }
コード例 #7
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);
  }
コード例 #8
0
  /** @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);
  }
コード例 #9
0
ファイル: MyStack.java プロジェクト: michaelfeinberg/MKS22XHW
 public int size() {
   return items.size();
 }
コード例 #10
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).");
 }
コード例 #11
0
 public int size() {
   return (list.size());
 }
コード例 #12
0
 public int pop() {
   return (list.removeHead());
 }
コード例 #13
0
 public String toString() {
   return LNode.toString();
 }
コード例 #14
0
 public boolean isEmpty() {
   return LNode.size() == 0;
 }
コード例 #15
0
ファイル: MyStack.java プロジェクト: michaelfeinberg/MKS22XHW
 public boolean isEmpty() {
   return items.size() == 0;
 }
コード例 #16
0
ファイル: MyStack.java プロジェクト: michaelfeinberg/MKS22XHW
 public void push(T item) {
   items.add(0, item);
 }
コード例 #17
0
 public void enqueue(T value) {
   LNode.add(value);
 }
コード例 #18
0
 public T dequeue() {
   if (LNode.size() == 0) {
     throw new NoSuchElementException();
   }
   return LNode.remove(0);
 }
コード例 #19
0
 public T peek() {
   if (LNode.size() == 0) {
     throw new NoSuchElementException();
   }
   return LNode.get(0);
 }
コード例 #20
0
 public boolean isEmpty() {
   return (list.isEmpty());
 }
コード例 #21
0
 public int size() {
   return LNode.size();
 }
コード例 #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);
 }
コード例 #23
0
 public MyLLIterator(MyLinkedList<T> enter) {
   current = enter.getNode(-1);
 }