中级农民
- 积分
- 268
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-1-13
- 最后登录
- 1970-1-1
|
注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
- package Chapter2;
- public class partition {
- public static void partition(MyLinkedList<Integer> list,int k){
- Node<Integer> BeforeStart=null;//左边是Node的引用,这个引用哪里也没指,指向null
- Node<Integer> BeforeEnd=null;
- Node<Integer> AfterStart=null;
- Node<Integer> AfterEnd=null;
- Node<Integer> Node=list.head;
- while(Node!=null){
- if(Node.data <k){
- if(BeforeStart==null){
- BeforeStart=Node;
- BeforeEnd=BeforeStart;
- }else{
- BeforeEnd.next=Node;
- BeforeEnd=Node;//BeforeEnd向前移动以便下一次遍历
- }
- }
- else{
- if(AfterStart==null){
- AfterStart=Node;
- AfterEnd=AfterStart;
- }else{
- AfterEnd.next=Node;
- AfterEnd=Node;//AfterEnd向前移动以便下一次遍历
- }
- }
- Node=Node.next;
- }
- BeforeEnd.next=AfterStart;}
- public static void main(String[] args) {
- MyLinkedList<Integer> list = new MyLinkedList<>(new Integer[] { 1, 2,
- 6,8,2,4,9,7});
- list.print();
- partition(list, 9);
- list.print();
- }
- }
复制代码 Node.java- package Chapter2;
-
- public class Node<T> { //T可以是int,string等
- public T data;
- public Node<T> next;
- public Node(T d, Node<T> n){
- data = d;
- next = n;
- }
- }
复制代码 MyLikedList.JAVA- package Chapter2;
-
- //Singly Linked List
- public class MyLinkedList<T> {
- public Node<T> head;
-
- public MyLinkedList(Node<T> h) {
- head = h;
- }
-
- public MyLinkedList(T[] dataArray) {
- if (dataArray == null || dataArray.length <= 0)
- return;
- head = new Node<>(dataArray[0], null);
- Node<T> node = head;//node指向head
- for (int i = 1; i < dataArray.length; i++) {
- node.next = new Node<T>(dataArray[i], null);
- node = node.next;
- }
- }
-
- public void print() {
- Node<T> cur = head;
- while (cur != null) {
- System.out.print(cur.data);
- if (cur.next != null) {
- System.out.print(" -> ");
- }
- cur = cur.next;
- }
- System.out.println();
- }
- }
复制代码 |
上一篇: 小白求助一个简单的C语言程序下一篇: 刷题好累怎么办
|