代码改变世界

数据结构学习--Java双端链表、双向链表

2019-11-06 16:52  小花儿鹿  阅读(185)  评论(0编辑  收藏  举报

1、双端链表

链表中保存着对最后一个链节点引用的链表

2、从头部进行插入

要对链表进行判断,如果为空则设置尾节点为新添加的节点

3、从尾部进行插入

如果链表为空,则直接设置头节点为新添加的节点,否则设置尾节点的后一个节点为新添加的节点

4、从头部进行删除

判断头节点是否有下一个节点,如果没有则设置为节点为null

 

 代码实现

package com.example.deer;

public class FirstLastLinkList {
//头节点
private Node first;
//尾节点
private Node last;

public FirstLastLinkList(){
this.first = null;
this.last = null;
}
/**
* 插入节点,在头节点后面进行插入
*/
public void insertFirst(long value){
Node node = new Node(value);
if(isEmpty()){
last = node;
}
node.next = first;
first = node;
}
/**
* 插入节点,从尾节点进行插入
*/
public void insertLast(long value){
Node node = new Node(value);
if(isEmpty()){
first = node;
}else{
last.next = node;
}
last = node;
}
/**
* 删除节点
*/
public Node deleteFirst(){
Node tmp = first;
if(first.next == null){
last = null;
}
first = tmp.next;
return tmp;
}
/**
* 显示方法
*/
public void display(){
Node current = first;
while (current != null){
current.display();
current = current.next;
}
System.out.println();
}
/**
* 查找方法
*/
public Node find(long value){
Node current = first;
while (current.data != value){
if(current.next == null){
return null;
}
current = current.next;
}
return current;
}
/**
* 删除方法
*/
public Node delete(long value){
Node current = first;
Node pervious = first;
while (current.data != value){
if(current.next == null){
return null;
}
pervious = current;
current = current.next;
}
if(current == first){
first = first.next;
}else{
pervious.next = current.next;
}
return current;
}
/**
* 判断是否为空
*/
public boolean isEmpty(){
return (first == null);
}
}

1、双向链表

每个节点除了保持对下一个节点的引用,同时还保存着对前一个节点的引用

2、从头部进行插入

要对链表进行判断,如果为空则设置尾节点为新添加的节点;如果不为空,还需要设置头节点的前一个节点为新添加的节点。

3、从尾部进行插入

如果链表为空,则直接设置头节点为新添加的节点,否则设置尾节点的后一个节点为新添加的节点。同时设置新添加的节点的前一个节点为尾节点。

4、从头部进行删除

判断头节点是否有下一个节点,如果没有则设置节点为null,否则设置头节点的下一个节点的previous为null。

5、从尾部进行删除

如果头节点后没有其他节点,则设置尾节点为null。否则设置尾节点前一个节点的next为null。设置尾节点为其前一个节点。

6、删除方法

不需要再使用临时变量。

 

Node.java

package com.example.deer;

public class Node {
//数据域
public long data;
//后一个节点
public Node next;
//前一个节点
public Node previous;

public Node(long value){
this.data = value;
}

/**
* 显示方法
*/
public void display(){
System.out.print(data + " ");
}
}
DoubleLinkList.java
package com.example.deer;

public class DoubleLinkList {
//头节点
private Node first;
//尾节点
private Node last;

public DoubleLinkList(){
this.first = null;
this.last = null;
}
/**
* 插入节点,在头节点后面进行插入
*/
public void insertFirst(long value){
Node node = new Node(value);
if(isEmpty()){
last = node;
} else {
first.previous = node;
}
node.next = first;
first = node;
}
/**
* 插入节点,从尾节点进行插入
*/
public void insertLast(long value){
Node node = new Node(value);
if(isEmpty()){
first = node;
}else{
last.next = node;
node.previous = last;
}
last = node;
}
/**
* 删除节点,从头部进行删除
*/
public Node deleteFirst(){
Node tmp = first;
if(first.next == null){
last = null;
} else {
first.next.previous = null;
}
first = tmp.next;
return tmp;
}
/**
* 删除节点,从尾部进行删除
*/
public Node deleteLast(){
Node tmp = last;
if(first.next == null){
first = null;
} else {
last.previous.next = null;
}
last = last.previous;
return last;
}
/**
* 显示方法
*/
public void display(){
Node current = first;
while (current != null){
current.display();
current = current.next;
}
System.out.println();
}
/**
* 查找方法
*/
public Node find(long value){
Node current = first;
while (current.data != value){
if(current.next == null){
return null;
}
current = current.next;
}
return current;
}
/**
* 删除方法
*/
public Node delete(long value){
Node current = first;
while (current.data != value){
if(current.next == null){
return null;
}
current = current.next;
}
if(current == first){
first = first.next;
}else{
current.previous.next = current.next;
}
return current;
}
/**
* 判断是否为空
*/
public boolean isEmpty(){
return (first == null);
}
}