// now let's define our method // firstly, please notice the return type must be a List type, otherwise for // Null case, it cannot // successfully create the adding function! static CircularList insert(CircularList myList, int n) { // firstly if it is null if (myList == null) { return new CircularList(n); } else if (myList.next == myList) { // only one element // add this node myList.next = new CircularList(n); myList.next.next = myList; // the next's next reference points back // to the head so forms a cycle return myList.value < n ? myList : myList.next; // we check value // and return the // smaller one as // head } else if (n < myList.value) { // if it is the smallest element! // find tail and append! CircularList current = myList; while (current.next != myList) current = current.next; current.next = new CircularList(n); // add it after the tail current.next.next = myList; // set the appended node's next to // original header return current.next; // because this is smallest value! And that's // another reason we return List other than void } // otherwise, we either find a position when node.value<n and // node.next.value>n or node.next==head (largest) CircularList current = myList; while (current.next != myList && current.next.value <= n) { current = current.next; } CircularList currentNext = current.next; current.next = new CircularList(n); current.next.next = currentNext; // notice we made a copy of original // next and assign it here return myList; // return header position is unchanged! }
@Override public synchronized void close() throws Exception { Channel ch = channels.fetch(); while (ch != null) { close(ch); ch = channels.fetch(); } }
public static void main(String[] args) { // test now // first null case CircularList myList = null; myList = insert(myList, 1); myList.printList(); // expect 1 // myList.next = new CircularList(3); // myList.next.next = new CircularList(5); // myList.next.next.next = myList;// so expect list is 1>3>5 myList = insert(myList, 3); myList.printList(); // expect 1>2>3>5 myList = insert(myList, 5); myList.printList(); // expect 1>2>3>5 myList = insert(myList, 2); myList.printList(); // expect 1>2>3>5 myList = insert(myList, 0); myList.printList(); // expect 0>1>2>3>5 myList = insert(myList, 6); myList.printList(); // expect 0>1>2>3>5>6 myList = insert(myList, 3); myList.printList(); // expect 0>1>2>3>5>6 }
@Override public Channel fetch() { Channel ch = channels.fetch(); if (ch != null && ch.isActive()) { if (ch.attr(GlobalConstance.attributeKey).get() == SessionState.Connect) { return ch; } } return null; }
public static void main(String[] args) { // Queue Tests System.out.println("------QUEUE TESTS------"); Queue myQueue = new Queue(); int items = 10; for (int i = 0; i < items; i++) { System.out.println("Queuing up> " + i); myQueue.enQueue(i); } for (int i = 0; i < items; i++) { System.out.println("DeQueuing> " + myQueue.deQueue()); } System.out.println("------END QUEUE TESTS------\n"); // Circular List Tests System.out.println("------CIRCULAR LIST TESTS------"); CircularList circles = new CircularList(); circles.addFirst(1); circles.addFirst(2); circles.addFirst(3); System.out.println("Are we Empty? " + circles.isEmpty()); System.out.println(circles.removeFirst()); System.out.println(circles.removeFirst()); System.out.println(circles.removeFirst()); System.out.println("Are we Empty? " + circles.isEmpty()); System.out.println("------END CIRCULAR LIST TESTS------\n"); System.out.println("------BIG FACTORIAL DIVISION------"); Factorial factorial = new Factorial(); System.out.println(factorial.bigDivision(75, 73)); }