Skip to content

Commit 7f25493

Browse files
authored
Merge pull request onlyliuxin#39 from diliuzuzhanghao/master
第六组修改后请求尝试合并
2 parents 4c4dee8 + c1511ba commit 7f25493

File tree

174 files changed

+8159
-119
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

174 files changed

+8159
-119
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.*;
2+
3+
/**
4+
* @author CCD
5+
*
6+
*/
7+
public class MyArrayList {
8+
9+
private Object[] elementData = new Object[100];
10+
private int size = 100 ;
11+
12+
public void add(Object o){
13+
elementData[size++] = o;
14+
}
15+
public void add(int index, Object o){
16+
if(index > size || index < 0)
17+
throw new IndexOutOfBoundsException("index "+ index +"is biger than size" + size+
18+
"index is less than 0");
19+
System.arraycopy(elementData, index, elementData, index+1, size-index);
20+
elementData[index] = o;
21+
size++;
22+
}
23+
24+
public Object get(int index){
25+
if(index > size || index < 0)
26+
throw new IndexOutOfBoundsException("index "+ index +"is biger than size" + size+
27+
"index is less than 0");
28+
return elementData[index];
29+
}
30+
31+
public Object remove(int index){
32+
if(index > size || index < 0)
33+
throw new IndexOutOfBoundsException("index "+ index +"is biger than size" + size+
34+
"index is less than 0");
35+
Object E = elementData[index];
36+
System.arraycopy(elementData, index+1, elementData, index,
37+
size - index - 1);
38+
elementData[--size] = null;
39+
return E;
40+
}
41+
42+
public int size(){
43+
return size;
44+
}
45+
46+
public Iterator iterator(){
47+
return null;
48+
}
49+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import java.util.*;
2+
3+
public class MyLinkedList implements List {
4+
5+
private Node first;
6+
private Node last;
7+
private int size = 0 ;
8+
public void Myadd(Object o){
9+
final Node l = last;
10+
final Node newNode = new Node(l,o,null);
11+
last = newNode;
12+
if(l == null)
13+
first = newNode;
14+
else
15+
l.next = newNode;
16+
size++;
17+
}
18+
public void Myadd(int index , Object o){
19+
checkPosition(index);
20+
if(index == size){
21+
Myadd(o);
22+
}
23+
else{
24+
final Node PreNode =GetNodeByIndex(index).prev;
25+
final Node newNode = new Node(PreNode,o,GetNodeByIndex(index));
26+
PreNode.next =newNode;
27+
if(PreNode == null)
28+
first = newNode; //ΪʲôҪ¸¶¸øÊ×Ö¸Õ룿
29+
else
30+
PreNode.next = newNode;
31+
size++;
32+
}
33+
}
34+
public Object get(int index){
35+
checkPosition(index);
36+
return GetNodeByIndex(index);
37+
}
38+
public void remove(int index){
39+
Node node = GetNodeByIndex(index);
40+
node.prev.next = node.next;
41+
node.next.prev = node.prev;
42+
node = null;
43+
size--;
44+
}
45+
46+
public int size(){
47+
return size;
48+
}
49+
50+
public void addFirst(Object o){
51+
final Node FirstNode= first;
52+
final Node newNode = new Node(null,o,first);
53+
first = newNode;
54+
if(FirstNode == null)
55+
last = newNode;
56+
else
57+
first.prev = newNode;
58+
size++;
59+
60+
}
61+
public void addLast(Object o){
62+
final Node LastNode = last;
63+
final Node newNode = new Node(last,o,null);
64+
last = newNode;
65+
if(last == null)
66+
first = newNode;
67+
else
68+
last.next = newNode;
69+
size++;
70+
}
71+
public void removeFirst(){
72+
final Node f = first;
73+
if(f == null)
74+
throw new NoSuchElementException();
75+
first = f.next;
76+
first = null;
77+
size--;
78+
}
79+
public void removeLast(){
80+
final Node f = last;
81+
if(f == null)
82+
throw new NoSuchElementException();
83+
last = last.prev;
84+
last = null;
85+
size--;
86+
}
87+
public Iterator iterator(){
88+
return null;
89+
}
90+
91+
92+
private static class Node{
93+
Object item;
94+
Node next;
95+
Node prev;
96+
Node (Node prev,Object element ,Node next){
97+
this.item = element;
98+
this.next = next;
99+
this.prev = prev;
100+
}
101+
}
102+
103+
private Node GetNodeByIndex(int index){
104+
if(index > size/2)
105+
{
106+
Node Temp = first;
107+
for(int i = 0; i< index;i++)
108+
Temp = Temp.next; //
109+
return Temp;
110+
}
111+
else
112+
{
113+
Node Temp = last;
114+
for(int i = size-1; i> index; i--)
115+
Temp = Temp.prev;
116+
return Temp;
117+
}
118+
}
119+
120+
private void checkPosition(int index){
121+
if(index < 0 || index > size)
122+
throw new IndexOutOfBoundsException("index:"+ index+"is llegal");
123+
}
124+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
/**
3+
* @author CCD
4+
*
5+
*/
6+
7+
import java.util.*;
8+
9+
public class MyQueue {
10+
11+
private static final int DEFAULT_SIZE = 10;
12+
13+
private Object[] elementData;
14+
private int head;
15+
private int tail;
16+
public MyQueue(){
17+
this(DEFAULT_SIZE);
18+
}
19+
public MyQueue(int size){
20+
this.elementData = new Object[size];
21+
this.head = 0;
22+
this.tail = 0;
23+
}
24+
25+
public void enQueue(Object o){
26+
if((tail+1)%elementData.length == head){
27+
}
28+
else{
29+
elementData[tail] = o;
30+
tail = (tail+1)%elementData.length;
31+
}
32+
}
33+
34+
public Object deQueue(){
35+
if(head == tail){
36+
return null;
37+
}
38+
else{
39+
Object o = elementData[head];
40+
head = (head+1)% elementData.length;
41+
return o ;
42+
}
43+
}
44+
45+
public boolean isEmpty(){
46+
return head == tail ;
47+
}
48+
49+
public int size(){
50+
return (tail-head)&(elementData.length -1);
51+
}
52+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.*;
2+
3+
/**
4+
*
5+
*/
6+
7+
/**
8+
* @author CCD
9+
*
10+
*/
11+
public class MyStack {
12+
13+
private ArrayList elementData = new ArrayList();
14+
private Object[] Myelement = elementData.toArray();
15+
private int Length = elementData.size();
16+
17+
public void push(Object E){
18+
Myelement[++Length] = E ;
19+
}
20+
21+
public Object pop(){
22+
int NowLength = size()-1;
23+
Object obj = peek();
24+
Length--;
25+
Myelement[Length] = null ;
26+
return obj;
27+
}
28+
29+
public Object peek(){
30+
int NowLength = size();
31+
if(NowLength == 0)
32+
throw new EmptyStackException();
33+
NowLength -= 1 ;
34+
if(NowLength >= Length )
35+
throw new ArrayIndexOutOfBoundsException(NowLength + " >= " + Length);
36+
return Myelement[NowLength];
37+
38+
}
39+
public boolean isEmpty(){
40+
return size() == 0;
41+
}
42+
public int size(){
43+
return Length;
44+
}
45+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>
File renamed without changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>JavaLearning</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>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.6
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.6
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.coding.basic;
2+
3+
/**
4+
* @deprecated 用数组实现list
5+
* @author wang
6+
*
7+
*/
8+
public class MyArrayList implements List {
9+
10+
private int size = 0;
11+
12+
private Object[] elementData = new Object[100];
13+
14+
public void add(Object o){
15+
// 确保数组大小
16+
ensureCapacity(size + 1);
17+
// 数组赋值并使得size+1;
18+
elementData[size ++] = o;
19+
size ++;
20+
}
21+
22+
/**
23+
* 确保数组不越界,否则就扩容;
24+
* @param minCapacity list的大小;
25+
*/
26+
public void ensureCapacity(int minCapacity) {
27+
int oldCapacity = elementData.length;
28+
int newCapacity = (oldCapacity / 3) * 2 + 1;
29+
if (minCapacity > newCapacity) {
30+
// 对数组扩容
31+
Object[] newDate = new Object[newCapacity];
32+
System.arraycopy(elementData, 0, newDate, 0, oldCapacity);
33+
elementData = newDate;
34+
}
35+
}
36+
37+
public void add(int index, Object o){
38+
// 对index进行校验:
39+
rangeCheck(index);
40+
ensureCapacity(size + 1);
41+
System.arraycopy(elementData, index, elementData, index + 1, size - index);
42+
elementData[index] = o;
43+
size ++;
44+
}
45+
46+
/**
47+
* 边界检查:
48+
* @param index
49+
*/
50+
private void rangeCheck(int index) {
51+
if (index < 0 || index >= size) {
52+
throw new IndexOutOfBoundsException("size" + size + ", index:"
53+
+ index);
54+
}
55+
}
56+
57+
public Object get(int index){
58+
rangeCheck(index);
59+
return elementData[index];
60+
}
61+
62+
public Object remove(int index){
63+
rangeCheck(index);
64+
Object oldValue = elementData[index];
65+
System.arraycopy(elementData, index, elementData, index - 1, size - index);
66+
return oldValue;
67+
}
68+
69+
public int size(){
70+
return size;
71+
}
72+
73+
public Iterator iterator(){
74+
return null;
75+
}
76+
77+
}

0 commit comments

Comments
 (0)