首页 > 编程语言 > 详细

十二、双向链表的java实现

时间:2018-02-24 12:51:44      阅读:188      评论:0      收藏:0      [点我收藏+]

原理图:

 

技术分享图片

 

运行结果:

技术分享图片

 

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实现

原文:https://www.cnblogs.com/fyz666/p/8464980.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!