Java单链表

Java单链表

什么是链表
链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点都包括两部分:一是数据域,用来存储元素数值数据,另一个是存储直接后继结点地址的指针域,该指针一般称为next,用来指向下一个结点的位置。由于下一个结点也是链表类型,所以next的指针也要定义为链表类型。

链表概念及特点
头指针:是指向链表中第一个结点的指针
首元结点:是指链表中存储的第一个数据元素的结点
头结点:是在链表的首元结点之前附设的一个结点
单链表是较为简单的链表,单链表的指针指向同一方向的下一个元素。在单链表中,有两个特殊的节点,头节点和尾节点。头节点(也叫根节点)是单链表中的第一个节点,是单链表中的关键节点。通过头节点,我们可以将指针指向头节点,遍历整个单链表,来实现增删改查等操作。这样操作可以防止链表为空(链表若为空,则头节点指针域为null),并且可以保持对单链表操作的统一性(从头节点开始遍历),简化操作减少bug。尾节点是单链表中的最后一个节点,它的指针指向一个空的地址null(没有下一个节点了)。在单链表的操作中,我们可以通过定义尾节点的指针来实现不同的方法。
在这里插入图片描述
下面用java实现链表的各种方法

创建一个链表

 public LinkedList create()
    {
        LinkedList node1=new LinkedList(11);
        LinkedList node2=new LinkedList(22);
        LinkedList node3=new LinkedList(33);
        LinkedList node4=new LinkedList(44);
        LinkedList node5=new LinkedList(55);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;
        head=node1;
        return head;
    }

头插法

public void addFirst(int data)
    {
        LinkedList node=new LinkedList(data);
        if(head==null)
        {
            head=node;
            return;
        }
        node.next=head;
        head=node;
    }

尾插法

    public void addLast(int data)
    {
        LinkedList node=new LinkedList(data);
        if(head==null)
        {
            head=node;
            return;
        }
        LinkedList prev=head;
        LinkedList cur=head.next;
        while(cur!=null)
        {
            prev=cur;
            cur=cur.next;
        }
        prev.next=node;
    }

在指定下标插入

 public void addIndex(int index,int data)
    {
        if(index<0||index>=size())
        {
            throw new OutExpection("INDEX越界了");
        }
        if(index==0||head==null)
        {
            addFirst(data);
            return;
        }
        if(index==size())
        {
            addLast(data);
            return;
        }
        LinkedList node=new LinkedList(data);
        LinkedList cur=head;
        LinkedList prev=null;
        while(index!=0)
        {
            prev=cur;
            cur=cur.next;
            index--;
        }
        node.next=cur;
        prev.next=node;
    }

