链表
1. 逻辑上有前后顺序
2. 不保证逻辑前后顺序的元素挨着
3. 地址{ value, next }
-
如何遍历链表
Node cur=head; //head 是第一个结点
while (cur 满足什么条件){
cur = cur.next;
} -
头插
1. 申请新结点
2. 新结点的next = 原第一个结点
3. 更新第一个结点为头结点 -
尾插
1. 链表为空,视为头插
2. 链表不为空,申请新结点;找到最后一个结点;更新最后一个结点的 next 为新结点 -
头删
更新头结点,原 head 的 next -
尾删
1. 找到倒数第二个结点,lastLast
2. lastLast . next = null ;
3. 释放原来最后一个结点(GC会负责)
子问题:如何找到倒数第二个结点
while (cur.next,next != null){}
如果表中只有一个结点,视为头删即可 -
练习代码
class Node{
public int value; //保存结点中数据
public Node next; //指向 下一个结点的引用
public Node(int value){
this.value=value;
this.next=null;
}
}
public class LinkedList{
public static void displayLinkedList(Node head){
//如何遍历一个链表
for(Node cur=head;cur!=null;cur=cur.next){
System.out.printf("(%d)-->",cur.value);
}
System.out.println("null");
}
public static Node createLinkdeList(){
Node n1=new Node(1);
Node n2=new Node(2);
Node n3=new Node(3);
Node n4=new Node(4);
Node n5=new Node(5);
n1.next=n2;
n2.next=n3;
n3.next=n4;
n4.next=n5;
n5.next=null;
return n1;
}
public static Node pushFront(Node head,int value){
//申请新结点
Node newNode=new Node(value);
//2.更新newNode的next
newNode.next=head;
//3.更新head,head=newNode, 做了没问题,但实际没产生任何影响,通常不做
return newNode;
}
public static Node pushBack(Node head,int value){
if(head==null){
//对空链表尾插
return pushFront(head,value);
}else{
//对非空链表尾插
//1.申请新结点,并且让next=null
Node newNode=new Node(value);
//2.找到当前的最后一个结点
Node last=getLast(head);
//3.让当前最后一个结点next=newNode
last.next=newNode;
return head;
}
}
public static Node getLast(Node head){
Node cur=head;
while(cur.next!=null){
cur=cur.next;
}
return cur;
}
public static Node popFront(Node head){
if(head==null){
System.out.println("参数不合法,无法删除空链表的结点");
return null;
}
return head.next;
}
public static Node popBack(Node head){
if(head==null){
System.out.println("参数不合法,无法删除空链表的结点");
return null;
}
if(head.next==null){
//链表只有一个结点,视为头删,释放最后一个结点(GC)负责
return null;
}else{
//1.找倒数第二个结点
Node lastLast=getLastLast(head);
//2.让倒数第二个的结点 next=null
lastLast.next=null;
//3.释放最后一个结点(GC)负责
return head;
}
}
private static Node getLastLast(Node head){
Node node=head;
while(node.next.next!=null){
node=node.next;
}
return node;
}
public static void main(String[] args){
Node head=createLinkdeList();
displayLinkedList(head);
head=pushFront(head,100);
displayLinkedList(head);
pushBack(head,66);
displayLinkedList(head);
head=null;
displayLinkedList(head);
head=pushBack(head,1);
head=pushBack(head,2);
head=pushBack(head,3);
head=pushBack(head,4);
head=pushBack(head,5);
displayLinkedList(head);
head=popFront(head);
head=popFront(head);
head=popFront(head);
displayLinkedList(head);
head=popBack(head);
displayLinkedList(head);
head=popBack(head);
displayLinkedList(head);
}
}
- 结果
D:\JavaCode>java LinkedList
(1)-->(2)-->(3)-->(4)-->(5)-->null
(100)-->(1)-->(2)-->(3)-->(4)-->(5)-->null
(100)-->(1)-->(2)-->(3)-->(4)-->(5)-->(66)-->null
null
(1)-->(2)-->(3)-->(4)-->(5)-->null
(4)-->(5)-->null
(4)-->null
null