Skip to content

Commit 0e84a06

Browse files
authored
Merge pull request onlyliuxin#4 from ghairui/master
work
2 parents 4d1652e + f22d1c4 commit 0e84a06

File tree

10 files changed

+475
-0
lines changed

10 files changed

+475
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>assignment</artifactId>
7+
<groupId>assignment</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>assignment-0215-0226</artifactId>
13+
14+
15+
</project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import java.io.IOException;
2+
import java.util.Iterator;
3+
import java.util.List;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: guohairui
8+
* Date: 17-2-22
9+
* Time: 上午12:06
10+
* To change this template use File | Settings | File Templates.
11+
*/
12+
public class MyArrayList {
13+
public int size = 0;
14+
private Object [] elementData = new Object[5];
15+
public void add(int index,Object obj){
16+
if(index>size() ||index<0)
17+
throw new IndexOutOfBoundsException("哎呀我去,不够了.");
18+
elementData[index]=obj;
19+
size++;
20+
}
21+
public void insert(int index,Object obj){
22+
if(size>elementData.length-1){
23+
System.out.println("当前size:" + size + " 当前length:" + elementData.length+",再插不够了,需要扩容");
24+
Object [] tmpData = elementData;
25+
elementData =new Object[size+5] ;
26+
System.out.println("当前size:" + size + " 当前length扩了5后为:" + elementData.length);
27+
System.arraycopy(tmpData,0,elementData,0,index);
28+
elementData[index]=obj;
29+
System.arraycopy(tmpData,index,elementData,index+1,tmpData.length-index);
30+
}else {
31+
if(elementData[index]==null){
32+
elementData[index]=obj;
33+
}else {
34+
System.out.println("当前size:" + size + " 当前length:" + elementData.length);
35+
System.arraycopy(elementData,index,elementData,index+1,size-index);
36+
elementData[index]=obj;
37+
}
38+
}
39+
size++;
40+
}
41+
public void add(Object obj){
42+
if(size>=elementData.length){
43+
System.out.println("当前size:" + size + " 当前length:" + elementData.length);
44+
Object [] tmpData = elementData;
45+
elementData =new Object[size+5] ;
46+
System.out.println("当前size:" + size + " 当前length扩了5后为:" + elementData.length);
47+
System.arraycopy(tmpData,0,elementData,0,size);
48+
elementData[size]=obj;
49+
}else {
50+
System.out.println("当前size:" + size + " 当前length:" + elementData.length);
51+
elementData[size]=obj;
52+
}
53+
size++;
54+
}
55+
public Object get(int index) {
56+
if(index>=size)
57+
throw new IndexOutOfBoundsException("越了");
58+
return elementData[index];
59+
}
60+
public Object remove(int index){
61+
Object delValue = elementData[index];
62+
int movesize = size-index-1;
63+
System.out.print("size:"+size+" index:"+index+" ,size-index-1:"+movesize);
64+
System.arraycopy(elementData,index+1,elementData,index,movesize);
65+
System.out.print("删除后前移位,数组末位清空");
66+
elementData[--size]=null;
67+
68+
return delValue;
69+
}
70+
public int size(){
71+
return size;
72+
}
73+
public String toString(){
74+
StringBuilder sb = new StringBuilder();
75+
sb.append('[');
76+
for (int i=0;i<size;i++) {
77+
Object o = elementData[i];
78+
sb.append(elementData[i]);
79+
if(i<size-1)
80+
sb.append(",");
81+
}
82+
return sb.append(']').toString();
83+
}
84+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Created with IntelliJ IDEA.
3+
* User: guohairui
4+
* Date: 17-2-26
5+
* Time: 下午3:21
6+
* To change this template use File | Settings | File Templates.
7+
*/
8+
public class MyLinkedList {
9+
private Node head=new Node(null,null,null);
10+
public int size = 0;
11+
12+
public MyLinkedList(){
13+
head.next=head.previous=head;
14+
}
15+
public void add(Object o){
16+
if(null==head){
17+
head = new Node(null,null,null);
18+
}
19+
Node newNode = new Node(o,head,head.previous); //放在最后
20+
newNode.previous.next=newNode;
21+
newNode.next.previous=newNode;
22+
size++;
23+
}
24+
public void add(int index,Object o ) {
25+
Node newNode = new Node(o,getNode(index),getNode(index).previous); //放在最后
26+
newNode.previous.next=newNode;
27+
newNode.next.previous=newNode;
28+
size++;
29+
}
30+
public Object get(int index){
31+
return getNode(index).data;
32+
}
33+
public Object remove(int index){
34+
remove(getNode(index));
35+
return null;
36+
}
37+
public void addFirst(Object o){
38+
39+
}
40+
public void removeFirst(){
41+
remove(head.next);
42+
}
43+
public void removeLast(){
44+
remove(head.previous);
45+
}
46+
public void remove(Node node){
47+
node.previous.next=node.next;
48+
node.next.previous=node.previous;
49+
size--;
50+
}
51+
52+
53+
public int size(){
54+
return size();
55+
}
56+
public Object[] toArray(){
57+
return null;
58+
}
59+
60+
61+
public Node getNode(int index){
62+
if(index<0||index>=size)
63+
throw new RuntimeException("超出范围了");
64+
Node node = head;
65+
if(index<(size>>1)){//当偏向于前一半时从头找,否则从尾找
66+
for ( int i=0;i<=index;i++) {
67+
node = node.next;
68+
}
69+
}else {
70+
for (int i=size;i>index;i--){
71+
node=node.previous;
72+
}
73+
}
74+
return node;
75+
}
76+
77+
private static class Node{
78+
Object data;//当前Entry
79+
Node next;//下一个
80+
Node previous;//前一个
81+
public Node(Object data,Node next,Node previous){
82+
this.data=data;
83+
this.next=next;
84+
this.previous=previous;
85+
}
86+
}
87+
public String toString(){
88+
StringBuilder sb = new StringBuilder();
89+
sb.append('[');
90+
Node n =getNode(0) ;
91+
for (int i=0;i<size;i++) {
92+
sb.append(n.data);
93+
if(i<size-1)
94+
sb.append(",");
95+
n=n.next;
96+
}
97+
return sb.append(']').toString();
98+
}
99+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 先进先出
3+
*/
4+
public class MyQueue {
5+
private MyLinkedList myLinkedList = new MyLinkedList();
6+
public void enQueue(Object o){
7+
myLinkedList.add(o);
8+
}
9+
public void deQueue(){
10+
myLinkedList.remove(0) ;
11+
}
12+
public boolean isEmpty(){
13+
return myLinkedList.size<1;
14+
}
15+
public int size(){
16+
return myLinkedList.size;
17+
}
18+
public String toString(){
19+
return myLinkedList.toString();
20+
}
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 先进后出
3+
*/
4+
public class MyStack {
5+
private MyArrayList myArrayList = new MyArrayList();
6+
public MyStack(){}
7+
public void push(Object o) {
8+
myArrayList.add(o);
9+
}
10+
public Object pop(){
11+
Object popo=myArrayList.get(myArrayList.size-1);
12+
myArrayList.remove(myArrayList.size-1);
13+
return popo;
14+
}
15+
public Object peek(){
16+
17+
return myArrayList.get(myArrayList.size-1);
18+
19+
}
20+
public boolean isEmpty(){
21+
22+
return myArrayList.size<1;
23+
}
24+
public int size(){
25+
return myArrayList.size;
26+
}
27+
public String toString(){
28+
return myArrayList.toString();
29+
}
30+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import org.junit.*;
2+
import org.junit.Test;
3+
4+
/**
5+
* Created with IntelliJ IDEA.
6+
* User: guohairui
7+
* Date: 17-2-22
8+
* Time: 上午12:24
9+
* To change this template use File | Settings | File Templates.
10+
*/
11+
public class MyArrayListTest {
12+
13+
@Test
14+
public void testAdd() throws Exception {
15+
MyArrayList arrayList = new MyArrayList();
16+
arrayList.add("1");
17+
arrayList.add("2");
18+
arrayList.add("3");
19+
arrayList.add("4");
20+
arrayList.add("5");
21+
arrayList.add("6");
22+
arrayList.add(7);
23+
arrayList.add(8);
24+
arrayList.add(9);
25+
arrayList.add(10);
26+
arrayList.add(true);
27+
System.out.println(String.valueOf(arrayList.get(2)));
28+
System.out.println(arrayList.toString());
29+
arrayList.remove(2);
30+
System.out.println(arrayList.toString());
31+
32+
}
33+
34+
@Test
35+
public void testGet() throws Exception {
36+
MyArrayList arrayList = new MyArrayList();
37+
//arrayList.add(6,"1");
38+
arrayList.add(5,"2");
39+
arrayList.add(4,"3");
40+
arrayList.add(3,"4");
41+
arrayList.add(2,"5");
42+
arrayList.add(1,"6");
43+
//arrayList.add(0,"7");
44+
System.out.println(arrayList.toString());
45+
System.out.println(arrayList.get(4));
46+
arrayList.remove(2);
47+
System.out.println(arrayList.toString());
48+
49+
}
50+
@Test
51+
public void testInsert() throws Exception {
52+
MyArrayList arrayList = new MyArrayList();
53+
arrayList.add("1");
54+
arrayList.add("2");
55+
arrayList.add("3");
56+
arrayList.add("4");
57+
arrayList.add("5");
58+
arrayList.add("6");
59+
System.out.println(arrayList.toString());
60+
System.out.println(arrayList.get(4));
61+
arrayList.insert(4,"ghr");
62+
System.out.println(arrayList.get(4));
63+
System.out.println(arrayList.toString());
64+
}
65+
66+
@Test
67+
public void testRemove() {
68+
throw new RuntimeException();
69+
}
70+
71+
@Test
72+
public void testSize() throws Exception {
73+
System.out.println(16>>2);
74+
}
75+
}

0 commit comments

Comments
 (0)