forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #11 from bananaloveapple/master
datastructure
- Loading branch information
Showing
7 changed files
with
363 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,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>basicstructure</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
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,86 @@ | ||
package firstday; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayListt { | ||
private Object arr[]=new Object[10]; | ||
private int pos=0; | ||
public boolean add(Object o) | ||
{ | ||
if(pos<arr.length) | ||
{ | ||
arr[pos]=o; | ||
pos++; | ||
return true; | ||
} | ||
else if(pos>=arr.length) | ||
{ | ||
arr=Arrays.copyOf(arr, arr.length+1); | ||
arr[pos] = o; | ||
pos++; | ||
return true; | ||
} | ||
return false; | ||
} | ||
public boolean add(Object o, int index) | ||
{ | ||
if(pos<arr.length) | ||
{ | ||
for(int i=index;i<arr.length;i++) | ||
{ | ||
arr[i+1]=arr[i]; | ||
} | ||
arr[index] = o; | ||
return true; | ||
} | ||
else if(pos>=arr.length) | ||
{ | ||
arr=Arrays.copyOf(arr, arr.length+1); | ||
for(int i=arr.length-2;i>=index;i--) | ||
{ | ||
arr[i+1] = arr[i]; | ||
} | ||
arr[index] = o; | ||
return true; | ||
} | ||
return false; | ||
} | ||
public Object get(int i) | ||
{ | ||
Object o = arr[i]; | ||
return o; | ||
} | ||
public Object remove(int index) | ||
{ | ||
Object var=arr[index]; | ||
for(int i=index+1;i<arr.length;i++) | ||
{ | ||
arr[i-1]=arr[i]; | ||
} | ||
pos--; | ||
return var; | ||
} | ||
public int size() | ||
{ | ||
return pos; | ||
} | ||
public static void main(String[] args) | ||
{ | ||
ArrayListt atest = new ArrayListt(); | ||
for(int i=0;i<10;i++) | ||
{ | ||
atest.add(i); | ||
} | ||
atest.add(99,7); | ||
for(int i=0;i<atest.arr.length;i++) | ||
{ | ||
System.out.println(atest.get(i)); | ||
} | ||
atest.remove(1); | ||
for(int i=0;i<atest.arr.length;i++) | ||
{ | ||
System.out.println(atest.get(i)); | ||
} | ||
atest.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,167 @@ | ||
package firstday; | ||
|
||
import firstday.LinkListt; | ||
//import firstday.LinkListt.Node; | ||
|
||
public class LinkListt { | ||
private static class Node | ||
{ | ||
private Object date=null; | ||
private Node next=null; | ||
public Node(Object o) | ||
{ | ||
this.date=o; | ||
} | ||
} | ||
public Node head=null; | ||
public void add(Object o) | ||
{ | ||
Node nde=new Node(o); | ||
if(head == null) | ||
{ | ||
head=nde; | ||
} | ||
else | ||
{ | ||
Node order=head; | ||
while(true) | ||
{ | ||
if(order.next==null) | ||
{ | ||
order.next=nde; | ||
break; | ||
} | ||
order=order.next; | ||
} | ||
} | ||
} | ||
public void add(int index,Object o) | ||
{ | ||
int cnt=1; | ||
Node n=head; | ||
while(true) | ||
{ | ||
if(cnt==index-1) | ||
{ | ||
Node no=new Node(o); | ||
no.next=n.next; | ||
n.next=no; | ||
break; | ||
} | ||
n=n.next; | ||
cnt++; | ||
} | ||
} | ||
public Object get(int index) | ||
{ | ||
int cnt=1; | ||
Node n=head; | ||
while(true) | ||
{ | ||
if(cnt==index) | ||
{ | ||
return n.date; | ||
} | ||
n=n.next; | ||
cnt++; | ||
} | ||
} | ||
public Object remove(int index) | ||
{ | ||
int cnt=1; | ||
Node n=head; | ||
Node front=null; | ||
Object re=null; | ||
while(true) | ||
{ | ||
if(cnt==index-1) | ||
{ | ||
front=n; | ||
} | ||
if(cnt==index) | ||
{ | ||
front.next=n.next; | ||
n.next=null; | ||
re=n.date; | ||
n=null; | ||
return re; | ||
} | ||
cnt++; | ||
n=n.next; | ||
} | ||
} | ||
public int size() | ||
{ | ||
int cnt=1; | ||
Node n=head; | ||
while(true) | ||
{ | ||
if(n.next==null) | ||
{ | ||
return cnt; | ||
} | ||
cnt++; | ||
n=n.next; | ||
} | ||
} | ||
public void addFirst(Object o) | ||
{ | ||
Node n=new Node(o); | ||
n.next=head; | ||
head=n; | ||
} | ||
public Object removeFirst() | ||
{ | ||
Object re; | ||
Node n=head; | ||
head=n.next; | ||
n.next=null; | ||
re=n.date; | ||
n=null; | ||
return re; | ||
} | ||
public Object removeLast() | ||
{ | ||
Node n=head; | ||
Node front=null; | ||
Object re=null; | ||
while(true) | ||
{ | ||
if(n.next==null) | ||
{ | ||
re=n.date; | ||
front.next=null; | ||
n=null; | ||
return re; | ||
} | ||
front=n; | ||
n=n.next; | ||
} | ||
} | ||
public static void main(String[] args) | ||
{ | ||
LinkListt l=new LinkListt(); | ||
for(int i=0;i<5;i++) | ||
{ | ||
l.add(i); | ||
} | ||
l.add(3,8); | ||
for(int i=1;i<=6;i++) | ||
{ | ||
System.out.println(l.get(i)); | ||
} | ||
//System.out.println(l.size()); | ||
l.addFirst(99); | ||
for(int i=1;i<=7;i++) | ||
{ | ||
System.out.println(l.get(i)); | ||
} | ||
l.removeFirst(); | ||
for(int i=1;i<=6;i++) | ||
{ | ||
System.out.println(l.get(i)); | ||
} | ||
System.out.println(l.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,43 @@ | ||
package firstday; | ||
import firstday.LinkListt; | ||
public class Queue { | ||
private LinkListt l=new LinkListt(); | ||
public Object deQueue() | ||
{ | ||
return l.removeFirst(); | ||
} | ||
public void enQueue(Object o) | ||
{ | ||
l.add(o); | ||
} | ||
public boolean isEmpty() | ||
{ | ||
if(l.head==null) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
public int size() | ||
{ | ||
return l.size(); | ||
} | ||
public static void main(String[] args) | ||
{ | ||
Queue q=new Queue(); | ||
for(int i=0;i<4;i++) | ||
{ | ||
q.enQueue(i); | ||
} | ||
System.out.println(q.size()); | ||
for(int i=0;i<4;i++) | ||
{ | ||
System.out.println(q.deQueue()); | ||
} | ||
System.out.println(q.size()); | ||
//System.out.println(q.isEmpty()); | ||
} | ||
} |
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,43 @@ | ||
package firstday; | ||
import firstday.ArrayListt; | ||
public class Stack { | ||
private ArrayListt a=new ArrayListt(); | ||
public Object peek() | ||
{ | ||
return a.get(a.size()-1); | ||
} | ||
public Object pop() | ||
{ | ||
return a.remove(a.size()-1); | ||
} | ||
public void push(Object o) | ||
{ | ||
a.add(o); | ||
} | ||
public boolean isEmpty() | ||
{ | ||
if(a.size()==0) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
public int size() | ||
{ | ||
return a.size(); | ||
} | ||
public static void main(String[] args) | ||
{ | ||
Stack s=new Stack(); | ||
for(int i=0;i<4;i++) | ||
{ | ||
s.push(i); | ||
} | ||
s.pop(); | ||
System.out.println(s.peek()); | ||
System.out.println(s.size()); | ||
} | ||
} |