链表的基本操作

package com.linkedlist.exe;

/**
 * @program: LinkdeList
 * @Date: *****
 * @Author: Kyrie
 * @Description: 链表的基本操作
 */
public class Demo1_LinkedList {

    private Node head;  //指向头节点“指针”,其实是头节点的对象引用
    private Node last;  //指向尾节点“指针”,其实是尾节点的对象引用
    private int size;   //链表的实际节点数,与容量含义不同

    public void init_list(){
        /**
         * @Description 初始化链表
         * @Author Kyrie
         * @Date 2019/5/11
         * @Param []
         * @Return void
         */
        size = 0;
        head = last;
        last = null;
    }
    //插入节点,若链表为空则插入至头节点
    public void insert(int data, int index) throws IndexOutOfBoundsException{
        if(index < 0 || index > size){
            throw new IndexOutOfBoundsException("索引越界异常");
        }
        Node new_node = new Node(data);
        if(size == 0){  //
            head = new_node;
            last = new_node;
        }else if(size == index){
            last.next = new_node;
            last = new_node;

        }else{
            //getNode(index)
            Node pre_Node = getNode(index-1);
            new_node.next = pre_Node.next;
            pre_Node.next = new_node;
        }
        size++;
    }
    //获取指定位置的节点
    public Node getNode(int index){
        Node temp = head;
        for (int i = 0; i < index ; i++) {
            temp = temp.next;
        }
        return temp;
    }
    //遍历链表
    public void printList(){
        Node temp = head;
        while (temp != null){
            System.out.println(temp.data);
            temp = temp.next;
        }
    }
    //删除指定节点
    public void deleteNode(int index){
        if(index < 0 || index >= size){
            throw new IndexOutOfBoundsException("越界异常");
        }

        Node pre_Node = getNode(index-1);
        Node del_Node = getNode(index);
        if(index == 0){
            head = del_Node.next;
            //del_Node.next = null;
        }else if(index == size -1){
            last = pre_Node;
            pre_Node.next = null;

        }else{
            pre_Node.next = del_Node.next;

        }
        size--;
    }

    public static void main(String[] args) throws Exception {
        Demo1_LinkedList list = new Demo1_LinkedList();

        for (int i = 0; i < 5; i++) {
            list.insert(12+i, i);
        }

        list.printList();
        System.out.println("--------------------------");
        list.insert(66, 3);
        list.printList();
        System.out.println("--------------------------");
        list.deleteNode(5);
        list.printList();


    }
}

class Node{
    public int data;
    public Node next;

    public Node() {
    }

    public Node(int data) {
        this.data = data;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值