Skip to content

Commit 63bca02

Browse files
authored
Merge pull request onlyliuxin#21 from pwenhua/master
学习一下高手的代码
2 parents d3e3f0a + b7abeb6 commit 63bca02

File tree

10 files changed

+507
-0
lines changed

10 files changed

+507
-0
lines changed

group20/2421586846/.project

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>2421586846</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
</natures>
11+
</projectDescription>

group20/2421586846/DS/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>DS</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package basic;
2+
3+
4+
import java.util.Arrays;
5+
6+
public class ArrayList implements List {
7+
8+
private int size = 0;
9+
10+
private Object[] elementData = new Object[2];
11+
12+
public void EnsureEnoughSize(){
13+
if (size == elementData.length )
14+
{
15+
elementData = Arrays.copyOf(elementData, elementData.length +10);
16+
17+
}
18+
}
19+
20+
public void add(Object o){
21+
EnsureEnoughSize();
22+
23+
// elementData = Arrays.copyOf(elementData, elementData.length +1);
24+
//Object[] NewelementData = new Object[size+1];
25+
//System.arraycopy( elementData,0, NewelementData, 0, elementData.length );
26+
27+
28+
elementData[size]=o;
29+
size++;
30+
}
31+
public void add(int index, Object o){
32+
if (index >= size || index < 0){
33+
return;
34+
}
35+
36+
EnsureEnoughSize();
37+
38+
for (int i = elementData.length-1; i>index;i --){
39+
elementData[i]=elementData[i-1];
40+
}
41+
elementData[index]=o;
42+
size++;
43+
}
44+
45+
public Object get(int index){
46+
if (index >= size || index < 0){
47+
return null;
48+
}
49+
else {
50+
return elementData[index];
51+
}
52+
53+
}
54+
55+
public Object remove(int index){
56+
if (index >= size || index < 0){
57+
return null;
58+
}
59+
else {
60+
Object o = elementData[index];
61+
for (int i =index; i< size-1; i++){
62+
elementData[i]= elementData[i+1];
63+
}
64+
size--;
65+
return o;
66+
}
67+
}
68+
69+
public int size(){
70+
return size;
71+
}
72+
73+
public Iterator iterator(){
74+
return null;
75+
}
76+
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package basic;
2+
3+
4+
public class BinaryTreeNode {
5+
6+
private Object data;
7+
private BinaryTreeNode left;
8+
private BinaryTreeNode right;
9+
10+
public Object getData() {
11+
return data;
12+
}
13+
public void setData(Object data) {
14+
this.data = data;
15+
}
16+
public BinaryTreeNode getLeft() {
17+
return left;
18+
}
19+
public void setLeft(BinaryTreeNode left) {
20+
this.left = left;
21+
}
22+
public BinaryTreeNode getRight() {
23+
return right;
24+
}
25+
public void setRight(BinaryTreeNode right) {
26+
this.right = right;
27+
}
28+
29+
public BinaryTreeNode insert(Object o){
30+
return null;
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package basic;
2+
3+
public interface Iterator {
4+
public boolean hasNext();
5+
public Object next();
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package basic;
2+
3+
4+
public class LinkedList implements List {
5+
6+
private Node head;
7+
8+
private int size;
9+
//加到最后
10+
public void add(Object o){
11+
Node newNode= new Node();
12+
newNode.data = o;
13+
newNode.next = null;
14+
15+
if (head == null ){
16+
head= newNode;
17+
size++;
18+
return;
19+
}
20+
21+
Node currentNode = head;
22+
23+
while (currentNode.next != null ){
24+
currentNode = currentNode.next ;
25+
}
26+
/*
27+
for (int i=0;i< size;i++){
28+
currentNode = currentNode.next ;
29+
}
30+
*/
31+
currentNode.next =newNode;
32+
size++;
33+
}
34+
35+
public void add(int index , Object o){
36+
if (index >= size ||index < 0) {
37+
return;
38+
}
39+
40+
Node newNode= new Node();
41+
newNode.data = o;
42+
Node PrevNode =null;
43+
44+
if (index ==0 ){
45+
46+
Node tempNode = head ;
47+
head = newNode;
48+
newNode.next = tempNode;
49+
50+
}
51+
else {
52+
Node currentNode = head;
53+
for (int i=0; i <index; i++){
54+
PrevNode = currentNode;
55+
currentNode =currentNode.next;
56+
}
57+
58+
PrevNode.next = newNode;
59+
newNode.next = currentNode;
60+
}
61+
size++;
62+
}
63+
64+
public Object get(int index){
65+
66+
if (index >= size ||index < 0) {
67+
return null;
68+
}
69+
Node currentNode = head;
70+
for (int i=0; i <index; i++){
71+
currentNode =currentNode.next;
72+
}
73+
return currentNode.data;
74+
75+
}
76+
public Object remove(int index){
77+
78+
if (index >= size ||index < 0) {
79+
return null;
80+
}
81+
82+
Node currentNode = head;
83+
if (index ==0 ){
84+
head =head.next;
85+
size--;
86+
return currentNode.data;
87+
}
88+
Node PrevNode = null;
89+
for (int i=0; i <index; i++){
90+
PrevNode= currentNode;
91+
currentNode =currentNode.next;
92+
}
93+
PrevNode.next = currentNode.next;
94+
size--;
95+
return currentNode.data;
96+
}
97+
98+
public int size(){
99+
100+
return size;
101+
}
102+
103+
public void addFirst(Object o){
104+
Node newNode = new Node();
105+
newNode.data =o;
106+
newNode.next = head;
107+
108+
head = newNode;
109+
size++;
110+
}
111+
public void addLast(Object o){
112+
if (head == null){
113+
head = new Node();
114+
head.data= o;
115+
size ++;
116+
return;
117+
}
118+
Node currentNode = head;
119+
while (currentNode.next != null){
120+
currentNode=currentNode.next;
121+
122+
}
123+
124+
Node newNode = new Node();
125+
newNode.data= o;
126+
newNode.next =null;
127+
128+
currentNode.next = newNode;
129+
size++;
130+
}
131+
public Object removeFirst(){
132+
133+
size--;
134+
if (head ==null){
135+
return null;
136+
}
137+
else {
138+
Node firstNode = head;
139+
head = firstNode.next;
140+
return firstNode.data ;
141+
}
142+
}
143+
public Object removeLast(){
144+
145+
if (head ==null ){
146+
return null;
147+
}
148+
if (size ==1){
149+
Node tempnode = head;
150+
head = null;
151+
size--;
152+
return tempnode.data;
153+
}
154+
Node currentNode = head.next;
155+
Node PrevNode= head;
156+
157+
while (currentNode.next != null){
158+
PrevNode = currentNode;
159+
currentNode=currentNode.next;
160+
}
161+
PrevNode.next= null;
162+
163+
size--;
164+
return currentNode.data ;
165+
166+
}
167+
public Iterator iterator(){
168+
return null;
169+
}
170+
171+
172+
private static class Node{
173+
Object data;
174+
Node next;
175+
176+
}
177+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package basic;
2+
public interface List {
3+
public void add(Object o);
4+
public void add(int index, Object o);
5+
public Object get(int index);
6+
public Object remove(int index);
7+
public int size();
8+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package basic;
2+
3+
public class Queue {
4+
private LinkedList elementData = new LinkedList();
5+
6+
public void enQueue(Object o){
7+
elementData.addLast(o);
8+
}
9+
10+
11+
public Object deQueue(){
12+
return elementData.removeFirst();
13+
}
14+
15+
public boolean isEmpty(){
16+
if (elementData.size()==0){
17+
18+
return true;
19+
}
20+
else {
21+
return false;
22+
}
23+
}
24+
25+
public int size(){
26+
return elementData.size();
27+
}
28+
}

0 commit comments

Comments
 (0)