Skip to content

Commit

Permalink
Merge pull request diliuzuzhanghao#22 from wanghongdi56/master
Browse files Browse the repository at this point in the history
0226作业提交
  • Loading branch information
gaodekui authored Feb 28, 2017
2 parents 065fb29 + c2e0b7b commit 2456576
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 0 deletions.
Binary file modified group16/313001956/src/com/coding/basic/ArrayList.java
Binary file not shown.
140 changes: 140 additions & 0 deletions group16/313001956/src/com/coding/basic/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.coding.basic;

public class LinkedList implements List {

private Node head;
private int size;

public LinkedList() {

}

public void add(Object o) {
addLast(o);
}

public void add(int index, Object o) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Ô½½ç£¡");
}
if (head == null) {
head = new Node();
head.data = o;
} else {

Node n = getNodebyIndex(index);

Node newnode = new Node();
newnode.data = o;
newnode.next = n.next;
n.next = newnode;
}
size++;
}

public Object get(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Ô½½ç£¡");
}
if (head == null)
return null;
Node n = getNodebyIndex(index);
return n.data;
}

public Object remove(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Ô½½ç£¡");
}
Node n = getNodebyIndex(index - 1);
Object o = n.next.data;
n.next = n.next.next;
size--;
return o;
}

public int size() {
return size;
}

public void addFirst(Object o) {
if (head == null) {
head = new Node();
head.data = o;
} else {
Node newnode = new Node();
newnode.data = o;
newnode.next = head;
head = newnode;
}
size++;
}

public void addLast(Object o) {
if (head == null) {
head = new Node();
head.data = o;
} else {
Node n = getNodebyIndex(size);
Node newnode = new Node();
newnode.data = o;
n.next = newnode;
}
size++;
}

public Object removeFirst() {
if (head == null)
return null;

head = head.next;
size--;
return head.data;
}

public Object removeLast() {
if (head == null)
return null;
Node n = getNodebyIndex(size - 1);
Object o = n.next.data;
n.next = null;
size--;
return o;
}

public Iterator iterator() {
return null;
}

private Node getNodebyIndex(int index) {
int i = 0;
Node n = head;
while (i < index) {
n = n.next;
i++;
}
return n;
}

private static class Node {
Object data;
Node next;

Object Getdata() {
return data;
}

void Setdata(Object o) {
data = o;
}

Node Getnext() {
return next;
}

void Setnext(Node n) {
next = n;
}
}
}

24 changes: 24 additions & 0 deletions group16/313001956/src/com/coding/basic/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.coding.basic;

public class Queue {

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

public Object deQueue(){
Object o= elementData.get(0);
elementData.removeFirst();
return o;
}


public boolean isEmpty(){
return elementData.size()==0;
}

public int size(){
return elementData.size();
}
}
37 changes: 37 additions & 0 deletions group16/313001956/src/com/coding/basic/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.coding.basic;

public class Stack {
private ArrayList elementData = new ArrayList();

public Stack() {
// TODO Auto-generated constructor stub
}

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

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

public Object peek() {
int size = elementData.size();
Object o = elementData.get(size - 1);

return o;
}

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

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

0 comments on commit 2456576

Please sign in to comment.