forked from diliuzuzhanghao/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 0
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 diliuzuzhanghao#22 from wanghongdi56/master
0226作业提交
- Loading branch information
Showing
4 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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,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; | ||
} | ||
} | ||
} | ||
|
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,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(); | ||
} | ||
} |
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,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(); | ||
} | ||
} |