Skip to content

Commit 7adcbb6

Browse files
authored
Merge pull request onlyliuxin#8 from XeCtvi/master
DataStructure
2 parents 23e8b88 + 3d76bff commit 7adcbb6

File tree

10 files changed

+380
-0
lines changed

10 files changed

+380
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>dataStructure</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.coding.basic;
2+
3+
public class ArrayList implements List {
4+
5+
private int size = 0;
6+
7+
private Object[] elementData = new Object[100];
8+
9+
public boolean add(Object o){
10+
add(size(), o);
11+
size++;
12+
return true;
13+
}
14+
public boolean add(int index, Object o){
15+
if (index >= 0 && index < elementData.length) {
16+
for (int i = size(); i > index; i--) {
17+
elementData[i] = elementData[i-1];
18+
}
19+
elementData[index] = o;
20+
}
21+
size++;
22+
return true;
23+
}
24+
25+
public Object get(int index){
26+
if (index >= 0 && index < size()) {
27+
return elementData[index];
28+
}
29+
// return null;
30+
throw new ArrayIndexOutOfBoundsException();
31+
}
32+
33+
public Object remove(int index){
34+
Object removedItem = elementData[index];
35+
if (index > size()) {
36+
throw new ArrayIndexOutOfBoundsException();
37+
}
38+
for (int i = index; i < size() - 1; i++) {
39+
elementData[i] = elementData[i+1];
40+
}
41+
size--;
42+
return removedItem;
43+
}
44+
45+
public int size(){
46+
return size;
47+
}
48+
49+
public Iterator iterator(){
50+
return new ArrayListIterator();
51+
}
52+
53+
private class ArrayListIterator implements Iterator {
54+
55+
private int current = 0;
56+
57+
public boolean hasNext() {
58+
return current < size();
59+
}
60+
61+
public Object next() {
62+
/* if (elementData[current+1] != null) {
63+
return elementData[current+1];
64+
} else {
65+
return null;
66+
}*/
67+
if (!hasNext()) {
68+
throw new java.util.NoSuchElementException();
69+
}
70+
return elementData[current++];
71+
}
72+
}
73+
74+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.coding.basic;
2+
3+
public class BinaryTreeNode {
4+
5+
private Object data;
6+
private BinaryTreeNode left;
7+
private BinaryTreeNode right;
8+
9+
public Object getData() {
10+
return data;
11+
}
12+
public void setData(Object data) {
13+
this.data = data;
14+
}
15+
public BinaryTreeNode getLeft() {
16+
return left;
17+
}
18+
public void setLeft(BinaryTreeNode left) {
19+
this.left = left;
20+
}
21+
public BinaryTreeNode getRight() {
22+
return right;
23+
}
24+
public void setRight(BinaryTreeNode right) {
25+
this.right = right;
26+
}
27+
28+
public BinaryTreeNode insert(Object o){
29+
return null;
30+
}
31+
32+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.coding.basic;
2+
3+
public interface Iterator {
4+
public boolean hasNext();
5+
public Object next();
6+
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package com.coding.basic;
2+
3+
public class LinkedList implements List {
4+
5+
private int size;
6+
private int modCount = 0;
7+
private Node beginMarker;
8+
private Node endMarker;
9+
10+
private static class Node{
11+
public Node( Object d, Node p, Node n) {
12+
data = d;
13+
prev = p;
14+
next = n;
15+
}
16+
public Object data;
17+
public Node prev;
18+
public Node next;
19+
}
20+
21+
public LinkedList () {
22+
doClear();
23+
}
24+
25+
public void clear() {
26+
doClear();
27+
}
28+
29+
private void doClear() {
30+
beginMarker = new Node(null, null, null);
31+
endMarker = new Node(null, beginMarker, null);
32+
beginMarker.next = endMarker;
33+
34+
size = 0;
35+
modCount++;
36+
}
37+
38+
public boolean add(Object o){
39+
add(size(), o);
40+
return true;
41+
}
42+
public boolean add(int index , Object o){
43+
/*if (index < 0 && index > size()) {
44+
throw new IndexOutOfBoundsException();
45+
}*/
46+
addBefore( getNode( index, 0, size()), o);
47+
return true;
48+
}
49+
50+
public void addFirst(Object o){
51+
add(0, o);
52+
}
53+
54+
public void addLast(Object o){
55+
add(size(), o);
56+
}
57+
58+
public Object removeFirst(){
59+
return remove(0);
60+
}
61+
62+
public Object removeLast(){
63+
return remove(size());
64+
}
65+
66+
public Object get(int index){
67+
return getNode(index).data;
68+
}
69+
public Object remove(int index){
70+
return remove(getNode(index));
71+
}
72+
73+
public int size(){
74+
return size;
75+
}
76+
77+
public boolean isEmpty() {
78+
return size() == 0;
79+
}
80+
81+
private void addBefore( Node p, Object o ) {
82+
Node newNode = new Node(o, p.prev, p );
83+
newNode.prev.next = newNode;
84+
p.prev = newNode;
85+
// p.prev = p.prev.next = new Node(o, p.prev, p);
86+
size++;
87+
modCount++;
88+
}
89+
90+
private Node getNode(int idx) {
91+
return getNode(idx, 0, size() - 1);
92+
}
93+
94+
private Node getNode(int idx, int lower, int upper) {
95+
Node p;
96+
if (idx < lower || idx > upper) {
97+
throw new IndexOutOfBoundsException();
98+
}
99+
100+
if (idx < size() / 2) {
101+
p = beginMarker.next;
102+
for (int i = 0; i < idx; i++) {
103+
p = p.next;
104+
}
105+
} else {
106+
p = endMarker.prev;
107+
for (int i = size(); i > idx; i--) {
108+
p = p.prev;
109+
}
110+
}
111+
return p;
112+
}
113+
114+
private Object remove( Node p ) {
115+
p.prev.next = p.next;
116+
p.next.prev = p.prev;
117+
modCount++;
118+
size--;
119+
120+
return p.data;
121+
}
122+
123+
124+
public java.util.Iterator iterator() {
125+
return new LinkListIterator();
126+
}
127+
128+
private class LinkListIterator implements java.util.Iterator {
129+
130+
private Node current = beginMarker.next;
131+
private int expectedModCount = modCount;
132+
private boolean okToRemove = false;
133+
134+
@Override
135+
public boolean hasNext() {
136+
return current != endMarker;
137+
}
138+
139+
@Override
140+
public Object next() {
141+
if ( modCount != expectedModCount ) {
142+
throw new java.util.ConcurrentModificationException();
143+
}
144+
if (!hasNext()) {
145+
throw new java.util.NoSuchElementException();
146+
}
147+
Object nextItem = current.next;
148+
current = current.next;
149+
okToRemove = true;
150+
return nextItem;
151+
}
152+
153+
public void remove() {
154+
if ( modCount != expectedModCount ) {
155+
throw new java.util.ConcurrentModificationException();
156+
}
157+
if ( !okToRemove ) {
158+
throw new IllegalStateException();
159+
}
160+
LinkedList.this.remove(current.prev);
161+
expectedModCount++;
162+
okToRemove = false;
163+
164+
}
165+
}
166+
}
167+
168+
class TestLinkLedList {
169+
170+
public static void main(String[] args) {
171+
LinkedList lst = new LinkedList();
172+
for (int i = 0; i < 10; i++) {
173+
lst.add(i);
174+
}
175+
for (int i = 20; i < 30; i++) {
176+
lst.add(0, i);
177+
}
178+
179+
lst.remove(0);
180+
lst.remove(lst.size()-1);
181+
182+
System.out.println(lst);
183+
184+
java.util.Iterator itr = lst.iterator();
185+
while (itr.hasNext()) {
186+
itr.next();
187+
itr.remove();
188+
System.out.println(lst);
189+
}
190+
}
191+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.coding.basic;
2+
3+
public interface List {
4+
public boolean add(Object o);
5+
public boolean add(int index, Object o);
6+
public Object get(int index);
7+
public Object remove(int index);
8+
public int size();
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.coding.basic;
2+
3+
public class Queue {
4+
5+
private LinkedList elementData;
6+
7+
public void enQueue(Object o){
8+
elementData.add(o);
9+
}
10+
11+
public Object deQueue(){
12+
return null;
13+
}
14+
15+
public boolean isEmpty(){
16+
return false;
17+
}
18+
19+
public int size(){
20+
return -1;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.coding.basic;
2+
3+
public class Stack {
4+
private ArrayList elementData = new ArrayList();
5+
6+
public void push(Object o){
7+
}
8+
9+
public Object pop(){
10+
return null;
11+
}
12+
13+
public Object peek(){
14+
return null;
15+
}
16+
public boolean isEmpty(){
17+
return false;
18+
}
19+
public int size(){
20+
return -1;
21+
}
22+
}

0 commit comments

Comments
 (0)