Introduction
Today we will learn a little more complicated LinkedList: DoublyLinkedList.
Compared with LinkedList, the nodes in DoublyLinkedList besides next point to the next node, there is also a node before prev. So it is called doublyLinkedList. doublyLinkedList is a doubly linked list, we can traverse the list forward or backward.
Today we will learn the basic operations and concepts of doublyLinkedList.
Construction of doublyLinkedList
Like linkedList, doublyLinkedList is composed of nodes one by one. In addition to storing the data to be saved, each node also needs to store the references of the next node and the previous node.
doublyLinkedList needs a head node, let's see how to build it:
public class DoublyLinkedList {
Node head; // head 节点
//Node表示的是Linked list中的节点,包含一个data数据,上一个节点和下一个节点的引用
class Node {
int data;
Node next;
Node prev;
//Node的构造函数
Node(int d) {
data = d;
}
}
}
Operation of doublyLinkedList
Next, we look at some basic operations of doublyLinkedList.
Head insertion
The logic of head insertion is to use the newly inserted node as the new head node, and point newNode.next to the original head node.
At the same time, head.prev needs to be pointed to the new insertion node.
Look at the java code:
//插入到linkedList的头部
public void push(int newData) {
//构建要插入的节点
Node newNode = new Node(newData);
//新节点的next指向现在的head节点
//新节点的prev指向null
newNode.next = head;
newNode.prev = null;
if (head != null)
head.prev = newNode;
//现有的head节点指向新的节点
head = newNode;
}
Tail insertion
The logic of tail insertion is: find the last node, point the next of the last node to the newly inserted node, and point the prev of the newly inserted node to the last node.
//新节点插入到list最后面
public void append(int newData) {
//创建新节点
Node newNode = new Node(newData);
//如果list是空,则新节点作为head节点
if (head == null) {
newNode.prev = null;
head = newNode;
return;
}
newNode.next = null;
//找到最后一个节点
Node last = head;
while (last.next != null) {
last = last.next;
}
//插入
last.next = newNode;
newNode.prev = last;
return;
}
Insert the given position
If we want to insert a node at a given position, we need to find the previous node at the insertion position, and then point the next node of the previous node to the new node. The prev of the new node points to the previous node.
At the same time, we need to point the next node of the new node to the next node, and the prev of the next node to the new node.
//插入在第几个元素之后
public void insertAfter(int index, int newData) {
Node prevNode = head;
for (int i = 1; i < index; i++) {
if (prevNode == null) {
System.out.println("输入的index有误,请重新输入");
return;
}
prevNode = prevNode.next;
}
//创建新的节点
Node newNode = new Node(newData);
//新节点的next指向prevNode的下一个节点
newNode.next = prevNode.next;
//将新节点插入在prevNode之后
prevNode.next = newNode;
//将新节点的prev指向prevNode
newNode.prev = prevNode;
//newNode的下一个节点的prev指向newNode
if (newNode.next != null)
newNode.next.prev = newNode;
}
Delete the node at the specified location
The logic of deleting a node is to find the previous node and the next node to delete the node. The next node of the previous node points to the next node, and the prev of the next node points to the previous node.
//删除特定位置的节点
void deleteNode(int index)
{
// 如果是空的,直接返回
if (head == null)
return;
// head节点
Node temp = head;
// 如果是删除head节点
if (index == 1)
{
head = temp.next;
return;
}
// 找到要删除节点的前一个节点
for (int i=1; temp!=null && i<index-1; i++)
temp = temp.next;
// 如果超出范围
if (temp == null || temp.next == null)
return;
// temp->next 是要删除的节点,删除节点
Node next = temp.next.next;
temp.next = next;
Node prev = temp.next.prev;
prev.prev = prev;
}
The code address of this article:
This article is included in http://www.flydean.com/algorithm-doubly-linked-list/
The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "Program those things", know technology, know you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。