zoukankan      html  css  js  c++  java
  • 十一 、双端链表的java实现

    原理:

    双端链表就是在上节链表的基础上加上了一个尾结点,据说是为了可以从尾部插入,可是我没用尾结点一样可以实现,顿时感觉双端链表很是鸡肋。

    所以不多说代码奉上吧:

    public class FirstLastLinklist {
    private Node first;
    private Node last; //尾结点

    public FirstLastLinklist() {
    // TODO Auto-generated constructor stub
    first =null;
    last = null;
    }

    public void insertFirst(int value) //头部插入
    {
    Node node = new Node(value);
    if(IsEmpty())
    {
    first = node;
    last =node ;
    }
    else
    {
    node.next = first;
    first = node;

    }

    }

    public void insertLast(int value) //从尾部插入结点
    {
    Node node = new Node(value);
    if(IsEmpty())
    {
    first = node;
    last =node;
    }
    else
    {
    last.next =node;
    last = node;
    }
    }

    public void deleteFirst() //删除头结点
    {
    Node tmp = first.next;
    first= tmp;
    if(first == null)
    last =null;
    }

    public void find(int value) //查找数据
    {
    Node current = first;
    while(current.data != value)
    {
    current =current.next;
    }
    current.display();
    }

    public void findDelete(int value) //查找数据并且删除
    {
    Node current = first;
    Node pro = null;

    if(first.data ==value)
    {
    first =first.next;
    }
    else
    {
    while(current.data != value)
    {
    pro =current;
    current =current.next;
    }
    pro.next = current.next;
    }




    }

    public boolean IsEmpty() //判断链表是否为空
    {
    return (first == null);
    }


    public void display() //显示全部结点
    {
    Node current = first;
    while(current != null)
    {
    current.display();
    current =current.next;
    }
    }



    }

  • 相关阅读:
    黑客书架上的书籍(转)
    vc 得到文件后缀名(转)
    配置IIS7(转)
    vs2008 目标框架 发布遇到的问题(转)
    CListCtrl用法(转)
    T400 折腾
    VS2008和.NET Framework3.5新功能(转)
    sql 2008 ctp 安装
    关于定位lsass内存中的明文密码(转)
    NT系统下木马进程的隐藏与检测(转)
  • 原文地址:https://www.cnblogs.com/fyz666/p/8464521.html
Copyright © 2011-2022 走看看