foreword
In the context of the current environment, an interview is not considered a qualified interview if you don’t ask about algorithms ( 卷
), and data structures closely related to algorithms are often asked, such as 集合
, 链表
, 树
, 图
, 栈
, 堆
, 队列
, 矩阵
and so on.
Does it feel difficult as follows:
- Collection : as long as you have it
- Linked list : simple and simple
- Queues : basic operations
- Binary tree : okay
- Balanced binary tree : mediocre
- Red-black tree : a bit difficult
- Heap/Stack : Increased Difficulty
- Picture : Today is the high-end bureau
It is still a bit difficult to come down with such a set of combination punches. In this article, we will first write 简简单单
the linked list by hand. There are one-way linked list and two-way linked list in the linked list. Can you know the two-way linked list or not the one-way linked list? Go directly to the two-way linked list.
attribute definition
The attribute content of the doubly linked list must have the upper node prev
and the lower node . We use generic definitions for the attributes. The attribute content of such a doubly linked list is as follows:next
data
private class Node<T>{
private Node prev;
private T data;
private Node next;
public Node(LinkedTable.Node prev,T data,LinkedTable.Node next){
this.prev = prev;
this.data = data;
this.next = next;
}
}
The above is the structure of the storage node. The actual external class needs to set the head node and the tail node, so that you can directly choose to traverse from the beginning or from the end.
public Node headNode;
public Node tailNode;
ADD method
add
The method has no return value. If there is no return value, 有参构造函数
the attribute content of the class is empty when entering add for the first time, which is headNode
the heel above tailNode
.
The first step of add : first create a Node object according to the content of add, the previous node is the current tail node, and the next node does not
private void add(T data) {
Node node = new Node<>(tailNode,data,null);
}
The second step of add : Initialize when it is judged headNode
that both are emptytailNode
private void add(T data) {
Node node = new Node<>(tailNode,data,null);
if (headNode == null && headNode == null){
headNode = node;
tailNode = node;
}
}
The third step of add : determine whether the tail node is empty, if it is not empty, point the next of the tail node to the creation node, and replace the tail node with the current node
private void add(T data) {
Node node = new Node<>(tailNode,data,null);
if (headNode == null && headNode == null){
headNode = node;
tailNode = node;
}else{
if (tailNode != null){
tailNode.next = node;
}
tailNode = node;
}
}
Circulate the add method 100 times for verification, as shown in the figure below:
The tail node records the last 99 of the loop, and the head node records the first 0 of the loop
DELETE method
The first step of delete : define a local variable to refer to the head node, without affecting the node position of the head and tail
private void delete(T data) {
Node now = headNode;
}
The second step of delete : while
loop to determine now
whether the node is not empty
private void delete(T data) {
Node now = headNode;
while (now != null){
}
}
The third step of deletenow
: judge whether the node value is equal to the parameter value in the loop data
. If the pointer of the previous node next
points to the current next node, it means that the grandfather directly points to the grandson, and the father is deleted, and then returns directly. Otherwise, the current node points to the next node to continue the cycle.
private void delete(T data) {
Node now = headNode;
while (now != null){
if (now.data == data){
now.prev.next = now.next;
return;
}
now = now.next;
}
}
GET method
Now that the data is put, it must be taken out intact, define a get method, the code is the same as the deletion above, it is nothing more than modifying the third step
private T get(T data){
Node<T> now = headNode;
while (now != null){
if (now.data == data){
return now.data;
}
now = now.next;
}
return null;
}
SET method
set
The method is regarded as overwriting update, set the content of the specified location, this step needs to be index
marked.
private boolean set(Integer index, T data){
Node<T> now = headNode;
AtomicInteger i = new AtomicInteger(0);
while (now != null){
if (i.getAndAdd(1) == index){
now.data = data;
return true;
}
now = now.next;
}
return false;
}
verify:
First add
an Map
object and then add
a int
value, so that the first bit of the linked list is Map对象
, and then execute set方法
to modify the first bit Map对象
to int
a value8546
public static void main(String[] args) {
LinkedTable table = new LinkedTable();
HashMap hashMap = new HashMap(){
{
put("哈喽","xxx");
}};
table.add(hashMap);
table.add(1);
System.out.println(table);
table.set(0,8546);
System.out.println(table);
}
The first breakpoint : currently the first node object property is stillMap
Second breakpoint : Now the first node object property becomesInteger
The above completes a doubly linked list basedcrud