Skip to content

Commit 7fb8093

Browse files
committed
数据结构代码提交
1 parent 4ea6342 commit 7fb8093

File tree

7 files changed

+324
-0
lines changed

7 files changed

+324
-0
lines changed

group15/1514_616019420/ArrayList.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 void add(Object o) {
10+
elementData[size] = o;
11+
size++;
12+
}
13+
14+
public void add(int index, Object o) {
15+
for (int i = size; i > index; i--) {
16+
elementData[i] = elementData[i - 1];
17+
}
18+
19+
elementData[index] = o;
20+
21+
}
22+
23+
public Object get(int index) {
24+
return elementData[index];
25+
}
26+
27+
public Object remove(int index) {
28+
Object oj = elementData[index];
29+
for (int i = index; i < size; i++) {
30+
elementData[i] = elementData[i + 1];
31+
}
32+
elementData[size--] = null;
33+
return oj;
34+
}
35+
36+
public int size() {
37+
38+
return size;
39+
}
40+
41+
public Iterator iterator() {
42+
return new MyIterator();
43+
}
44+
45+
public class MyIterator implements Iterator {
46+
47+
int i = 0;
48+
49+
@Override
50+
public boolean hasNext() {
51+
while (elementData[i] != null) {
52+
return true;
53+
}
54+
return false;
55+
}
56+
57+
@Override
58+
public Object next() {
59+
return elementData[i++];
60+
}
61+
}
62+
63+
64+
public static void main(String[] args) {
65+
ArrayList a = new ArrayList();
66+
a.add(1);
67+
a.add(2);
68+
a.add(3);
69+
70+
MyIterator b = (MyIterator) a.iterator();
71+
72+
while(b.hasNext()){
73+
System.out.println(b.next());
74+
}
75+
}
76+
77+
78+
79+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
11+
return data;
12+
}
13+
14+
public void setData(Object data) {
15+
this.data = data;
16+
}
17+
18+
public BinaryTreeNode getLeft() {
19+
return left;
20+
}
21+
22+
public void setLeft(BinaryTreeNode left) {
23+
this.left = left;
24+
}
25+
26+
public BinaryTreeNode getRight() {
27+
return right;
28+
}
29+
30+
public void setRight(BinaryTreeNode right) {
31+
this.right = right;
32+
}
33+
34+
public BinaryTreeNode insert(Object o) {
35+
BinaryTreeNode node = new BinaryTreeNode();
36+
if (left == null && right != null) {
37+
right = node;
38+
} else if (right == null & left != null) {
39+
left = node;
40+
} else {
41+
return null;
42+
}
43+
return node;
44+
}
45+
46+
}

group15/1514_616019420/Iterator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.coding.basic;
2+
3+
public interface Iterator {
4+
public boolean hasNext();
5+
public Object next();
6+
7+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.coding.basic;
2+
3+
public class LinkedList implements List {
4+
5+
private Node head;
6+
private int size;
7+
8+
public void add(Object o) {
9+
Node n = new Node();
10+
n.data = o;
11+
if (head != null) {
12+
n.next = head;
13+
size++;
14+
} else {
15+
size = 1;
16+
}
17+
head = n;
18+
}
19+
20+
public void add(int index, Object o) {
21+
22+
Node n1 = head;
23+
Node n2 = new Node();
24+
for (int i = size - 1; i >= index; i--) {
25+
if (i == index) {
26+
n2.next = n1.next;
27+
n2.data = 0;
28+
n1.next = n2;
29+
} else {
30+
n1 = n1.next;
31+
}
32+
33+
}
34+
35+
size++;
36+
}
37+
38+
public Object get(int index) {
39+
Node n1 = head;
40+
Object o = null;
41+
for (int i = size - 1; i >= index; i--) {
42+
n1 = n1.next;
43+
if (i == index) {
44+
o = n1.data;
45+
}
46+
47+
}
48+
49+
return o;
50+
}
51+
52+
public Object remove(int index) {
53+
Node n1 = head;
54+
Node n2 = new Node();
55+
Node n3 = new Node();
56+
Object o = null;
57+
for (int i = size - 1; i >= index; i--) {
58+
if (i == index + 1) {
59+
n2 = n1.next;
60+
} else if (i == index) {
61+
n3 = n2.next;
62+
o = n3.data;
63+
n1 = n3.next;
64+
} else {
65+
n1 = n1.next;
66+
}
67+
68+
}
69+
n2.next = n1;
70+
size--;
71+
return o;
72+
}
73+
74+
public int size() {
75+
return size;
76+
}
77+
78+
public void addFirst(Object o) {
79+
Node n=new Node();
80+
Node n1=head;
81+
82+
for(int i=size-1;i>=0;i--)
83+
{
84+
n1=n1.next;
85+
if(i==0){
86+
n=n1.next;
87+
n.data=o;
88+
}
89+
90+
}
91+
}
92+
93+
public void addLast(Object o) {
94+
Node n=new Node();
95+
n.data=o;
96+
n.next=head;
97+
head=n;
98+
99+
}
100+
101+
public Object removeFirst() {
102+
Object o= new Object();
103+
Node n1=head;
104+
105+
for(int i=size-1;i>=0;i--)
106+
{
107+
n1=n1.next;
108+
if(i==1){
109+
o=n1.next.data;
110+
n1.next=null;
111+
}
112+
113+
}
114+
return o;
115+
}
116+
117+
public Object removeLast() {
118+
Object o= new Object();
119+
Node n=head;
120+
head=n.next;
121+
o=n.data;
122+
n.next=null;
123+
return o;
124+
}
125+
126+
public Iterator iterator() {
127+
128+
return null;
129+
}
130+
131+
private static class Node {
132+
Object data;
133+
Node next;
134+
135+
}
136+
}

group15/1514_616019420/List.java

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 void add(Object o);
5+
public void add(int index, Object o);
6+
public Object get(int index);
7+
public Object remove(int index);
8+
public int size();
9+
}

group15/1514_616019420/Queue.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.coding.basic;
2+
3+
public class Queue {
4+
5+
private LinkedList list =new LinkedList();
6+
7+
public void enQueue(Object o){
8+
9+
list.addFirst(o);
10+
}
11+
12+
public Object deQueue(){
13+
return list.removeLast();
14+
}
15+
16+
public boolean isEmpty(){
17+
return list.size()==0;
18+
}
19+
20+
public int size(){
21+
return list.size();
22+
}
23+
}

group15/1514_616019420/Stack.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.coding.basic;
2+
3+
public class Stack {
4+
private ArrayList elementData = new ArrayList();
5+
6+
public void push(Object o){
7+
elementData.add(o);
8+
}
9+
10+
public Object pop(){
11+
12+
return elementData.remove(elementData.size()-1);
13+
}
14+
15+
public Object peek(){
16+
return elementData.get(elementData.size()-1);
17+
}
18+
public boolean isEmpty(){
19+
return elementData.size()==0;
20+
}
21+
public int size(){
22+
return elementData.size();
23+
}
24+
}

0 commit comments

Comments
 (0)