Skip to content

Commit 9c69823

Browse files
author
chenweijie
committed
数据结构和算法-栈
1 parent 7d9a891 commit 9c69823

File tree

5 files changed

+361
-0
lines changed

5 files changed

+361
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.chen.algorithm.sort;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2019-03-01 12:35 AM
6+
*/
7+
public class InsertSort {
8+
9+
10+
public static int[] sort(int[] array) {
11+
12+
int j;
13+
//从下标为1的元素开始选择合适的位置插入,因为下标为0的只有一个元素,默认是有序的
14+
for (int i = 1; i < array.length; i++) {
15+
//记录要插入的数据
16+
int temp = array[i];
17+
j = i;
18+
//从已经排序的序列最右边的开始比较,找到比其小的数
19+
while (j > 0 && temp < array[j - 1]) {
20+
//向后挪动
21+
array[j] = array[j - 1];
22+
j--;
23+
}
24+
//存在比其小的数,插入
25+
array[j] = temp;
26+
}
27+
return array;
28+
}
29+
30+
//遍历显示数组
31+
public static void display(int[] array) {
32+
for (int i = 0; i < array.length; i++) {
33+
System.out.print(array[i] + " ");
34+
}
35+
System.out.println();
36+
}
37+
38+
public static void main(String[] args) {
39+
int[] array = {4, 2, 8, 9, 5, 7, 6, 1, 3};
40+
//未排序数组顺序为
41+
System.out.println("未排序数组顺序为:");
42+
display(array);
43+
System.out.println("-----------------------");
44+
array = sort(array);
45+
System.out.println("-----------------------");
46+
System.out.println("经过插入排序后的数组顺序为:");
47+
display(array);
48+
}
49+
50+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.chen.dataStructure.stack;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author : chen weijie
7+
* @Date: 2019-03-06 12:31 AM
8+
*/
9+
public class ArrayStack {
10+
11+
12+
/**
13+
* object类型的数组可以存储任意类型
14+
*/
15+
private Object[] elementData;
16+
17+
/**
18+
* 指向栈顶的指针
19+
*/
20+
private int top;
21+
22+
/**
23+
* 栈的容量
24+
*/
25+
private int size;
26+
27+
28+
public ArrayStack() {
29+
30+
this.elementData = new Object[10];
31+
this.top = -1;
32+
this.size = 10;
33+
}
34+
35+
public ArrayStack(int initialCapacity) {
36+
37+
if (initialCapacity < 0) {
38+
throw new IllegalArgumentException("栈的初始容量不得小于0:" + initialCapacity);
39+
}
40+
this.elementData = new Object[initialCapacity];
41+
this.top = -1;
42+
this.size = initialCapacity;
43+
}
44+
45+
46+
public Object push(Object value) {
47+
isGrow(top + 1);
48+
elementData[++top] = value;
49+
return value;
50+
}
51+
52+
53+
public void remove(int top) {
54+
55+
//栈顶元素置为null
56+
elementData[top] = null;
57+
this.top--;
58+
}
59+
60+
61+
public boolean isEmpty() {
62+
return top == -1;
63+
}
64+
65+
66+
public Object peek() {
67+
68+
if (top == -1) {
69+
return null;
70+
}
71+
return elementData[top];
72+
73+
}
74+
75+
76+
public Object pop() {
77+
Object object = peek();
78+
remove(top);
79+
return object;
80+
}
81+
82+
83+
public boolean isGrow(int minCapacity) {
84+
85+
int oldCapacity = size;
86+
//如果当前元素压入栈之后总容量大于前面定义的容量,则需要扩容
87+
if (oldCapacity <= minCapacity) {
88+
//定义扩大之后栈的总容量
89+
int newCapacity = 0;
90+
if ((oldCapacity << 1) - Integer.MAX_VALUE > 0) {
91+
newCapacity = Integer.MAX_VALUE;
92+
} else {
93+
newCapacity = (oldCapacity << 1);//左移一位,相当于*2
94+
}
95+
this.size = newCapacity;
96+
int[] newArray = new int[size];
97+
elementData = Arrays.copyOf(elementData, size);
98+
return true;
99+
} else {
100+
return false;
101+
}
102+
}
103+
104+
public static void main(String[] args) {
105+
106+
ArrayStack stack = new ArrayStack(3);
107+
stack.push(1);
108+
//System.out.println(stack.peek());
109+
stack.push(2);
110+
stack.push(3);
111+
stack.push("abc");
112+
System.out.println(stack.peek());
113+
stack.pop();
114+
stack.pop();
115+
stack.pop();
116+
System.out.println(stack.peek());
117+
118+
}
119+
120+
121+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.chen.dataStructure.stack;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2019-03-05 11:47 PM
6+
*/
7+
public class MyStack {
8+
9+
10+
/**
11+
* 栈的实际元素
12+
*/
13+
private int[] array;
14+
15+
/**
16+
* 指向栈顶的指针
17+
*/
18+
private int top;
19+
20+
/**
21+
* 栈的容量
22+
*/
23+
private int maxSize;
24+
25+
26+
public MyStack(int size) {
27+
this.maxSize = size;
28+
array = new int[size];
29+
top = -1;
30+
}
31+
32+
33+
/**
34+
* 插入元素
35+
*
36+
* @param value
37+
*/
38+
public void push(int value) {
39+
if (top < maxSize - 1) {
40+
array[++top] = value;
41+
}
42+
}
43+
44+
45+
/**
46+
* 弹出元素
47+
*
48+
* @return
49+
*/
50+
public int pop() {
51+
return array[top--];
52+
}
53+
54+
55+
/**
56+
* 访问栈顶元素
57+
*
58+
* @return
59+
*/
60+
public int peep() {
61+
return array[top];
62+
}
63+
64+
65+
/**
66+
* 栈是否是空
67+
*
68+
* @return
69+
*/
70+
public boolean isEmpty() {
71+
return (top == -1);
72+
}
73+
74+
75+
/**
76+
* @return
77+
*/
78+
public boolean isFull() {
79+
return (maxSize - 1) == top;
80+
}
81+
82+
83+
public static void main(String[] args) {
84+
85+
MyStack myStack = new MyStack(4);
86+
87+
myStack.push(10);
88+
myStack.push(20);
89+
myStack.push(1);
90+
myStack.push(-1);
91+
92+
System.out.println("isFull:" + myStack.isFull());
93+
System.out.println("peek:" + myStack.peep());
94+
95+
while (!myStack.isEmpty()) {
96+
System.out.println("弹出:" + myStack.pop());
97+
System.out.println("栈内元素个数:" + myStack.top);
98+
}
99+
}
100+
101+
102+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.chen.dataStructure.stack;
2+
3+
/**
4+
* 利用栈实现字符串逆序
5+
*
6+
* @author : chen weijie
7+
* @Date: 2019-03-06 12:59 AM
8+
*/
9+
public class RevertString {
10+
11+
12+
public static void main(String[] args) {
13+
14+
15+
String str = "how are you ";
16+
char[] charArray = str.toCharArray();
17+
ArrayStack myStack = new ArrayStack();
18+
for (char c : charArray) {
19+
myStack.push(c);
20+
}
21+
22+
while (!myStack.isEmpty()) {
23+
System.out.print(myStack.pop());
24+
}
25+
26+
}
27+
28+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.chen.dataStructure.stack;
2+
3+
/**
4+
* 利用栈判断分隔符是否匹配
5+
* 比如:<abc[123]abc>这是符号相匹配的,如果是 <abc[123>abc] 那就是不匹配的。
6+
* <p>
7+
*  对于 12<a[b{c}]>,我们分析在栈中的数据:遇到匹配正确的就消除
8+
*
9+
* @author : chen weijie
10+
* @Date: 2019-03-06 1:04 AM
11+
*/
12+
public class TestMatch {
13+
14+
15+
//遇到左边分隔符了就push进栈,遇到右边分隔符了就pop出栈,看出栈的分隔符是否和这个有分隔符匹配
16+
17+
18+
public static void main(String[] args) {
19+
20+
ArrayStack arrayStack = new ArrayStack(3);
21+
22+
String str = "12<a[b{c}]>";
23+
24+
char[] chars = str.toCharArray();
25+
26+
for (char c : chars) {
27+
28+
switch (c) {
29+
30+
case '{':
31+
case '[':
32+
case '<':
33+
arrayStack.push(c);
34+
break;
35+
case '}':
36+
case ']':
37+
case '>':
38+
if (!arrayStack.isEmpty()) {
39+
char ch = arrayStack.pop().toString().toCharArray()[0];
40+
if (c == '}' && ch != '{' || c == ']' && ch != '[' ||
41+
c == '>' && ch != '<') {
42+
System.out.println("Error:" + ch + "-" + c);
43+
44+
}
45+
}
46+
break;
47+
default:
48+
break;
49+
50+
51+
}
52+
53+
54+
}
55+
56+
57+
}
58+
59+
60+
}

0 commit comments

Comments
 (0)