链表的插入删除

本文深入讲解链表的基本概念,包括其逻辑结构与物理地址特点。详细介绍了链表的遍历方法,以及头插、尾插、头删和尾删的具体实现步骤。通过代码示例,演示了如何创建、插入、删除链表节点,为读者提供了丰富的实践指导。

链表
 1. 逻辑上有前后顺序
 2. 不保证逻辑前后顺序的元素挨着
 3. 地址{ value, next }

  1. 如何遍历链表
       Node cur=head;  //head 是第一个结点
       while (cur 满足什么条件){
         cur = cur.next;
    }

  2. 头插
       1. 申请新结点
       2. 新结点的next = 原第一个结点
       3. 更新第一个结点为头结点

  3. 尾插
       1. 链表为空,视为头插
       2. 链表不为空,申请新结点;找到最后一个结点;更新最后一个结点的 next 为新结点

  4. 头删
      更新头结点,原 head 的 next

  5. 尾删
       1. 找到倒数第二个结点,lastLast
       2. lastLast . next = null ;
       3. 释放原来最后一个结点(GC会负责)
      子问题:如何找到倒数第二个结点
       while (cur.next,next != null){}
       如果表中只有一个结点,视为头删即可

  6. 练习代码

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);
  
 }
}
  1. 结果
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值