Skip to content

Commit 2a8309a

Browse files
authored
Merge pull request onlyliuxin#8 from drunbility/master
20组组员浮云提交作业
2 parents c13357c + e8ba36e commit 2a8309a

File tree

11 files changed

+429
-0
lines changed

11 files changed

+429
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
对计算机硬件的理解
2+
计算机中cpu是核心部件,cpu是主要的计算部件,所有的逻辑运算和算术运算都是由cpu来玩成的。但cpu只管运算,怎么运算,何时运算,运算什么这些cpu都是不管的,这些其实就是一个个的指令,指令用来表示所有运算的信息,包括做什么运算,运算什么,运算结果如何处置等。这些指令都是存在内存当中的,cpu按照一定的顺序从内存当中读取指令,执行指令做运算,然后按照相应的指令输出结果。这就是一个简单的程序运行过程。内存空间是有限的,另外,内存上的指令也不能持久化的保存,断电之后就没有了。所以需要一个硬件保存大的二进制资源以及持久化的保存指令,所以就有了硬盘。所以计算机先从硬盘中读取指令和资源到内存当中来,然后cpu再从内存当中读取指令和资源,做运算,运算完之后再按照指令将输出保存在内存中或者硬盘中,程序执行完毕。
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package liuxincourse;
2+
3+
import java.util.Arrays;
4+
5+
6+
public class ArrayList implements List{
7+
8+
private int size=0;
9+
10+
private Object [] elementDataObjects = new Object[3];
11+
12+
public void add (Object o){
13+
if (size>=elementDataObjects.length) {
14+
elementDataObjects=Arrays.copyOf(elementDataObjects, elementDataObjects.length+50);
15+
}
16+
elementDataObjects[size]=o;
17+
size++;
18+
}
19+
20+
public void add (int index ,Object o){
21+
if (index>=size||index<0) {
22+
throw new IndexOutOfBoundsException();
23+
}
24+
if (size>=elementDataObjects.length) {
25+
elementDataObjects=Arrays.copyOf(elementDataObjects, elementDataObjects.length+50);
26+
}
27+
System.arraycopy(elementDataObjects, index, elementDataObjects, index+1, size-index);
28+
elementDataObjects[index]=o;
29+
size++;
30+
}
31+
32+
public Object get (int index){
33+
if (index>=size||index<0) {
34+
throw new IndexOutOfBoundsException();
35+
}
36+
return elementDataObjects[index];
37+
}
38+
39+
public Object remove(int index){
40+
if (index>=size||index<0) {
41+
throw new IndexOutOfBoundsException();
42+
}
43+
Object rem=elementDataObjects[index];
44+
System.arraycopy(elementDataObjects, index+1, elementDataObjects, index, size-index-1);
45+
size--;
46+
return rem;
47+
}
48+
49+
public int size(){
50+
return size;
51+
}
52+
53+
// public Iterator iterator(){
54+
//
55+
// }
56+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package liuxincourse;
2+
3+
4+
public class LinkedList implements List{
5+
6+
private Node head;
7+
8+
private int size=0;
9+
10+
11+
public void add(Object o){
12+
if (size==0) {
13+
head=new Node();
14+
head.data=o;
15+
size++;
16+
return;
17+
}
18+
Node last=head;
19+
for (int i = 0; i < size-1; i++) {
20+
last=last.next;
21+
}
22+
Node added=new Node();
23+
last.next=added;
24+
added.data=o;
25+
size++;
26+
}
27+
28+
public void add(int index,Object o){
29+
if (index<0||index>size) {
30+
throw new IndexOutOfBoundsException();
31+
}
32+
Node pre=getNode(index-1);
33+
Node next=getNode(index);
34+
Node addedNode=new Node();
35+
addedNode.data=o;
36+
pre.next=addedNode;
37+
addedNode.next=next;
38+
size++;
39+
}
40+
41+
private Node getNode(int index){
42+
Node node=head;
43+
44+
for (int i = 0; i < index; i++) {
45+
node=node.next;
46+
}
47+
48+
return node;
49+
}
50+
51+
public Object get(int index){
52+
if (index<0||index>size-1) {
53+
throw new IndexOutOfBoundsException();
54+
}
55+
if (index==0&&head==null) {
56+
return null;
57+
}
58+
return getNode(index).data;
59+
60+
}
61+
62+
public Object remove(int index) {
63+
if (index<0||index>size-1) {
64+
throw new IndexOutOfBoundsException();
65+
}
66+
Node pre=getNode(index-1);
67+
Node next=getNode(index+1);
68+
pre.next=next;
69+
return getNode(index);
70+
}
71+
72+
public int size(){
73+
return size;
74+
}
75+
76+
public void addFirst(Object o){
77+
if (head==null) {
78+
head=new Node();
79+
head.data=o;
80+
size++;
81+
return;
82+
}
83+
Node addNode=new Node();
84+
addNode.data=o;
85+
addNode.next=head;
86+
head=addNode;
87+
size++;
88+
}
89+
90+
public void addLast(Object o){
91+
Node preLast=getNode(size-1);
92+
Node addNode=new Node();
93+
addNode.data=o;
94+
preLast.next=addNode;
95+
size++;
96+
}
97+
98+
public Object removeFirst(){
99+
Node preHead=head;
100+
head=head.next;
101+
size--;
102+
return preHead.data;
103+
}
104+
105+
public Object removeLast(){
106+
Node preLast=getNode(size-1);
107+
Node last=getNode(size-2);
108+
last.next=null;
109+
size--;
110+
return preLast.data;
111+
}
112+
113+
private static class Node{
114+
115+
Object data;
116+
Node next;
117+
118+
}
119+
120+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package liuxincourse;
2+
3+
public interface List {
4+
5+
void add(Object o);
6+
void add(int index,Object o);
7+
Object get(int index);
8+
Object remove(int index);
9+
int size();
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package liuxincourse;
2+
3+
public class Queue {
4+
5+
LinkedList list=new LinkedList();
6+
7+
public void enQueue(Object o){
8+
list.add(o);
9+
}
10+
11+
public Object deQueue(){
12+
13+
return list.removeFirst();
14+
}
15+
16+
public boolean isEmpty() {
17+
18+
return size()==0?true:false;
19+
}
20+
21+
public int size(){
22+
return list.size();
23+
}
24+
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package liuxincourse;
2+
3+
public class Stack {
4+
5+
private LinkedList elementData=new LinkedList();
6+
7+
public void push (Object o){
8+
elementData.addFirst(o);
9+
}
10+
11+
public Object pop() {
12+
return elementData.removeFirst();
13+
}
14+
15+
public Object peek(){
16+
return elementData.get(0);
17+
}
18+
19+
public boolean isEmpty(){
20+
return size()==0?true:false;
21+
}
22+
23+
public int size() {
24+
return elementData.size();
25+
}
26+
27+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package liuxincourse;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class ArrayListTest {
8+
9+
10+
@Test
11+
public void testAdd(){
12+
ArrayList list=new ArrayList();
13+
list.add(12);
14+
list.add(2);
15+
list.add(8);
16+
assertEquals(8, list.get(2));
17+
}
18+
19+
@Test
20+
public void testAddIndex(){
21+
ArrayList list=new ArrayList();
22+
list.add(12);
23+
list.add(2);
24+
list.add(8);
25+
list.add(1,33);
26+
list.add(6);
27+
list.add(7);
28+
assertEquals(8, list.get(3));
29+
}
30+
31+
@Test
32+
public void testRemove(){
33+
ArrayList list=new ArrayList();
34+
list.add(12);
35+
list.add(2);
36+
list.add(8);
37+
list.add(1,33);
38+
list.remove(1);
39+
assertEquals(2, list.get(1));
40+
}
41+
42+
@Test
43+
public void testSize(){
44+
ArrayList list=new ArrayList();
45+
list.add(12);
46+
list.add(2);
47+
list.add(8);
48+
list.add(9);
49+
list.add(77);
50+
assertEquals(5, list.size());
51+
}
52+
53+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package liuxincourse;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class LinkedListTest {
8+
9+
@Test
10+
public void testAdd() {
11+
LinkedList list=new LinkedList();
12+
list.add(33);
13+
list.add(44);
14+
list.add(55);
15+
list.add(88);
16+
assertEquals(88, list.get(3));
17+
}
18+
19+
@Test
20+
public void testAddIndex() {
21+
LinkedList list=new LinkedList();
22+
list.add(33);
23+
list.add(44);
24+
list.add(55);
25+
list.add(1, 88);
26+
assertEquals(55, list.get(3));
27+
}
28+
29+
@Test
30+
public void testAddFirst() {
31+
LinkedList list=new LinkedList();
32+
list.add(33);
33+
list.add(44);
34+
list.add(55);
35+
list.addFirst(88);
36+
assertEquals(88, list.get(0));
37+
}
38+
39+
@Test
40+
public void testAddLast() {
41+
LinkedList list=new LinkedList();
42+
list.add(33);
43+
list.add(44);
44+
list.add(55);
45+
list.addFirst(88);
46+
list.addLast(00);
47+
assertEquals(00, list.get(list.size()-1));
48+
}
49+
50+
@Test
51+
public void testRemoveFirst() {
52+
LinkedList list=new LinkedList();
53+
list.add(33);
54+
list.add(44);
55+
list.add(55);
56+
list.addFirst(88);
57+
assertEquals(88, list.removeFirst());
58+
}
59+
60+
@Test
61+
public void testRemoveLast() {
62+
LinkedList list=new LinkedList();
63+
list.add(33);
64+
list.add(44);
65+
list.add(55);
66+
list.addFirst(88);
67+
assertEquals(55, list.removeLast());
68+
}
69+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package liuxincourse;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class QueueTest {
8+
9+
@Test
10+
public void testEnqueue() {
11+
Queue queue=new Queue();
12+
queue.enQueue(11);
13+
queue.enQueue(22);
14+
assertEquals(11, queue.deQueue());
15+
}
16+
17+
@Test
18+
public void testIsempty() {
19+
Queue queue=new Queue();
20+
queue.enQueue(11);
21+
queue.enQueue(22);
22+
queue.deQueue();
23+
queue.deQueue();
24+
assertEquals(true, queue.isEmpty());
25+
}
26+
27+
}

0 commit comments

Comments
 (0)