forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 7
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 #6 from JaneZhou91/master
data structures
- Loading branch information
Showing
7 changed files
with
250 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 @@ | ||
/bin/ |
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 @@ | ||
JaneZhou91's files |
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,57 @@ | ||
package data_Structures; | ||
|
||
|
||
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){ | ||
|
||
int temp = size; | ||
while(temp > index) | ||
{ | ||
elementData[temp] = elementData[temp-1]; | ||
temp--; | ||
} | ||
|
||
elementData[index] = o; | ||
|
||
size++; | ||
|
||
|
||
} | ||
|
||
public Object get(int index){ | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
|
||
int temp = index; | ||
while(temp < size-1) | ||
{ | ||
elementData[temp] = elementData[temp+1]; | ||
temp++; | ||
} | ||
size--; | ||
|
||
return elementData[index]; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
} |
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,125 @@ | ||
package data_Structures; | ||
|
||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
private int size; | ||
|
||
LinkedList() | ||
{ | ||
size = 0; | ||
} | ||
|
||
public void add(Object o){ | ||
|
||
Node newNode = new Node(o); | ||
|
||
if(head == null) | ||
{ | ||
head = newNode; | ||
} | ||
else | ||
{ | ||
Node tempNode = head; | ||
while(tempNode.next != null) | ||
{ | ||
tempNode = tempNode.next; | ||
} | ||
tempNode.next = newNode; | ||
|
||
} | ||
|
||
size++; | ||
} | ||
|
||
public void add(int index , Object o){ | ||
|
||
Node newNode = new Node(o, getNode(index)); | ||
getNode(index-1).next = newNode; | ||
|
||
size++; | ||
|
||
} | ||
|
||
public Node getNode(int index) | ||
{ | ||
Node tempNode = head; | ||
int i = 0; | ||
while(i < index) | ||
{ | ||
tempNode = tempNode.next; | ||
i++; | ||
} | ||
|
||
return tempNode; | ||
} | ||
|
||
|
||
|
||
public Object get(int index){ | ||
return getNode(index).data; | ||
} | ||
public Object remove(int index){ | ||
|
||
Node tempNode = getNode(index); | ||
getNode(index - 1).next = getNode(index+1); | ||
tempNode.next = null; | ||
tempNode.data = null; | ||
size--; | ||
return getNode(index).data; | ||
|
||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
|
||
Node tempNode = head; | ||
head = new Node(o); | ||
head.next = tempNode; | ||
|
||
size++; | ||
} | ||
public void addLast(Object o){ | ||
add(o); | ||
|
||
} | ||
public Object removeFirst(){ | ||
|
||
Node tempNode = head; | ||
head = getNode(1); | ||
size--; | ||
|
||
return tempNode.data; | ||
} | ||
public Object removeLast(){ | ||
getNode(size-1).next = null; | ||
size--; | ||
return getNode(size).data; | ||
} | ||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
|
||
Node(Object data, Node next ) | ||
{ | ||
this.data = data; | ||
this.next = next; | ||
} | ||
|
||
Node(Object data) | ||
{ | ||
this.data = data; | ||
this.next = null; | ||
} | ||
|
||
} | ||
} |
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 data_Structures; | ||
|
||
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,28 @@ | ||
package data_Structures; | ||
|
||
public class Queue { | ||
|
||
private LinkedList ll; | ||
|
||
Queue() | ||
{ | ||
ll = new LinkedList(); | ||
} | ||
|
||
public void enQueue(Object o){ | ||
ll.add(o);; | ||
} | ||
|
||
public Object deQueue(){ | ||
return ll.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
int size = ll.size(); | ||
return (size == 0); | ||
} | ||
|
||
public int size(){ | ||
return ll.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,29 @@ | ||
package data_Structures; | ||
|
||
public class Stack { | ||
// private ArrayList elementData = new ArrayList(); | ||
|
||
private LinkedList ll; | ||
Stack() | ||
{ | ||
ll = new LinkedList(); | ||
} | ||
|
||
public void push(Object o){ | ||
ll.addLast(o); | ||
} | ||
|
||
public Object pop(){ | ||
return ll.removeLast(); | ||
} | ||
|
||
public Object peek(){ | ||
return null; | ||
} | ||
public boolean isEmpty(){ | ||
return ll.size()==0; | ||
} | ||
public int size(){ | ||
return ll.size(); | ||
} | ||
} |