十双向链表的java实现
Posted Heaven of freedom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了十双向链表的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;
}
}
}
以上是关于十双向链表的java实现的主要内容,如果未能解决你的问题,请参考以下文章