Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions group14/190530132/20170219作业/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.coding.basic;

public class ArrayList {

private int size = 0;

private Object[] elementData = new Object[100];


//在ArrayList的尾部添加
public void add(Object o){

size = elementData.length + 1;
Object[] tempData = new Object[size];
System.arraycopy(elementData, 0, tempData,0, elementData.length);
elementData = tempData;

elementData[size-1] = o;

}

//在ArrayList中的某一个元素后面添加, 这里的关键在于先移动末尾的元素
public void add(int index, Object o){

size = elementData.length + 1;
Object[] tempData = new Object[size];
System.arraycopy(elementData, 0, tempData,0, elementData.length);
elementData = tempData;

for (int i=elementData.length-2; i>index; i--) {
elementData[i+1] = elementData[i];
}

elementData[index+1] = o;

}

//按下标来访问ArrayList中的元素
public Object get(int index){
return elementData[index];
}

//按下标来删除ArrayList中的元素
public Object remove(int index){
Object r = elementData[index];
for (int i=index; i<elementData.length-1; i++) {
elementData[i] = elementData[i+1];
}

size = elementData.length - 1;

Object[] tempData = new Object[size];
System.arraycopy(elementData, 0, tempData,0, size);
elementData = tempData;
return r;
}

//获取ArrayList的大小
public int size(){
return size;
}

//public Iterator iterator(){
// return null;
//}

}
128 changes: 128 additions & 0 deletions group14/190530132/20170219作业/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.coding.basic;
import java.util.NoSuchElementException;

//以下实现的是单向链表
public class LinkedList {

private int size = 0;

private Node head;


//在不指定index的情况下,默认在链表的尾部添加新节点
public void add(Object o){
addLast(o);
}


//在指定index的情况下,
public void add(int index , Object o){

//index越界检查
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index:" + index + ",size:" + size);
}

Node n = new Node();
n.data = o;
Node m = get(index);
n.next = m.next;
m.next = n;
size = size + 1;
}

public Node get(int index){
//index越界检查
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index:" + index + ",size:" + size);
}

Node n = head.next;
int count = 0;
while(count<=index){
if(count==index){
return n;
}
n = n.next;
count++;
}

return null;
}

public void remove(int index){
if(index<0||index>=size)
throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size);

Node d = get(index);
Node pred = get(index-1);
pred.next = d.next;
size = size - 1;
}

public int size(){
return size;
}

public void addFirst(Object o){
Node n = new Node();
n.data = o;

//避免空链表
if (head==null) head=new Node();

n.next = head.next;
head.next = n;
size = size + 1;
}

public void addLast(Object o){
Node n = new Node();
n.data = o;

//避免空链表
if (head==null) head = new Node();

//从头部往后顺序查找,找到尾部就添加
Node m = head;
while (m.next != null){
m = m.next;
}
n.next = m.next;
m.next = n;
size = size + 1;
}

public Object removeFirst(){
if(head==null||head.next==null)
throw new NoSuchElementException();
Node d = head.next;
head.next = d.next;
size = size - 1;
return d.data;
}

public Object removeLast(){
if(head==null||head.next==null)
throw new NoSuchElementException();

Node m = head;
Node n = head.next;
while(n.next != null){
m = n;
n = n.next;
}
size = size - 1;
return m.data;
}

//public Iterator iterator(){
// return null;
//}

private static class Node{
Node next;
Object data;
}

}
25 changes: 25 additions & 0 deletions group14/190530132/20170219作业/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.coding.basic;

public class Queue {

LinkedList l = new LinkedList();
public void enQueue(Object o){
l.addLast(o);
}

public Object deQueue(){
return l.removeFirst();
}

public boolean isEmpty(){
if (l.size() == 0)
return true;
else
return false;
}

public int size(){
return l.size();
}

}
33 changes: 33 additions & 0 deletions group14/190530132/20170219作业/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.coding.basic;

public class Stack {

private ArrayList elementData = new ArrayList();

public void push(Object o){
elementData.add(o);
}

public Object pop(){
int index = elementData.size() - 1;
Object o = elementData.remove(index);
return o;
}

public Object peek(){
int e = elementData.size() - 1;
return elementData.get(e);
}

public boolean isEmpty(){
if (elementData.size() == 0 )
return true;
else
return false;
}

public int size(){
return elementData.size();
}

}
1 change: 1 addition & 0 deletions group14/190530132/20170219作业/文章链接.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://rexwcl.blog.163.com/blog/static/270599039201712651450997/