实现单向链表(不带傀儡节点)代码.

功能介绍

//头插法
public void addFirst(int data);

//尾插法
public void addLast(int data);

//打印出链表中的元素
public void display();

//得到单链表的长度
public int size();

//任意位置插入,第一个数据节点为0号下标
public boolean addIndex(int index,int data);

// 给定 index 下标, 找到对应的节点
private Node getPos(int index)

//查找是否包含关键字key是否在单链表当中
public boolean contains(int key);

// 找到 toRemove 的前一个节点
private Node searchPrev(int toRemove)

//删除第一次出现关键字为key的节点
public void remove(int key);

//删除所有值为key的节点
public void removeAllKey(int key);

//清空链表,使链表中的元素全为0
public void clear();

class Node {
    public int data;
    public Node next = null;

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

public class LinkedList {
    //管理所有的链表节点,只需要知道链表表头的位置即可
    //初始化一个head为null的头节点,表示空链表,并且此链表不带傀儡节点
    private Node head = null;

    public void addFirst(int data) {

        Node node = new Node(data);//根据输入的data值构建一个节点

        if (head == null) {    //如果要插入的链表为空链表
            head = node;
            return;
        }
        //如果不是空链表
        node.next = head;
        head = node;
    }

    public void addLast(int data) {
        Node node = new Node(data);

        if (head == null) {
            head = node;
            return;
        }
        Node tail = head;
        while (tail.next != null) {   //如果链表非空,需要先找到这个链表末尾的最后一个节点
            tail = tail.next;
        }
        tail.next = node;
    }

    public void display() {   //打印出链表中的每个元素
        for (Node cur = head; cur != null; cur = cur.next) {
            System.out.println(cur.data + " ");
        }
        System.out.println();
    }


    public int getSize() {
        int size = 0;
        for (Node cur = head; cur != null; cur = cur.next) {
            size++;
        }
        return size;
    }

    public boolean addIndex(int index, int data) {
        int size = getSize();
        if (index < 0 || index > size) {  //index无效,插入失败
            return false;
        }
        if (index == 0) {    //如果index为0,相当于头插入
            addFirst(data);
            return true;
        }
        if (index == size) {
            addLast(data);
            return true;
        }

        Node node = new Node(data);
        //如果index是一个中间的位置
        // (1)先找到index的前一个节点 index-1;
        Node pre = getPos(index - 1);
        node.next = pre.next;
        pre.next = node;
        return true;
    }

    private Node getPos(int index) {
        Node cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur;
    }

    public boolean contains(int toFind) {
        for (Node cur = head; cur != null; cur = cur.next) {
            if (cur.data == toFind) {
                return true;
            }

        }
        return false;

    }
    
    public Node searchPrev(int toRemove) { //找到toRemove的前一个节点

        for (Node cur = head; cur != null && cur.next != null; cur = cur.next) {
            if (cur.next.data == toRemove) {
                return cur;
            }
        }
        return null;
    }


    public void remove(int toRemove) { //删除第一次出现关键字为key的节点
        if (head.data == toRemove) {
            head = head.next;
            return;
        }
        Node pre = searchPrev(toRemove);
        pre.next = pre.next.next;
        //为了直观,还可以写成
        //Node toDelete=pre.next;
        //pre.next=toDelete.next;

    }



    public void removeAllKey(int key) {
        Node cur = head;
        if (head.data == key) {
            head = head.next;
        }
        while (cur != null) {
            if (cur.next.data == key) {
                Node pre = searchPrev(key);
                pre.next = pre.next.next;
            }
            cur = cur.next;
        }

    }

    public void clear() {
        Node cur = head;
        while (cur != cur) {
            cur.data = 0;
            cur = cur.next;
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值