是否包含

 public boolean contains(int key)
    {
        LinkedList cur=head;
        while(cur!=null)
        {
            if(cur.val==key)
            {
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

逆转链表

public void reverseList(LinkedList head)
    {
        LinkedList top=head;
        LinkedList next1=head.next;
        LinkedList next2=next1.next;
        top.next=null;
        while(next1!=null)
        {
            next1.next=top;
            top=next1;
            next1=next2;
            if(next2!=null)
            {
                next2=next2.next;
            }

        }
        LinkedList Top=top;
        while(Top!=null)
        {
            System.out.print(Top.val+" ");
            Top=Top.next;
        }
    }

移除第一个出现的元素

 public void remove(int key)
    {
        if(head==null)
        {
            throw new HeadEmpeyExpection("头是空的,没有要删除的值");
        }
        if(head.val==key)
        {
            head=head.next;
            return;
        }
        LinkedList prev=head;
        LinkedList cur=head.next;
        while(cur!=null)
        {
            if(cur.val==key)
            {
                prev.next=cur.next;
                return;
            }
            prev=cur;
            cur=cur.next;
        }
    }

移除所有元素

public void removeAllKey(int key)
    {
        if(head==null)
        {
            throw new HeadEmpeyExpection("头是空的,没有要删除的值");
        }
        LinkedList prev=head;
        LinkedList cur=prev.next;
        while(cur!=null)
        {
            if(cur.val==key)
            {
                prev.next=cur.next;
                cur=cur.next;
            }
            else
            {
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==key)
        {
            head=head.next;
        }
    }

计算链表大小

 public int size()
    {
        LinkedList cur=head;
        int count=0;
        while(cur!=null)
        {
            count++;
            cur=cur.next;
        }
        return count;
    }

清楚链表

public void clear()
    {
        while(head!=null)
        {
            LinkedList next=head.next;
            head=null;
            head=next;
        }
    }

打印链表

public void display()
    {
        LinkedList cur=head;
        while(cur!=null)
        {
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }

下面是所有代码

public class LinkList
{
    static class LinkedList
    {
        int val;
        LinkedList next;
        public LinkedList(int val)
        {
            this.val=val;
        }
    }
    LinkedList head=null;
    public LinkedList create()
    {
        LinkedList node1=new LinkedList(11);
        LinkedList node2=new LinkedList(22);
        LinkedList node3=new LinkedList(33);
        LinkedList node4=new LinkedList(44);
        LinkedList node5=new LinkedList(55);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;
        head=node1;
        return head;
    }
    public void addFirst(int data)
    {
        LinkedList node=new LinkedList(data);
        if(head==null)
        {
            head=node;
            return;
        }
        node.next=head;
        head=node;
    }
    public void addLast(int data)
    {
        LinkedList node=new LinkedList(data);
        if(head==null)
        {
            head=node;
            return;
        }
        LinkedList prev=head;
        LinkedList cur=head.next;
        while(cur!=null)
        {
            prev=cur;
            cur=cur.next;
        }
        prev.next=node;
    }
    public void addIndex(int index,int data)
    {
        if(index<0||index>=size())
        {
            throw new OutExpection("INDEX越界了");
        }
        if(index==0||head==null)
        {
            addFirst(data);
            return;
        }
        if(index==size())
        {
            addLast(data);
            return;
        }
        LinkedList node=new LinkedList(data);
        LinkedList cur=head;
        LinkedList prev=null;
        while(index!=0)
        {
            prev=cur;
            cur=cur.next;
            index--;
        }
        node.next=cur;
        prev.next=node;
    }
    public boolean contains(int key)
    {
        LinkedList cur=head;
        while(cur!=null)
        {
            if(cur.val==key)
            {
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
   public void reverseList(LinkedList head)
    {
        LinkedList top=head;
        LinkedList next1=head.next;
        LinkedList next2=next1.next;
        top.next=null;
        while(next1!=null)
        {
            next1.next=top;
            top=next1;
            next1=next2;
            if(next2!=null)
            {
                next2=next2.next;
            }

        }
        LinkedList Top=top;
        while(Top!=null)
        {
            System.out.print(Top.val+" ");
            Top=Top.next;
        }
    }
    public void remove(int key)
    {
        if(head==null)
        {
            throw new HeadEmpeyExpection("头是空的,没有要删除的值");
        }
        if(head.val==key)
        {
            head=head.next;
            return;
        }
        LinkedList prev=head;
        LinkedList cur=head.next;
        while(cur!=null)
        {
            if(cur.val==key)
            {
                prev.next=cur.next;
                return;
            }
            prev=cur;
            cur=cur.next;
        }
    }
    public void removeAllKey(int key)
    {
        if(head==null)
        {
            throw new HeadEmpeyExpection("头是空的,没有要删除的值");
        }
        LinkedList prev=head;
        LinkedList cur=prev.next;
        while(cur!=null)
        {
            if(cur.val==key)
            {
                prev.next=cur.next;
                cur=cur.next;
            }
            else
            {
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==key)
        {
            head=head.next;
        }
    }
    public int size()
    {
        LinkedList cur=head;
        int count=0;
        while(cur!=null)
        {
            count++;
            cur=cur.next;
        }
        return count;
    }
    public void clear()
    {
        while(head!=null)
        {
            LinkedList next=head.next;
            head=null;
            head=next;
        }
    }
    public void display()
    {
        LinkedList cur=head;
        while(cur!=null)
        {
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值