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

    原理图:

    运行结果:

    Node代码:

    public class Node {
    int data;
    Node next;
    Node previous; //前向指针

    public Node(int value) {
    // TODO Auto-generated constructor stub
    this.data = value;
    }

    public void display()
    {
    System.out.print(data+ " ");
    }
    }

    doubleLinklist:

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

    public doubleLinklist() {
    // 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.previous = node;
    first = node;

    }

    }

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

    public void deleteFirst() //删除头结点
    {

    if(first.next ==null)
    {
    first =null;
    last=null;
    }
    else
    {
    Node tmp = first.next;
    tmp.previous =null;
    first= tmp;
    if(first == null)
    last =null;
    }

    }

    public void deleteLast() //从尾部删除
    {
    if(last.previous == null)
    {
    last =null;
    first=null;
    }
    else
    {
    last.previous.next =null;
    last = last.previous;
    }

    }


    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;

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




    }

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


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


    }

  • 相关阅读:
    xtrabackup备份原理
    四“当”
    MySQL BinLog Server 搭建实战
    mysqldump 原理
    MGR测试及搭建
    自动化测试-12.selenium的弹出框处理
    自动化测试-11.selenium的下拉框处理类Select
    自动化测试-10.selenium的iframe与Frame
    自动化测试-9.selenium多窗口句柄的切换
    自动化测试-8.selenium操作元素之键盘和鼠标事件
  • 原文地址:https://www.cnblogs.com/fyz666/p/8464980.html
Copyright © 2011-2022 走看看