Skip to content

Commit 6dd2578

Browse files
committed
添加链表反转操作和数组合并操作
1 parent 0964e4e commit 6dd2578

File tree

4 files changed

+207
-4
lines changed

4 files changed

+207
-4
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.chen.algorithms;
2+
3+
/**
4+
* 两个有序数组合并为一个有序数组
5+
* http://www.cnblogs.com/A_ming/archive/2010/04/15/1712313.html
6+
*
7+
* @Author chenweijie
8+
* @Date 2017/10/16 2:18
9+
*/
10+
public class ArraySort {
11+
12+
//两个有序数组的合并函数
13+
public static int[] mergeList(int[] a, int[] b) {
14+
15+
int result[];
16+
17+
if (checkSort(a) && checkSort(b)) {
18+
19+
result = new int[a.length + b.length];
20+
21+
//i:用于标示a数组 j:用来标示b数组 k:用来标示传入的数组
22+
int i = 0, j = 0, k = 0;
23+
while (i < a.length && j < b.length) {
24+
if (a[i] < b[j]) {
25+
result[k++] = a[i++];
26+
} else {
27+
result[k++] = b[j++];
28+
}
29+
30+
}
31+
32+
// 后面连个while循环是用来保证两个数组比较完之后剩下的一个数组里的元素能顺利传入
33+
34+
while (i < a.length) {
35+
result[k++] = a[i++];
36+
}
37+
38+
while (j < b.length) {
39+
result[k++] = b[j++];
40+
}
41+
42+
return result;
43+
44+
45+
} else {
46+
System.out.println("非有序数组。。。");
47+
return null;
48+
}
49+
50+
}
51+
52+
53+
//检查数组是否是顺序存储的
54+
public static boolean checkSort(int[] n) {
55+
56+
//这个标志位是一种优化程序的方法,可以看看我写的冒泡排序优化就会明白了
57+
boolean change = true;
58+
59+
for (int i = 0; i < n.length - 1 && change; i++) {
60+
61+
for (int j = 0; j < j - 1 - i; j++) {
62+
if (n[j] > n[j + 1]) {
63+
return false;
64+
} else {
65+
change = false;
66+
}
67+
}
68+
69+
}
70+
return true;
71+
}
72+
73+
public static void main(String[] args) {
74+
75+
int a[] = {1, 2, 2, 3, 5, 6, 7, 7};
76+
int b[] = {1, 2, 4, 5, 8, 8, 9, 10, 11, 12, 12, 13, 14};
77+
78+
int c[] = mergeList(a, b);
79+
if (c != null) {
80+
for (int t : c) {
81+
System.out.println(t);
82+
}
83+
} else {
84+
System.out.println("");
85+
}
86+
87+
}
88+
89+
90+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.chen.dataStructure;
2+
3+
/**
4+
* http://blog.csdn.net/guyuealian/article/details/51119499(单链表的反转)
5+
* @Author chenweijie
6+
* @Date 2017/10/16 1:37
7+
* @Description
8+
* 基本思路是:将当前节点cur的下一个节点 cur.getNext()缓存到temp后,然后更改当前节点指针指向上一结点pre。
9+
* 也就是说在反转当前结点指针指向前,先把当前结点的指针域用tmp临时保存,以便下一次使用,其过程可表示如下:
10+
pre:上一结点
11+
cur: 当前结点
12+
tmp: 临时结点,用于保存当前结点的指针域(即下一结点)
13+
*/
14+
public class LinkNodeTest {
15+
16+
public static void main(String[] args) {
17+
Node head =new Node(0);
18+
Node node1 = new Node(1);
19+
Node node2 = new Node(2);
20+
Node node3 = new Node(3);
21+
head.setNext(node1);
22+
node1.setNext(node2);
23+
node2.setNext(node3);
24+
25+
//打印反转前的列表
26+
Node h =head;
27+
while (null!=h){
28+
System.out.println(h.getData()+" ");
29+
h =h.getNext();
30+
}
31+
32+
// 调用反转方法
33+
head = reverse(head);
34+
35+
// 打印反转后的结果
36+
while (null != head) {
37+
System.out.print(head.getData() + " ");
38+
head = head.getNext();
39+
}
40+
41+
42+
}
43+
44+
45+
46+
47+
/**
48+
* 遍历,将当前节点的下一个节点缓存后更改当前节点指针
49+
*/
50+
public static Node reverse(Node head){
51+
52+
if (head ==null)
53+
return head;
54+
55+
//上一节点
56+
Node pre =head;
57+
//当前节点
58+
Node cur =head.getNext();
59+
//临时节点 用于保存当前节点的指针域(即下一节点)
60+
Node tmp;
61+
62+
//当前节点为null 说明位于尾节点
63+
while (cur!=null){
64+
tmp =cur.getNext();
65+
//反转指针域的指向
66+
cur.setNext(pre);
67+
//指针向下移动
68+
pre =cur;
69+
cur =tmp;
70+
}
71+
// 最后将原链表的头节点的指针域置为null,还回新链表的头结点,即原链表的尾结点
72+
head.setNext(null);
73+
74+
return pre;
75+
}
76+
77+
78+
79+
80+
81+
82+
83+
84+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.chen.dataStructure;
2+
3+
/**
4+
* @Author chenweijie
5+
* @Date 2017/10/16 1:52
6+
*/
7+
public class Node {
8+
9+
//数据域
10+
private int Data;
11+
//指针域
12+
private Node Next;
13+
14+
public Node(int Data) {
15+
this.Data = Data;
16+
}
17+
18+
public int getData() {
19+
return Data;
20+
}
21+
22+
public void setData(int data) {
23+
Data = data;
24+
}
25+
26+
public Node getNext() {
27+
return Next;
28+
}
29+
30+
public void setNext(Node next) {
31+
Next = next;
32+
}
33+
}

src/main/java/com/chen/designPattern/singleton/Singleton1.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.chen.designPattern.singleton;
22

3-
import java.util.concurrent.atomic.AtomicInteger;
4-
53
/**
64
* 懒汉式单例模式
75
* Created by chenwj3 on 2017/2/28.
@@ -16,8 +14,6 @@ private Singleton1() {
1614

1715
public static Singleton1 getSingleton1() {
1816

19-
AtomicInteger integer =new AtomicInteger();
20-
2117
if (singleton1 == null) {
2218
singleton1 = new Singleton1();
2319
}

0 commit comments

Comments
 (0)