登录
  • #刷题
  • #careercup

Career Cup 2.4ListPartition我这个一运行Eclpse就崩溃了,咋回事啊

TonyJang
859
0
package Chapter2;[br][/br][br][/br]public class partition {[br][/br][br][/br]public static void partition(MyLinkedList<Integer> list,int k){[br][/br][br][/br]Node<Integer> BeforeStart=null;//左边是Node的引用,这个引用哪里也没指,指向null[br][/br][br][/br]Node<Integer> BeforeEnd=null;[br][/br][br][/br]Node<Integer> AfterStart=null;[br][/br][br][/br]Node<Integer> AfterEnd=null;[br][/br][br][/br]Node<Integer> Node=list.head;[br][/br][br][/br]while(Node!=null){[br][/br][br][/br]if(Node.data <k){[br][/br][br][/br]if(BeforeStart==null){[br][/br][br][/br]BeforeStart=Node;[br][/br][br][/br]BeforeEnd=BeforeStart;[br][/br][br][/br]}else{[br][/br][br][/br]BeforeEnd.next=Node;[br][/br][br][/br]BeforeEnd=Node;//BeforeEnd向前移动以便下一次遍历[br][/br][br][/br]}[br][/br][br][/br]}[br][/br][br][/br]else{[br][/br][br][/br]if(AfterStart==null){[br][/br][br][/br]AfterStart=Node;[br][/br][br][/br]AfterEnd=AfterStart;[br][/br][br][/br]}else{[br][/br][br][/br]AfterEnd.next=Node;[br][/br][br][/br]AfterEnd=Node;//AfterEnd向前移动以便下一次遍历[br][/br][br][/br]}[br][/br][br][/br]}[br][/br][br][/br]Node=Node.next;[br][/br][br][/br]}[br][/br][br][/br]BeforeEnd.next=AfterStart;}[br][/br][br][/br]public static void main(String[] args) {[br][br][/br]MyLinkedList<Integer> list = new MyLinkedList<>(new Integer[] { 1, 2,[br][br][/br]6,8,2,4,9,7});[br][/br][br][/br]list.print();[br][/br][br][/br]partition(list, 9);[br][/br][br][/br]list.print();[br][/br][br][/br]}[br][/br][br][/br]}[br][/br][br][/br]
Node.java
package Chapter2;[br][/br][br][/br] [br][/br][br][/br]public class Node<T> {  //T可以是int,string等[br][/br][br][/br]public T data;[br][/br][br][/br]public Node<T> next;[br][/br][br][/br]public Node(T d, Node<T> n){[br][/br][br][/br]data = d;[br][/br][br][/br]next = n;[br][/br][br][/br]}[br][/br][br][/br]}[br][/br][br][/br]
MyLikedList.JAVA
package Chapter2;[br][/br][br][/br] [br][/br][br][/br]//Singly Linked List[br][/br][br][/br]public class MyLinkedList<T> {[br][/br][br][/br]public Node<T> head;[br][/br][br][/br] [br][/br][br][/br]public MyLinkedList(Node<T> h) {[br][/br][br][/br]head = h;[br][/br][br][/br]}[br][/br][br][/br] [br][/br][br][/br]public MyLinkedList(T[] dataArray) {[br][br][/br]if (dataArray == null || dataArray.length <= 0)[br][/br][br][/br]return;[br][/br][br][/br]head = new Node<>(dataArray[0], null);[br][/br][br][/br]Node<T> node = head;//node指向head[br][/br][br][/br]for (int i = 1; i < dataArray.length; i++) {[br][/br][br][/br]node.next = new Node<T>(dataArray[i], null);[br][/br][br][/br]node = node.next;[br][/br][br][/br]}[br][/br][br][/br]}[br][/br][br][/br] [br][/br][br][/br]public void print() {[br][/br][br][/br]Node<T> cur = head;[br][/br][br][/br]while (cur != null) {[br][/br][br][/br]System.out.print(cur.data);[br][/br][br][/br]if (cur.next != null) {[br][/br][br][/br]System.out.print(" -> ");[br][/br][br][/br]}[br][/br][br][/br]cur = cur.next;[br][/br][br][/br]}[br][/br][br][/br]System.out.println();[br][/br][br][/br]}[br][/br][br][/br]}[br][/br][br][/br][/i]
0条回复
热度排序

发表回复