LinkList单链表实现

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));
}
}






}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值