Skip to content

Commit b70ddf8

Browse files
authored
Merge pull request onlyliuxin#12 from yys213114/master
提交作业
2 parents a85c46f + 8bbd619 commit b70ddf8

14 files changed

+302
-119
lines changed

.project

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>coding2017</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
</natures>
11+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8

group20/423184723/Test/博客地址

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
http://www.cnblogs.com/yyssyh213/p/6442285.html
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.coding.basic;
2+
3+
import com.sun.media.sound.EmergencySoundbank;
4+
5+
public class ArrayList implements List {
6+
/**
7+
* 列表中元素的个数
8+
*/
9+
private int size = 0;
10+
11+
/**
12+
* 初始化数组大小
13+
*/
14+
private int arraySize = 100;
15+
/**
16+
* 初始化数组
17+
*/
18+
private Object[] elementData = new Object[arraySize];
19+
20+
/**
21+
* 添加方法
22+
*/
23+
public void add(Object o){
24+
if(size>=(arraySize*0.75)){
25+
Object [] target = new Object[(int) (arraySize*1.5)];
26+
System.arraycopy(elementData,0,target,0,arraySize);
27+
target[size-1] = o;
28+
size++;
29+
}else if(size<(arraySize*0.75)){
30+
elementData[size-1]=o;
31+
size++;
32+
}
33+
}
34+
/**
35+
* 根据索引添加方法
36+
*/
37+
public void add(int index, Object o){
38+
if(size >= arraySize*0.75){
39+
Object [] target = new Object[(int) (arraySize*1.5)];
40+
System.arraycopy(elementData,0,target,0,arraySize);
41+
for (int j = target.length;j>=index;j--){
42+
target[j-1] = target[j-2];
43+
}
44+
target[index] = o;
45+
size++;
46+
}else if(size < arraySize*0.75){
47+
for (int j = elementData.length;j>=index;j--){
48+
elementData[j-1] = elementData[j-2];
49+
}
50+
elementData[index] = o;
51+
size++;
52+
}
53+
}
54+
/**
55+
* 根据索引获取对象
56+
*/
57+
public Object get(int index){
58+
return elementData[index];
59+
}
60+
/**
61+
* 根据索引移除对象
62+
*/
63+
public Object remove(int index){
64+
for (int i = index; i < elementData.length; i++) {
65+
elementData[i]=elementData[i+1];
66+
size++;
67+
}
68+
return elementData[index];
69+
}
70+
/**
71+
* 获取数组大小
72+
*/
73+
public int size(){
74+
return this.size;
75+
}
76+
77+
78+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.coding.basic;
2+
3+
import com.coding.basic.LinkedList.Node;
4+
5+
public class LinkedList implements List {
6+
7+
private Node head;
8+
private int size;
9+
10+
public void add(Object o){
11+
if (head.data == null) {
12+
head.data = o;
13+
head.next = null;
14+
size++;
15+
return;
16+
}
17+
Node node = new Node(o);
18+
Node curr = head;
19+
while (curr.next != null) {
20+
curr = curr.next;
21+
}
22+
curr.next = node;
23+
size++;
24+
}
25+
public void add(int index , Object o){
26+
if (index < 0 || index > size) {
27+
System.out.println(index + "无效指数");
28+
return;
29+
}
30+
if (head.data == null) {
31+
if (index == 0) {
32+
head.data = o;
33+
head.next = null;
34+
size++;
35+
return;
36+
} else {
37+
System.out.println("无效指数");
38+
return;
39+
}
40+
}
41+
Node node = new Node(o);
42+
Node curr = head;
43+
for (int i = 0; i < index - 1; i++) {
44+
curr = curr.next;
45+
}
46+
Node temp = curr.next;
47+
curr.next = node;
48+
node.next = temp;
49+
size++;
50+
}
51+
public Object get(int index){
52+
if (index < 0 || index > size) {
53+
System.out.println(index + " is invalid index!");
54+
return null;
55+
}
56+
Node result = head;
57+
for (int i = 0; i < index; i++) {
58+
result = result.next;
59+
}
60+
return result;
61+
}
62+
public Object remove(int index){
63+
if (index < 0 || index > size) {
64+
System.out.println(index + " is invalid index!");
65+
return null;
66+
}
67+
Node curr = head;
68+
for (int i = 0; i < index - 1; i++) {
69+
curr = curr.next;
70+
}
71+
Node result = curr.next;
72+
curr.next = curr.next.next;
73+
size--;
74+
return result;
75+
}
76+
77+
public int size(){
78+
return this.size;
79+
}
80+
81+
public void addFirst(Object o){
82+
if (head.data == null) {
83+
head.data = o;
84+
head.next = null;
85+
size++;
86+
return;
87+
}
88+
Node temp = head;
89+
head = new Node(o);
90+
head.next = temp;
91+
size++;
92+
}
93+
public void addLast(Object o){
94+
if (head.data == null) {
95+
head.data = o;
96+
head.next = null;
97+
size++;
98+
return;
99+
}
100+
Node node = new Node(o);
101+
Node curr = head;
102+
while (curr.next != null) {
103+
curr = curr.next;
104+
}
105+
curr.next = node;
106+
size++;
107+
}
108+
public Object removeFirst(){
109+
if (head.data == null) {
110+
return null;
111+
}
112+
Node result = head;
113+
head = head.next;
114+
size--;
115+
return result;
116+
}
117+
public Object removeLast(){
118+
if (head.data == null) {
119+
return null;
120+
}
121+
Node curr = head;
122+
for (int i = 0; i < size - 1; i++) {
123+
curr = curr.next;
124+
}
125+
Node result = curr.next;
126+
curr.next = null;
127+
size--;
128+
return result;
129+
}
130+
public Iterator iterator(){
131+
return null;
132+
}
133+
134+
135+
private static class Node{
136+
Object data;
137+
Node next;
138+
public Node(Object o) {
139+
data = o;
140+
next = null;
141+
}
142+
143+
144+
145+
}
146+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.coding.basic;
2+
3+
public class Queue {
4+
5+
private LinkedList list = new LinkedList();
6+
public void enQueue(Object o){
7+
list.add(o);
8+
}
9+
10+
public Object deQueue(){
11+
int length = list.size();
12+
if (length == 0) {
13+
return null;
14+
}
15+
return list.removeFirst();
16+
}
17+
18+
public boolean isEmpty(){
19+
if (list.size() == 0) {
20+
return true;
21+
} else {
22+
return false;
23+
}
24+
}
25+
26+
public int size(){
27+
return list.size;
28+
}
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.coding.basic;
2+
3+
public class Stack {
4+
private ArrayList elementData = new ArrayList();
5+
6+
public void push(Object o){
7+
elementData.add(o);
8+
}
9+
10+
public Object pop(){
11+
int length = elementData.size();
12+
if (length == 0) {
13+
return null;
14+
}
15+
return elementData.remove(length - 1);
16+
}
17+
18+
public Object peek(){
19+
int length = elementData.size();
20+
if (length == 0) {
21+
return null;
22+
}
23+
return elementData.get(length - 1);
24+
}
25+
public boolean isEmpty(){
26+
if (elementData.size() != 0) {
27+
return false;
28+
} else {
29+
return true;
30+
}
31+
}
32+
public int size(){
33+
return elementData.size();
34+
}
35+
}

group20/423184723/src/com/coding/basic/ArrayList.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

group20/423184723/src/com/coding/basic/LinkedList.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)