数据结构双链表的模拟实现

package com.panjiahao;
public class DoubleLinkLst {
    private static class linkNode{
        int value;
        linkNode prev;
        linkNode next;

        public linkNode(int value) {
            this.value = value;
        }
    }
    linkNode head;
    linkNode tail;
    //头插法
    public void addFirst(int data){
        linkNode node=new linkNode(data);
        if(head==null){
            head=tail=node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }
    //尾插法
    public void addLast(int data){
        linkNode node=new linkNode(data);
        if(head==null){
            head=tail=node;
        }else {
            tail.next=node;
            node.prev=tail;
            tail = node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
      checkIndex(index);

        if (index == 0) {//插在头节点的前面
            addFirst(data);
        }else if(index==size()){//插在尾节点的后面
            addLast(data);
        }else{//插在中间
            //先将结点移动到要插入的位置前面
             linkNode current=head;
            while(index>0){
                current=current.next;
                index--;
            }
            //再将新结点插入到链表中
            linkNode node=new linkNode(data);
            current.prev.next=node;
            node.next=current;
            node.prev=current.prev;
            current.prev=node;
        }
    }
    private void checkIndex(int index){
        if(index<0||index>size())
            throw new ArrayIndexOutOfBoundsException("index索引不合法index="+index);
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(head==null){
            return false;
        }
        linkNode current=head;
        while(current!=null){
            if(current.value==key){
                return true;
            }
            current=current.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (contains(key)) {//先判断链表中是否含有元素key
            linkNode current = head;
            while (current != null) {
                if (current.value == key) {
                    //判断节点的位置
                    if (current == head) {//删除的结点是头结点
                        head = head.next;
                        if(head==null){
                            tail=null;
                            return;
                        }
                        head.prev.next = null;
                        head.prev = null;
                    }
                    else if (current == tail) {//删除的结点是为节点
                        tail = tail.prev;
                        tail.next.prev = null;
                        tail.next = null;
                    }
                    else {//删除的结点在链表的中间
                            current.prev.next=current.next;
                            current.next.prev=current.prev;
                            current.prev=current.next=null;
                    }
                 break;
                }
                 current=current.next;//继续循环遍历
            }//while循环结束
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head==null){
            return;
        }
        //先处理头尾结点
      while(true){
          if(head.value==key){
              head=head.next;
              if(head!=null) {
                  head.prev.next = null;
                  head.prev = null;
              }else{
                  break;
              }

          }else if(tail.value==key){
              tail=tail.prev;
              tail.next.prev=null;
              tail.next=null;
          }else{
              break;
          }
      }
      linkNode current =head;
      int count=0;
      while(current!=null){
          count++;
          current=current.next;
      }
      if(count>=3){
          linkNode tmp=head.next;
          linkNode nextnode=tmp;
                while(nextnode.next!=null){
                    nextnode=nextnode.next;
                    if(tmp.value==key){
                        tmp.prev.next=tmp.next;
                        tmp.next.prev=tmp.prev;
                        tmp.next=tmp.prev=null;
                    }
                    tmp=nextnode;
                }
      }
    }
    //得到单链表的长度
    public int size(){
        linkNode current=head;
        int count =0;
        while(current!=null){
            count++;
            current=current.next;
        }
        return count;
    }
    public void clear(){
        linkNode current=head;
        while(current!=null){
            head=head.next;
            if(head!=null) {
                head.prev.next = null;
                head.prev = null;
            }
            current=current.next;
        }
        head=tail=null;
    }
    public void display(){
        StringBuilder sb=new StringBuilder();
        sb.append("[");
        linkNode current=head;
        while(current!=null){
            sb.append(current.value);
            if(current.next!=null){
                sb.append(",");
            }
            current=current.next;
        }
        sb.append("]");
        System.out.println(sb);
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jhpan666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值