Skip to content

Commit f6c77f4

Browse files
author
unknown
committed
暂存作业
1 parent ff0ad0e commit f6c77f4

File tree

3 files changed

+166
-40
lines changed

3 files changed

+166
-40
lines changed

group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java

Lines changed: 83 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,119 @@
33
/**
44
* Created by QiPan on 2017/2/23.
55
*/
6-
public class LinkedList implements List {
6+
public class LinkedList {
77

8-
Node last;
8+
private Node head;
9+
private int size;
10+
public LinkedList() {
11+
head = new Node(null, null);
12+
size = 0;
13+
}
914

15+
public void add(Object o){
16+
add(size, o);
17+
}
1018

11-
public boolean add(Object o) {
19+
public void add(int index , Object o){
20+
if (index > size || index < 0) {
21+
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
22+
}
23+
Node frontNode = getNode(index-1);
24+
Node newNode = new Node(o, frontNode.next);
25+
frontNode.next = newNode;
26+
size++;
1227

13-
return true;
1428
}
1529

16-
public Object set(int index, Object element) {
17-
return null;
30+
private Node getNode(int index) {
31+
Node node = head;
32+
int i = 0;
33+
while(node.next != null && i <= index) {
34+
node = node.next;
35+
i++;
36+
}
37+
return node;
1838
}
1939

20-
public boolean add(int index, Object o) {
40+
public Object get(int index){
41+
if (index >= size || index < 0) {
42+
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
43+
}
44+
45+
Node node = getNode(index);
46+
return node.data;
47+
}
2148

22-
return true;
49+
public Object remove(int index){
50+
if (index >= size || index < 0) {
51+
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
52+
}
53+
Node frontNode = getNode(index-1);
54+
Node oldNode = getNode(index);
55+
frontNode.next = oldNode.next;
56+
size--;
57+
return oldNode.data;
2358
}
2459

25-
public Object get(int index) {
26-
return null;
60+
public int size(){
61+
return size;
2762
}
2863

29-
public Object remove(int index) {
30-
return null;
64+
public void addFirst(Object o){
65+
add(0, o);
3166
}
3267

33-
public int size() {
34-
return 0;
68+
public void addLast(Object o){
69+
add(size, o);
3570
}
3671

37-
public boolean isEmpty() {
38-
return false;
72+
public Object removeFirst(){
73+
return remove(0);
3974
}
4075

41-
public Iterator iterator() {
42-
return null;
76+
public Object removeLast(){
77+
return remove(size-1);
4378
}
44-
45-
private static class Node {
46-
Object item;
47-
Node next;
4879

49-
Node(Object item, Node next) {
50-
this.item = item;
51-
this.next = next;
80+
public Iterator iterator(){
81+
return new LinkedListIterator();
82+
}
83+
84+
private class LinkedListIterator implements Iterator {
85+
int index;
86+
final int capacity = size;
87+
LinkedListIterator() {
88+
index = 0;
5289
}
53-
public Object getItem() {
54-
return item;
90+
@Override
91+
public boolean hasNext() {
92+
return index < capacity;
5593
}
5694

57-
public void setItem(Object item) {
58-
this.item = item;
95+
@Override
96+
public Object next() {
97+
return get(index++);
5998
}
6099

61-
public Node getNext() {
62-
return next;
100+
@Override
101+
public void remove() {
102+
63103
}
104+
}
64105

65-
public void setNext(Node next) {
106+
private String outOfBoundsMsg(int index) {
107+
return "index:" + index + ", size:" + size;
108+
}
109+
110+
private static class Node {
111+
Object data;
112+
Node next;
113+
114+
Node(Object data, Node next) {
115+
this.data = data;
66116
this.next = next;
67117
}
68118
}
69119

120+
70121
}

group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,49 @@
22

33
/**
44
* Created by QiPan on 2017/2/23.
5+
* 队列,链表实现版本
56
*/
6-
public class Queue {
7+
public class Queue<E> {
78

8-
public void enQueue(Object o){
9+
private Node first;
10+
private Node last;
11+
private int N;
12+
13+
private class Node {
14+
E item;
15+
Node next;
16+
}
17+
18+
public void enQueue(E e) {
19+
// 向表尾添加元素
20+
Node oldLast = last;
21+
last = new Node();
22+
last.item = e;
23+
last.next = null;
24+
if (isEmpty()){// 如果是往空的队列里面添加东西。那么首尾链表都是指向第一个元素
25+
first = last;
26+
}else {
27+
28+
oldLast.next = last;
29+
}
30+
N++;
931
}
1032

11-
public Object deQueue(){
12-
return null;
33+
public E deQueue() {
34+
E item = first.item;
35+
first = first.next;
36+
if (isEmpty()){
37+
last = null;
38+
}
39+
N--;
40+
return item;
1341
}
1442

15-
public boolean isEmpty(){
16-
return false;
43+
public boolean isEmpty() {
44+
return first == null;
1745
}
1846

19-
public int size(){
20-
return -1;
47+
public int size() {
48+
return N;
2149
}
2250
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.apn.coding2017.basic;
2+
3+
/**
4+
* Created by Pan on 2017/2/25.
5+
* 栈(链表实现): 下压栈。操作栈顶元素
6+
*/
7+
public class Stack2<E> {
8+
9+
private Node first;
10+
private int N;
11+
12+
private class Node {
13+
E item;
14+
Node next;
15+
}
16+
17+
public boolean isEmpty(){
18+
return first == null;
19+
}
20+
21+
public int size(){
22+
return N;
23+
}
24+
25+
/**
26+
* 向栈顶添加元素
27+
* @param element
28+
*/
29+
public void push (E element){
30+
Node oldFirst = first;
31+
first = new Node();
32+
first.item = element;
33+
first.next = oldFirst;
34+
N++;
35+
}
36+
37+
/**
38+
* 弹出栈顶元素
39+
* @return
40+
*/
41+
public E pop(){
42+
E item = first.item;
43+
first = first.next;
44+
N--;
45+
return item;
46+
}
47+
}

0 commit comments

Comments
 (0)