forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
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 #13 from hengzwd/master
数据结构代码提交
- Loading branch information
Showing
7 changed files
with
324 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,79 @@ | ||
package com.coding.basic; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o) { | ||
elementData[size] = o; | ||
size++; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
for (int i = size; i > index; i--) { | ||
elementData[i] = elementData[i - 1]; | ||
} | ||
|
||
elementData[index] = o; | ||
|
||
} | ||
|
||
public Object get(int index) { | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
Object oj = elementData[index]; | ||
for (int i = index; i < size; i++) { | ||
elementData[i] = elementData[i + 1]; | ||
} | ||
elementData[size--] = null; | ||
return oj; | ||
} | ||
|
||
public int size() { | ||
|
||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new MyIterator(); | ||
} | ||
|
||
public class MyIterator implements Iterator { | ||
|
||
int i = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
while (elementData[i] != null) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return elementData[i++]; | ||
} | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
ArrayList a = new ArrayList(); | ||
a.add(1); | ||
a.add(2); | ||
a.add(3); | ||
|
||
MyIterator b = (MyIterator) a.iterator(); | ||
|
||
while(b.hasNext()){ | ||
System.out.println(b.next()); | ||
} | ||
} | ||
|
||
|
||
|
||
} |
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,46 @@ | ||
package com.coding.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public Object getData() { | ||
|
||
return data; | ||
} | ||
|
||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
|
||
public BinaryTreeNode getLeft() { | ||
return left; | ||
} | ||
|
||
public void setLeft(BinaryTreeNode left) { | ||
this.left = left; | ||
} | ||
|
||
public BinaryTreeNode getRight() { | ||
return right; | ||
} | ||
|
||
public void setRight(BinaryTreeNode right) { | ||
this.right = right; | ||
} | ||
|
||
public BinaryTreeNode insert(Object o) { | ||
BinaryTreeNode node = new BinaryTreeNode(); | ||
if (left == null && right != null) { | ||
right = node; | ||
} else if (right == null & left != null) { | ||
left = node; | ||
} else { | ||
return null; | ||
} | ||
return node; | ||
} | ||
|
||
} |
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 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
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,136 @@ | ||
package com.coding.basic; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
private int size; | ||
|
||
public void add(Object o) { | ||
Node n = new Node(); | ||
n.data = o; | ||
if (head != null) { | ||
n.next = head; | ||
size++; | ||
} else { | ||
size = 1; | ||
} | ||
head = n; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
|
||
Node n1 = head; | ||
Node n2 = new Node(); | ||
for (int i = size - 1; i >= index; i--) { | ||
if (i == index) { | ||
n2.next = n1.next; | ||
n2.data = 0; | ||
n1.next = n2; | ||
} else { | ||
n1 = n1.next; | ||
} | ||
|
||
} | ||
|
||
size++; | ||
} | ||
|
||
public Object get(int index) { | ||
Node n1 = head; | ||
Object o = null; | ||
for (int i = size - 1; i >= index; i--) { | ||
n1 = n1.next; | ||
if (i == index) { | ||
o = n1.data; | ||
} | ||
|
||
} | ||
|
||
return o; | ||
} | ||
|
||
public Object remove(int index) { | ||
Node n1 = head; | ||
Node n2 = new Node(); | ||
Node n3 = new Node(); | ||
Object o = null; | ||
for (int i = size - 1; i >= index; i--) { | ||
if (i == index + 1) { | ||
n2 = n1.next; | ||
} else if (i == index) { | ||
n3 = n2.next; | ||
o = n3.data; | ||
n1 = n3.next; | ||
} else { | ||
n1 = n1.next; | ||
} | ||
|
||
} | ||
n2.next = n1; | ||
size--; | ||
return o; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o) { | ||
Node n=new Node(); | ||
Node n1=head; | ||
|
||
for(int i=size-1;i>=0;i--) | ||
{ | ||
n1=n1.next; | ||
if(i==0){ | ||
n=n1.next; | ||
n.data=o; | ||
} | ||
|
||
} | ||
} | ||
|
||
public void addLast(Object o) { | ||
Node n=new Node(); | ||
n.data=o; | ||
n.next=head; | ||
head=n; | ||
|
||
} | ||
|
||
public Object removeFirst() { | ||
Object o= new Object(); | ||
Node n1=head; | ||
|
||
for(int i=size-1;i>=0;i--) | ||
{ | ||
n1=n1.next; | ||
if(i==1){ | ||
o=n1.next.data; | ||
n1.next=null; | ||
} | ||
|
||
} | ||
return o; | ||
} | ||
|
||
public Object removeLast() { | ||
Object o= new Object(); | ||
Node n=head; | ||
head=n.next; | ||
o=n.data; | ||
n.next=null; | ||
return o; | ||
} | ||
|
||
public Iterator iterator() { | ||
|
||
return null; | ||
} | ||
|
||
private static class Node { | ||
Object data; | ||
Node next; | ||
|
||
} | ||
} |
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,9 @@ | ||
package com.coding.basic; | ||
|
||
public interface List { | ||
public void add(Object o); | ||
public void add(int index, Object o); | ||
public Object get(int index); | ||
public Object remove(int index); | ||
public int size(); | ||
} |
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,23 @@ | ||
package com.coding.basic; | ||
|
||
public class Queue { | ||
|
||
private LinkedList list =new LinkedList(); | ||
|
||
public void enQueue(Object o){ | ||
|
||
list.addFirst(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
return list.removeLast(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return list.size()==0; | ||
} | ||
|
||
public int size(){ | ||
return list.size(); | ||
} | ||
} |
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,24 @@ | ||
package com.coding.basic; | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object pop(){ | ||
|
||
return elementData.remove(elementData.size()-1); | ||
} | ||
|
||
public Object peek(){ | ||
return elementData.get(elementData.size()-1); | ||
} | ||
public boolean isEmpty(){ | ||
return elementData.size()==0; | ||
} | ||
public int size(){ | ||
return elementData.size(); | ||
} | ||
} |