单向单链表的java实现

单向表

package datamemory.link;

//单链表
public class ObjectLink
{
    private int size;// 链表长度
    
    private ObjectNode head;// 头节点
    
    public void add(Object obj)
    {
        ObjectNode node = new ObjectNode(obj, null);
        if (head == null)
        {
            head = node;
        }
        else
        {
            ObjectNode temp = head;
            while (temp.getNext() != null)
            {
                temp = temp.getNext(); // 移动到最后的节点
                
            }
            temp.addNodeAfter(obj);
            size++;// 表的长度+1;
        }
    }
    
    public int getSize()
    {
        return size;
    }
    
    public Object get(int i)
    {
        if (i > size - 1)
        {
            return null;
        }
        ObjectNode temp = head;
        for (int k = 0; k < i; k++)
        {
            temp = temp.getNext();
        }
        return temp;
    }
    
    public int indexOf(Object obj)
    {
        int i = 0;
        ObjectNode node = new ObjectNode(obj, null);
        if (head == null)
            return -1;
        if (head.equals(node))
            return 0;
        ObjectNode temp = head;
        while (temp.getNext() != null)
        {
            i++;
            if (temp.getNext().equals(node))
            {
                return i;
            }
            temp = temp.getNext();
        }
        
        return -1;
    }
    
    public void remove(Object obj)
    {
        ObjectNode node = new ObjectNode(obj, null);
        if (head != null)
        {
            if (head.equals(node))
                head = head.getNext();// 设置第二节点为头节点
            else
            {
                ObjectNode temp = head;
                while (temp.getNext() != null)
                {
                    if (temp.getNext().equals(node))
                    {
                        temp.removeNodeAfter();// 删除该节点之后的节点
                        size--;
                        break;
                    }
                }
            }
        }
    }
    
}

   节点类:

package datamemory.link;

//单链节点
public class ObjectNode
{
    
    private Object data;
    
    private ObjectNode next;
    
    public ObjectNode(Object data, ObjectNode next)
    {
        super();
        this.data = data;
        this.next = next;
    }
    
    protected Object getData()
    {
        return data;
    }
    
    protected void setData(Object data)
    {
        this.data = data;
    }
    
    // 在改节点之后添加新节点
    public void addNodeAfter(Object obj)
    {
        ObjectNode node = this.next;
        if (node == null)// 当改节点是尾节点时,把新加的节点设置为尾节点
        {
            this.next = new ObjectNode(obj, null);
        }
        else
        // 把该节点设置为下一节点,原先的节点为新节点的下一节点
        {
            this.next = new ObjectNode(obj, node);
        }
    }
    
    public void removeNodeAfter()
    {
        if (next != null)
        {
            next = next.next;// 设置下一节点为下下个节点
        }
    }
    
    protected ObjectNode getNext()
    {
        return next;
    }
    
    protected void setNext(ObjectNode next)
    {
        this.next = next;
    }
    
    public boolean equals(Object obj)
    {
        if (obj instanceof ObjectNode)
        {
            ObjectNode node = (ObjectNode)obj;
            if (node.getData() == this.data)// 指向的对象是同一个对象
            {
                return true;
            }
        }
        return false;
    }
}

 

如果需要给这个把这个链表类修改成泛型实现,可以在类ObjectLink,ObjectNode修改为ObjectLink<E>,ObjectNode<E>,然后把传入的对象设置为E类型即可。

你可能感兴趣的:(java)