1.封装Node实体类
package data_table.syn.com.common.util;
/**
* 简单封装Node节点
* @author Administrator
*
*/
public class Node {
private Object value;// 值
private Node next; // 下一个对象的地址值
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node() {
super();
// TODO Auto-generated constructor stub
}
public Node(Object value) {
super();
this.value = value;
}
}
2.MyLinkList的实现
package data_table.syn.com.common.util;/**
* 简单实现LinkList的增删该查
* @author Administrator
*
*/
public class MyLinkList {
private Node head=null;
private int size=0;
public int size() {
return size;
}
/**
* 添加值
* @param value
*/
public void add(Object value) {
Node newNode=new Node(value);
if (head==null) {
head=newNode;
}else {
Node temp=head;
while (temp.getNext()!=null) {
temp=temp.getNext();
}
//循环结束temp为最后node
temp.setNext(newNode);
}
size++;
}
/**
*获取指定位置的值
* @param index
* @return
*/
public Object get(int index) {
Node temp=head;
for(int i=0;i<index;i++) {
temp=temp.getNext();
}
return temp.getValue();
}
/**
* 给指定位置设置值
* @param index
* @param value
*/
public void set(int index,Object value) {
Node temp=head;
for(int i=0;i<index;i++) {
temp=temp.getNext();
}
temp.setValue(value);
}
/**
* 清空数据
*/
public void clear() {
size=0;
head=null;
}
/**
* 删除指定位置值
* @param index
*/
public void removeAt(int index) {
Node temp=head;
if (index==0) {
head=temp.getNext();
}else {
for(int i=0;i<index-1;i++) {
temp=temp.getNext();
}
temp.setNext(temp.getNext().getNext());
}
size--;
}
public void toStrings() {
for(int i=0;i<size;i++) {
System.out.println(get(i));
}
}
}