1.单链表:
何为链表?
通过地址的方式,找到数据。
比如 到银行办理业务,业务员根据票号,来找到下一个人进行办理业务。
同样地,链表的意思是,通过一个地址,找到数据。
1.1数据内容为int型的链表
public class LinkList{
public static void main(String[] args){
Node n1=new Node(9);
Node n2=new Node(8);
Node n3=new Node(7);
//追加节点
n1.append(n2);
n2.append(n3);
System.out.println(n1.next().next().getData());//7
}
}
//构建一个节点类
class Node{
//节点内容
int data;
//下一个节点
Node next;
public Node(int value){
this.data=value;
}
//为当前节点追加下一个节点
public void append(Node node){
//将当前节点赋值给currentNode
Node currentNode=this;
//循环向后找
while(true){
Node nextNode=currentNode.next;
//如果下一个节点为空,则退出循环
if(nextNode==null){
break;
}
//将下一个节点赋给当前节点
currentNode=nextNode;
}
//因为退出循环的时候,是当下一节点为空的时候,所以把需要返回的节点追加为当前节点的下一节点
currentNode.next=node;
}
//返回下一个节点
public Node next(){
return this.next;
}
//获得节点内容
public int getData(){
return this.data;
}
}
1.2数据内容为数组的链表
import java.util.Arrays;
import java.util.Set;
//import com.sun.corba.se.impl.orbutil.graph.Node;
public class LinkList{
public static void main(String[] args){
Node n1=new Node(new int[]{1,2,4,5,6});
Node n2=new Node(new int[]{7,8,9,10});
Node n3=new Node(new int[]{11,12,13,14});
n1.append(n2);
n2.append(n3);
//System.out.println(n1.next().getdata());
n1.next().getdata();//7,8,9,10
}
}
class Node{
//节点内容
int[] data;
//下一个节点
Node next;
public Node(int[] value){
this.data=value;
}
//追加节点
public void append(Node node){
Node currentNode=this;
//往后寻找
while(true){
Node nextNode=currentNode.next;
if(nextNode==null){
break;
}
// 追加下一节点为当前节点;
currentNode=nextNode;
}
//由于当前节点的后一节点为空,因此将node放到当前节点的下一节点
currentNode.next=node;
}
//返回下一个节点
public Node next(){
return this.next;
}
public void getdata(){
for(int i=0;i<data.length;i++){
System.out.print(data[i]+"\t");
}
}
}
2.删除插入单链表中的节点
删除节点:
public void remove(){
//取出下下节点
Node newNext=next.next;
//将下下节点 赋值给当前节点的下一节点
this.next=newNext;
思路:
取出当前节点的下下节点,将下下节点赋值给 当前节点的下一节点。那么原来 当前节点的下一节点就被删除了。
插入节点:
我们插入节点只能插入当前节点的下一节点,而不能直接插入给当前节点。
思路:1.先将当前节点下一节点取出 2.再将需要插入的节点赋值给当前节点的下一节点3.把之前取出的节点作为新节点的下一节点
//插入节点
public void add(node){
//取出下一节点,作为下下节点
Node nextNode=this.next;
//将需要插入的节点,作为当前节点的下一节点
this.next=node;
//将之前的下下节点作为新节点的下一节点
node.next=newNode;
}
3.循环链表
之前的单向链表是通过一个节点,找下一个节点,但是我们却不能通过其他节点来找第一个节点,那么循环链表的意思就是通过最后一个节点,来寻找到第一个节点。整个节点就串联起来了。
整个循环链表和单链很相似。只需要将下一节点变为当前节点。
import java.util.Arrays;
import java.util.Set;
/**循环链表 */
public class LinkList2{
public static void main(String[] args){
LoopNode n1=new LoopNode(1);
LoopNode n2=new LoopNode(2);
LoopNode n3=new LoopNode(3);
//追加节点
n1.add(n2);
n2.add(n3);
System.out.println(n3.next().getdata());
}
}
class LoopNode{
//节点内容
int data;
//这里就是与单链表的区别,将下一节点初始化为当前节点
LoopNode next=this;
public LoopNode(int value){
this.data=value;
}
//插入节点
public void add(LoopNode node){
//取出下一节点,作为下下节点
LoopNode nextNode=this.next;
//将需要插入的节点,作为当前节点的下一节点
this.next=node;
//将之前的下下节点作为新节点的下一节点
node.next=nextNode;
}
//返回下一个节点
public LoopNode next(){
return this.next;
}
public int getdata(){
return this.data;
}
}
4.双向循环链表
通过每个节点可以寻找到他的上一个和下一个节点。因此叫双向。这个相较于单向循环链表,就是多了一个向前的节点。
核心代码处理解:
public class DoubleLoopLinkList{
public static void main(String[] args){
DoubleNode n1=new DoubleNode(2);
DoubleNode n2=new DoubleNode(3);
DoubleNode n3=new DoubleNode(4);
n1.after(n2);
n2.after(n3);
System.out.println(n1.next().getdata());//3
System.out.println(n2.pre().getdata());//2
}
}
class DoubleNode{
//节点数据
int data;
//上一个节点
DoubleNode pre=this;
DoubleNode next=this;
public DoubleNode(int value){
this.data=value;
}
/*增加节点 双向链表核心代码*/
public void after(DoubleNode node){
//原来节点的下一节点,
DoubleNode nextNext=next;
//将新节点作为当前节点的下一节点
this.next=node;
//把新节点作为当前节点的前一节点;
node.pre=this;
//让原来的节点的下一节点作为当前节点的下一节点
node.next=nextNext;
//将原来节点的上一节点作为新节点
nextNext.pre=node;
}
//下一个节点
public DoubleNode next(){
return this.next;
}
//上一个节点
public DoubleNode pre(){
return this.pre;
}
public int getdata(){
return this.data;
}
}