forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from yinwenbing/master
提交第一次作业
- Loading branch information
Showing
8 changed files
with
419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# CPU、内存、硬盘、指令之间的关系 | ||
|
||
## 1、简介 | ||
|
||
### 1.1 CPU | ||
> CPU,中央处理器,是一个超大规模的集成电路,是一台计算机的运算核心和控制核心。它的主要功能是解释计算机指令以及处理计算机软件中的数据。 | ||
### 1.2 内存 | ||
> 内存是计算机中重要的部件之一,它是与CPU沟通的桥梁。计算机中所有的程序都是在内存中运行的。其作用是用于暂时存放CPU中的运算数据,以及与硬盘等外部存储器交换数据。 | ||
### 1.3 硬盘 | ||
> 硬盘是计算机主要的存储媒介之一。硬盘分为固态硬盘(SSD 盘,新式硬盘)、机械硬盘(HDD 硬盘)、混合硬盘(HHD 一块基于传统机械硬盘诞生出来的新硬盘)。 | ||
### 1.4 指令 | ||
> 指挥计算机工作的指示命令。程序是一系列按一定顺序排列的,执行程序的过程就是计算机的工作过程。 | ||
## 2、关系 | ||
计算机在运行时,**CPU** 从**内存** 中获取第一条 **指令** ,然后内存再从硬盘中读取所需的数据。然后CPU再取出第二条指令,一直到指令结束。 | ||
|
||
## 3、参考资料 | ||
[中央处理器 —— 百度百科](http://baike.baidu.com/item/%E4%B8%AD%E5%A4%AE%E5%A4%84%E7%90%86%E5%99%A8/284033?sefr=cr&fromtitle=CPU&fromid=120556) | ||
|
||
[内存 —— 百度百科](http://baike.baidu.com/item/%E5%86%85%E5%AD%98?sefr=enterbtn) | ||
|
||
[硬盘 —— 百度百科](http://baike.baidu.com/item/%E7%A1%AC%E7%9B%98?sefr=enterbtn) | ||
|
||
[计算机指令 —— 百度百科](http://baike.baidu.com/item/%E8%AE%A1%E7%AE%97%E6%9C%BA%E6%8C%87%E4%BB%A4) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# IntelliJ IDEA | ||
|
||
target/ | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.kevin</groupId> | ||
<artifactId>coding01</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
|
||
</project> |
117 changes: 117 additions & 0 deletions
117
group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.kevin.coding01.basic; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* Created by YinWenBing on 2017/2/25. | ||
*/ | ||
public class MyArrayList<E> implements MyList<E> { | ||
private int size = 0; | ||
private Object[] elementData = new Object[10]; | ||
|
||
/** | ||
* 添加 | ||
* 判断数组空间是否足够,不够则扩容,将元素放在数组末尾 | ||
* | ||
* @param e | ||
*/ | ||
public void add(E e) { | ||
isGrow(size + 1);//判断是否需要扩容 | ||
elementData[size++] = e; | ||
} | ||
|
||
/** | ||
* 是否需要扩容 | ||
* | ||
* @param size | ||
*/ | ||
private void isGrow(int size) { | ||
if (size > elementData.length) { | ||
grow(size); | ||
} | ||
} | ||
|
||
/** | ||
* 扩容 | ||
* | ||
* @param minCapacity | ||
*/ | ||
private void grow(int minCapacity) { | ||
int oldCapacity = elementData.length; | ||
int newCapacity = oldCapacity + (oldCapacity >> 1);//>>将oldCapacity向右移一位,右移一位代表除2,右移n位代表除以2的n次方。左移则是乘 | ||
if (newCapacity - minCapacity < 0) { | ||
newCapacity = minCapacity; | ||
} else if (newCapacity - (Integer.MAX_VALUE - 8) > 0) { | ||
newCapacity = hugeCapacity(minCapacity); | ||
} | ||
elementData = Arrays.copyOf(elementData, newCapacity); | ||
} | ||
|
||
private int hugeCapacity(int minCapacity) { | ||
if (minCapacity < 0) { | ||
throw new OutOfMemoryError(); | ||
} | ||
return (minCapacity > (Integer.MAX_VALUE)) ? Integer.MAX_VALUE : Integer.MAX_VALUE - 8; | ||
} | ||
|
||
/** | ||
* 添加指定索引的元素 | ||
* 判断索引是否小于0或大于size | ||
* | ||
* @param index | ||
* @param e | ||
*/ | ||
public void add(int index, E e) { | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); | ||
} | ||
isGrow(index); | ||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
elementData[index] = e; | ||
size++; | ||
} | ||
|
||
/** | ||
* 根据索引获取数组中的元素 | ||
* | ||
* @param index | ||
* @return | ||
*/ | ||
public E get(int index) { | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); | ||
} | ||
return (E) elementData[index]; | ||
} | ||
|
||
/** | ||
* 根据索引移除数组中的元素,如果移除中间的元素,后面的元素要往前移 | ||
* | ||
* @param index | ||
* @return | ||
*/ | ||
public E remove(int index) { | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); | ||
} | ||
E oldValue = (E) elementData[index]; | ||
|
||
int numMoved = size - index - 1; | ||
if (numMoved > 0) { | ||
System.arraycopy(elementData, index + 1, elementData, index, numMoved); | ||
elementData[--size] = null; | ||
size--; | ||
} | ||
return oldValue; | ||
} | ||
|
||
/** | ||
* 获取数组中存放值得数量 | ||
* | ||
* @return | ||
*/ | ||
public int size() { | ||
return size; | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyLinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package com.kevin.coding01.basic; | ||
|
||
/** | ||
* Created by YinWenBing on 2017/2/25. | ||
*/ | ||
public class MyLinkedList<E> implements MyList<E> { | ||
private Node<E> first;//头节点 | ||
private int size = 0;//默认大小为0 | ||
|
||
public void add(E e) { | ||
if (size == 0) { | ||
first = new Node<E>(); | ||
first.element = e; | ||
size++; | ||
} else { | ||
Node head = first; | ||
for (int i = 0; i < size - 1; i++) { | ||
head = head.next; | ||
} | ||
|
||
Node add = new Node(); | ||
add.element = e; | ||
head.next = add; | ||
size++; | ||
} | ||
} | ||
|
||
|
||
public void add(int index, E e) { | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException("index:" + index + ";size:" + size); | ||
} | ||
Node prev = getNode(index - 1);//当前索引指向的节点的上一节点 | ||
Node next = getNode(index);//当前索引指向的节点成为添加节点的next | ||
Node add = new Node(); | ||
add.element = e; | ||
prev.next = add; | ||
add.next = next; | ||
size++; | ||
} | ||
|
||
private Node<E> getNode(int index) { | ||
Node node = first; | ||
|
||
for (int i = 0; i < index; i++) { | ||
node = first.next; | ||
} | ||
|
||
return node; | ||
} | ||
|
||
public E get(int index) { | ||
if (index < 0 || index > size - 1) { | ||
throw new IndexOutOfBoundsException("index:" + index + ";size:" + size); | ||
} | ||
|
||
if (size == 0) { | ||
return null; | ||
} | ||
|
||
return getNode(index).element; | ||
} | ||
|
||
public E remove(int index) { | ||
if (index < 0 || index > size - 1) { | ||
throw new IndexOutOfBoundsException("index:" + index + ";size:" + size); | ||
} | ||
|
||
Node prev = getNode(index - 1); | ||
Node next = getNode(index + 1); | ||
|
||
prev.next = next; | ||
return getNode(index).element; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public void addFirst(E e) { | ||
if (size == 0) { | ||
first = new Node<E>(); | ||
first.element = e; | ||
size++; | ||
} else { | ||
Node add = new Node(); | ||
add.element = e; | ||
add.next = first; | ||
first = add; | ||
size++; | ||
} | ||
} | ||
|
||
public void addLast(E e) { | ||
Node oldLast = getNode(size - 1); | ||
Node add = new Node(); | ||
add.element = e; | ||
oldLast.next = add; | ||
size++; | ||
} | ||
|
||
public E removeFirst() { | ||
Node oldFirst = first; | ||
if (first.next != null) { | ||
first = first.next; | ||
size--; | ||
return (E) oldFirst.element; | ||
} else {//只有一个节点或者一个节点也没有 | ||
first = null; | ||
return null; | ||
} | ||
} | ||
|
||
public E removeLast() { | ||
Node last = getNode(size - 1); | ||
if (last != null) { | ||
E element = (E) last.element; | ||
Node newLast = getNode(size - 2); | ||
newLast.next = null; | ||
size--; | ||
return element; | ||
} else { //一个节点都不存在 | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* 静态内部类 | ||
*/ | ||
private static class Node<E> { | ||
E element;//节点数据 | ||
Node<E> next;//下一节点 | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.kevin.coding01.basic; | ||
|
||
/** | ||
* Created by YinWenBing on 2017/2/25. | ||
*/ | ||
public interface MyList<E> { | ||
|
||
void add(E e); | ||
|
||
void add(int index, E e); | ||
|
||
E get(int index); | ||
|
||
E remove(int index); | ||
|
||
int size(); | ||
} |
35 changes: 35 additions & 0 deletions
35
group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.kevin.coding01.basic; | ||
|
||
/** | ||
* 队列:先进先出 | ||
* Created by YinWenBing on 2017/2/25. | ||
*/ | ||
public class MyQueue<E> { | ||
private MyLinkedList<E> elementDate = new MyLinkedList<E>(); | ||
|
||
//入队列 | ||
public void enQueue(E e) { | ||
elementDate.addLast(e); | ||
} | ||
|
||
//出队列 | ||
public E deQueue() { | ||
return elementDate.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return elementDate.size() == 0 ? true : false; | ||
} | ||
|
||
public int size() { | ||
return elementDate.size(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
MyQueue<Integer> queue = new MyQueue(); | ||
queue.enQueue(1); | ||
queue.enQueue(2); | ||
|
||
System.out.println(queue.deQueue());//1 | ||
} | ||
} |
Oops, something went wrong.