单向链表与双向链表的区别:
(1)单向链表,查找的方向只能是一个方向,而双向链表表示可以向前或向后查找
(2)单向链表不能自我删除,需要利用辅助节点找到需要删除节点的前一个节点,而双向链表,则可以实现自我删除。
双向链表的遍历:
遍历方法与单向链表一样,只是可以向前,也可以向后查找
//向后遍历
//遍历
public void printList() {
Node temp = head.next;
while(temp != null) {
System.out.println(temp);
temp = temp.next;
}
}
双向链表的添加(默认添加到双向链表的最后):
1.先利用辅助指针temp找到双向链表的最后一个节点
2.temp.next = newNode;
3.newNode.pre = temp;
//添加数据到链表尾
public void add(Node newNode){
if(head.next == null) {
head.next = newNode;
return;
}
Node temp = head;
while(temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
newNode.pre = temp;
}
按照某一个属性的顺序排序:
//按照顺序添加数据
public void addByOrder(Node newNode) {
Node temp = head;
boolean flag = false;
while(true) {
if(temp.next != null) {
if(temp.next.item > newNode.item) {//找到添加的位置
break;
}else if(temp.next.item == newNode.item) {
flag = true;
break;
}
}else {
temp.next = newNode;
newNode.pre = temp;
return;
}
temp = temp.next;
}
if(flag) {
System.out.println("该节点已经存在!");
}else {
newNode.pre = temp;
newNode.next = temp.next;
temp.next.pre = newNode;
temp.next = newNode;
}
}
双向链表修改:
思路和原理与单向差不多
//修改链表
public void updata(int item,int newItem) {
if(head.next == null) {
System.out.println("链表为空,不能修改");
return;
}
Node temp = head.next;
boolean flag = false;
while(true) {
if(temp.item == item) {
flag = true;
temp.item = newItem;
break;
}
temp = temp.next;
}
if(flag) {
System.out.println("修改成功!!!");
}else {
System.out.println("没有找到该节点");
}
}
双向链表的删除(可以实现自我删除):
1.利用辅助节点temp找到这个节点
2.temp.pre.next = temp.next;
3.temp.next.pre = temp.pre;
//删除链表
public void del(int item) {
if(head.next == null) {
System.out.println("该链表为空,无法删除");
return;
}
Node temp = head;
boolean flag = false;
while(true) {
if(temp.item == item) {
flag = true;
break;
}
if(temp.next == null) {
break;
}
temp = temp.next;
}
if(flag) {
temp.next.pre = temp.pre;
temp.pre.next = temp.next;
}else {
System.out.println("没有找到此节点");
}
}
总代码:
public class DoubleListTest {
public static void main(String[] args) {
Node node1 = new Node(1);
Node node2 = new Node(4);
Node node3 = new Node(7);
Node node4 = new Node(3);
Node node5 = new Node(2);
Node node6 = new Node(5);
DoubleList doubleList = new DoubleList();
//增加
doubleList.add(node1);
doubleList.addByOrder(node2);
doubleList.addByOrder(node3);
doubleList.addByOrder(node4);
doubleList.addByOrder(node5);
doubleList.addByOrder(node6);
//遍历
System.out.println("遍历链表:");
doubleList.printList();
//修改
System.out.println("修改链表后:");
doubleList.updata(4, 11);
doubleList.printList();
System.out.println("删除某个节点后:");
//删除
doubleList.del(11);
doubleList.printList();
}
}
class DoubleList{
//先定义一个头节点不要具体数据
Node head = new Node(0, null, null);
//添加数据到链表尾
public void add(Node newNode){
if(head.next == null) {
head.next = newNode;
return;
}
Node temp = head;
while(temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
newNode.pre = temp;
}
//按照顺序添加数据
public void addByOrder(Node newNode) {
Node temp = head;
boolean flag = false;
while(true) {
if(temp.next != null) {
if(temp.next.item > newNode.item) {//找到添加的位置
break;
}else if(temp.next.item == newNode.item) {
flag = true;
break;
}
}else {
temp.next = newNode;
newNode.pre = temp;
return;
}
temp = temp.next;
}
if(flag) {
System.out.println("该节点已经存在!");
}else {
newNode.pre = temp;
newNode.next = temp.next;
temp.next.pre = newNode;
temp.next = newNode;
}
}
//修改链表
public void updata(int item,int newItem) {
if(head.next == null) {
System.out.println("链表为空,不能修改");
return;
}
Node temp = head.next;
boolean flag = false;
while(true) {
if(temp.item == item) {
flag = true;
temp.item = newItem;
break;
}
temp = temp.next;
}
if(flag) {
System.out.println("修改成功!!!");
}else {
System.out.println("没有找到该节点");
}
}
//删除链表
public void del(int item) {
if(head.next == null) {
System.out.println("该链表为空,无法删除");
return;
}
Node temp = head;
boolean flag = false;
while(true) {
if(temp.item == item) {
flag = true;
break;
}
if(temp.next == null) {
break;
}
temp = temp.next;
}
if(flag) {
temp.next.pre = temp.pre;
temp.pre.next = temp.next;
}else {
System.out.println("没有找到此节点");
}
}
//遍历
public void printList() {
Node temp = head.next;
while(temp != null) {
System.out.println(temp);
temp = temp.next;
}
}
}
//定义一个节点类
class Node{
int item;
Node next;//指向下一个节点
Node pre;//指向上一个节点
public Node(int item) {
super();
this.item = item;
}
public Node() {
super();
}
public Node(int item, Node next, Node pre) {
super();
this.item = item;
this.next = next;
this.pre = pre;
}
@Override
public String toString() {
return "Node [item=" + item + "]";
}
}