Skip to content

Commit 594a6ed

Browse files
ThinkPadThinkPad
authored andcommitted
linkedlist .文章 提交
1 parent 317f47a commit 594a6ed

File tree

3 files changed

+196
-3
lines changed

3 files changed

+196
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## cpu 内存 硬盘 指令之间的关系
2+
> 计算机整体分为软件和硬件。
3+
> 软件是由指令和数据组成的程序,硬件包含主板、CPU、内存、硬盘等
4+
5+
6+
指令指的是计算机可以识别的机器语言,每个操作系统都有内置的指令集,计算机可根据不同的指令来驱动计算机工作或是相应的输出。指令是以二进制的形式存储在硬盘中,当CPU执行某个指令的时候,需要将指令先从硬盘加载到内存,CPU从内存中获取执行解释执行。
7+
8+
CPU是计算的核心部件,相当于人类的大脑。CPU执行指令的时候其实不关心也不知道指令的意思,只是按照指令执行。由于硬盘读写性能比较差,CPU从硬盘中直接读取数据的时候比较费时,就有的内存的存在。内存比硬盘读写速度快很多,CPU要执行程序时,先将程序加载到内存中在执行。
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package com.my.list;
2+
3+
/**
4+
* 实现 LinkedList
5+
*
6+
*/
7+
public class LinkedList {
8+
9+
//首节点
10+
Node first = new Node();
11+
//集合大小
12+
int size = 0;
13+
14+
/**
15+
* 添加元素
16+
* @param object
17+
* @return
18+
*/
19+
public Object add(Object object){
20+
if (size == 0) {
21+
first.setObject(object);
22+
}else{
23+
Node previous = first;
24+
Node temp = first.getNext();
25+
while (temp != null) {
26+
previous = temp;
27+
temp = temp.getNext();
28+
}
29+
Node node = new Node();
30+
node.setObject(object);
31+
previous.setNext(node);
32+
}
33+
++size;
34+
return object;
35+
}
36+
37+
/**
38+
* 根据下标添加元素
39+
* @param index
40+
* @param object
41+
* @return
42+
*/
43+
public Object add(int index , Object object){
44+
if (index < 0 || index > size) {
45+
throw new IndexOutOfBoundsException();
46+
}
47+
48+
if (size == 0) {
49+
first.setObject(object);
50+
}else{
51+
if (index == 0) {
52+
Node temp = new Node();
53+
temp.setObject(object);
54+
temp.setNext(first);
55+
first = temp;
56+
}else{
57+
int count = 1;
58+
Node temp = first;
59+
while (temp != null) {
60+
if (count++ == index) {
61+
Node next = temp.getNext();
62+
Node node = new Node();
63+
temp.setNext(node);
64+
node.setObject(object);
65+
node.setNext(next);
66+
break;
67+
}
68+
temp = temp.getNext();
69+
}
70+
}
71+
}
72+
++size;
73+
return object;
74+
}
75+
76+
/**
77+
* 根据下标删除元素
78+
* @param index
79+
* @return
80+
*/
81+
public Object remove(int index){
82+
if (index < 0 || index >= size) {
83+
throw new IndexOutOfBoundsException();
84+
}
85+
Node node = null;
86+
if (index == 0) {
87+
Node next = first.getNext();
88+
first = next;
89+
node = first;
90+
}else{
91+
int count = 1;
92+
Node temp = first;
93+
while (temp != null) {
94+
if (count++ == index) {
95+
node = temp.getNext();
96+
Node next = node.getNext();
97+
temp.setNext(next);
98+
break;
99+
}
100+
temp = temp.getNext();
101+
}
102+
}
103+
--size;
104+
return node.getObject();
105+
}
106+
107+
/**
108+
* 根据下标获取元素
109+
* @param index
110+
* @return
111+
*/
112+
public Object get(int index) {
113+
Node temp = first;
114+
int count = 0;
115+
while (temp != null) {
116+
if (count++ == index) {
117+
break;
118+
}
119+
temp = temp.getNext();
120+
}
121+
return temp.getObject();
122+
}
123+
124+
/**
125+
* 获取集合大小
126+
* @return
127+
*/
128+
public int size() {
129+
return size;
130+
}
131+
132+
133+
}
134+
135+
136+
/**
137+
* 节点元素
138+
*
139+
*/
140+
class Node{
141+
private Object object ;
142+
private Node next;
143+
public Object getObject() {
144+
return object;
145+
}
146+
public void setObject(Object object) {
147+
this.object = object;
148+
}
149+
public Node getNext() {
150+
return next;
151+
}
152+
public void setNext(Node next) {
153+
this.next = next;
154+
}
155+
156+
}

group08/121027265/0226/src/com/my/list/Test.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
public class Test {
44

55
public static void main(String[] args) {
6-
testArrayList();
76

8-
7+
testArrayList();
8+
System.out.println("--------------------------------");
9+
testLinkedList();
910
}
1011

1112
/**
12-
* list 测试
13+
* ArrayList 测试
1314
*/
1415
public static void testArrayList(){
16+
System.out.println("ArrayList 测试 --开始");
1517
ArrayList list = new ArrayList();
1618
list.add("123");
1719
list.add("123");
@@ -31,6 +33,33 @@ public static void testArrayList(){
3133
Object object = list.get(i);
3234
System.out.println(object);
3335
}
36+
System.out.println("ArrayList 测试 --结束");
37+
}
38+
39+
/**
40+
* LinkedList 测试
41+
*/
42+
public static void testLinkedList(){
43+
System.out.println("LinkedList 测试 --开始");
44+
LinkedList list = new LinkedList();
45+
list.add(1);
46+
list.add(2);
47+
list.add(3);
48+
list.add(4);
49+
list.add(5);
50+
list.add(6);
51+
52+
list.add(1, 111);
53+
54+
list.remove(1);
55+
56+
System.out.println(list.size());
57+
58+
for (int i = 0; i < list.size(); i++) {
59+
Object object = list.get(i);
60+
System.out.println(object);
61+
}
62+
System.out.println("LinkedList 测试 --结束");
3463
}
3564

3665
}

0 commit comments

Comments
 (0)