看源码和视频自己写了写,当然和源码差了n个数量级. 也算复习了下链表
SetLinkedList类
package cn.hjy.collection;
import java.util.List;
/**
* @author 543363559@qq.com
* @date 2017年4月30日 下午4:47:34
*/
public class SetLinkedList {
private Node first;
private Node last;
private int size;
/**
*
* @Description: 添加结点 建first or 往last.next添加
* @param @param obj
* @param @return
* @return boolean
*/
public boolean add(Object obj){
Node n = new Node();
if(first == null){
n.setPrevious(null);
n.setObj(obj);
n.setNext(null);
first = n;
last = n;
} else {
//直接往last结点后增加新的结点
n.setPrevious(last);
n.setObj(obj);
n.setNext(null);
last.setNext(n);
last = n;
}
size++;
return true;
}
/**
*
* @Description: 返回链表大小
* @param @return
* @return int
*/
public int size(){
return size;
}
/**
*
* @Description: 删除指定索引位置对象
* @param @param index
* @return void
*/
public void remove(int index){
//得到结点
Node temp = node(index);
if (temp != null) {
Node up = temp.getPrevious();
Node down = temp.getNext();
up.setNext(down);
if (down != null) {
down.setPrevious(up);
}
}
size--;
}
/**
*
* @Description: 获得想要位置的结点
* @param @param index
* @param @return
* @return Node
*/
public Node node(int index) {
Node temp = null;
if (first != null) {
temp = first;
for (int i = 0; i < index; i++) {
temp = temp.getNext();
}
}
return temp;
}
/**
*
* @Description: 得到index的obj值
* @param @param index
* @param @return
* @return Object
*/
public Object get(int index){
rangeCheck(index); //越界处理
Node temp = node(index);
if (temp != null) {
return temp.getObj();
} else {
return null;
}
}
/**
*
* @Description: 在链表index位置添加结点
* @param @param index
* @param @param obj
* @return void
*/
public void add(int index,Object obj) {
Node temp = node(index);
Node newNode = new Node();
newNode.setObj(obj);
if (temp != null) {
Node up = temp.getPrevious();
up.setNext(newNode);
newNode.setPrevious(up);
newNode.setNext(temp);
temp.setPrevious(newNode);
}
size++;
}
/**
*
* @Description: 修改链表中 index的obj
* @param @param index
* @param @param obj
* @return void
*/
public void set(int index,Object obj) {
Node temp = node(index);
if (temp != null) {
temp.setObj(obj);
}
}
/**
*
* @Description: 判断index是否超出链表范围,超出则抛出异常
* @param @param index
* @return void
*/
private void rangeCheck(int index) {
if (index < 0 || index >= size) {
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
* @Description: 遍历显示
* @param
* @return void
*/
public void show() {
Node temp = new Node();
if (first != null) {
temp = first;
while (temp != null) {
System.out.print(temp.getObj() + " ");
temp = temp.getNext();
}
}
}
public SetLinkedList() {
}
public static void main(String[] args) {
SetLinkedList list = new SetLinkedList();
list.add("aaa");
list.add("bbb");
list.add(1, "www");
System.out.println(list.get(1)); //www
list.set(1, "qqq");
System.out.println(list.get(1)); //qqq
list.remove(1);
System.out.println(list.get(1)); //bbb
list.show(); //aaa bbb
System.out.println(list.size()); //2
}
}
Node类
package cn.hjy.collection;
class Node {
private Node previous;
private Object obj;
private Node next;
Node(){
}
public Node(Node previous, Object obj, Node next) {
super();
this.previous = previous;
this.obj = obj;
this.next = next;
}
public Node getPrevious() {
return previous;
}
public void setPrevious(Node previous) {
this.previous = previous;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}