java实现链表的头插尾插及头删尾删

java

链表的头插尾插

class Node{
    int value;
    Node next;
public Node(int value){
    this.value=value;
    this.next=null;
}}

头插

public static Node pushFront(Node head,int value){
         Node node = new Node(value);
         node.next=head;
         return node;
     }

尾插

public static Node pushBack(Node head,int value){
         Node node =new Node(value);
         while (head==null){
             node.next=head;
             return node;
         }
         Node last=getLast(head);
         last.next=node;
         return head;
     }

头删

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){
             return null;
         }else{
             Node lastLast = getLastLast(head);
             lastLast.next = null;
         }
         return head;
     }

getLast()方法,获取最后一个节点

public static Node getLast(Node head){
         Node last = head;
         while(last.next!=null){
             last=last.next;
         }
         return last;
     }

getLastLast()获取倒数第二个结点

 public static Node getLastLast(Node head){
         Node cur = head;
         while(cur.next.next!=null){
             cur=cur.next;
         }
         return cur;
     }

disPlay()便利打印链表

public static void disPlay(Node head){
         Node cur = head;
         while(cur!=null){
                 System.out.printf("(%d)-->", cur.value);
                 cur=cur.next;
         }
         System.out.println("null");
         /*for(Node cur = head;cur!=null;cur=cur.next){
             System.out.printf("(%d) --> ", cur.value);
         }
         System.out.println("null");*/
     }

举例

public static void main(String args[]){
             Node head = creatLinkList();
             disPlay(head);
         head = pushFront(head, 888);
         disPlay(head);
         pushBack(head, 666);
         disPlay(head);
         head = null;
         disPlay(head);
         head = pushBack(head, 1);
         head = pushBack(head, 2);
         head = pushBack(head, 3);
         head = pushBack(head, 4);
         head = pushBack(head, 5);
         disPlay(head);
         head = popFront(head);
         head = popFront(head);
         disPlay(head);
         head = popBack(head);
         disPlay(head);
         head = popBack(head);
         disPlay(head);
     }

完整代码

class Node{
    int value;
    Node next;
public Node(int value){
    this.value=value;
    this.next=null;
}}
public class NodeTest {
     public static Node pushFront(Node head,int value){
         Node node = new Node(value);
         node.next=head;
         return node;
     }
     public static Node pushBack(Node head,int value){
         Node node =new Node(value);
         while (head==null){
             node.next=head;
             return node;
         }
         Node last=getLast(head);
         last.next=node;
         return head;
     }
     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){
            return null;
        }else{
            Node lastLast = getLastLast(head);
            lastLast.next = null;
        }
        return head;
    }
     public static Node getLast(Node head){
         Node last = head;
         while(last.next!=null){
             last=last.next;
         }
         return last;
     }
     public static Node getLastLast(Node head){
         Node cur = head;
         while(cur.next.next!=null){
             cur=cur.next;
         }
         return cur;
     }
     public static void disPlay(Node head){
         Node cur = head;
         while(cur!=null){
                 System.out.printf("(%d)-->", cur.value);
                 cur=cur.next;
         }
         System.out.println("null");
         /*for(Node cur = head;cur!=null;cur=cur.next){
             System.out.printf("(%d) --> ", cur.value);
         }
         System.out.println("null");*/
     }
     public static Node creatLinkList(){
         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 void main(String args[]){
             Node head = creatLinkList();
             disPlay(head);
         head = pushFront(head, 888);
         disPlay(head);
         pushBack(head, 666);
         disPlay(head);
         head = null;
         disPlay(head);
         head = pushBack(head, 1);
         head = pushBack(head, 2);
         head = pushBack(head, 3);
         head = pushBack(head, 4);
         head = pushBack(head, 5);
         disPlay(head);
         head = popFront(head);
         head = popFront(head);
         disPlay(head);
         head = popBack(head);
         disPlay(head);
         head = popBack(head);
         disPlay(head);
     }
}

运行结果
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值