数据结构与算法(四)链表(一)-单链表实现

1.单链表的增、删、遍历

public class Test {
    @org.junit.Test
    public void test() {

        //测试代码        
        System.out.println("00000000000000000000000000000000000000000000000000000000000000000000000000");

        LinkedList linkedList = new LinkedList();
        linkedList.add(34);
        linkedList.add(4);
        linkedList.add(6);
        linkedList.add(68);
        linkedList.add(938);

        linkedList.showData();

        System.out.println("00000000000000000000000000000000000000000000000000000000000000000000000000");
        linkedList.delete(68);
        linkedList.showData();
        System.out.println("00000000000000000000000000000000000000000000000000000000000000000000000000");
        System.out.println(linkedList.getIndex(111));
//        linkedList.showData();
        System.out.println("00000000000000000000000000000000000000000000000000000000000000000000000000");

    }

    class Node {
        //定义Node对象
        int data;
        Node next = null;

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

    class LinkedList {
        Node head = null;

        public void showData() {
            if (head == null) {
                System.out.println("-1");
                return;
            }
            Node current = head;
            int count = 0;
            while (current != null) {
                System.out.println(++count + " = " + current.data);
                current = current.next;
            }
        }

        //获取长度的过程就是遍历的过程
        public int getLength() {
            if (head == null) {
//                System.out.println("-1");
                return 0;
            }
            Node current = head;
            int count = 0;
            while (current != null) {
//                System.out.println(++count + " = " + current.data);
                ++count;
                current = current.next;
            }
            return count;
        }

        public void add(int data) {
            Node newNode = new Node(data);
            if (head == null) {
                head = newNode;
                return;
            }
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }

        //删除分为根据下标删除和根据值删除,这里是做的按值删除
        public void delete(int data) {
            if (head == null) {
                return;
            }

            Node current = head;
            if (current.data == data) {
                head = current.next;
                return;
            }
            while (current.next != null) {

                Node pre = current;
                current = current.next;
                if (current.data == data) {
                    pre.next = current.next;
                }
            }
        }

        //获取某个值在链表的下标
        public int getIndex(int data) {

            int index = -1;
            if (head == null) {
                return index;
            }
            Node current = head;
            while (current != null) {
                index++;
                if (current.data == data) {
                    return index;
                }

                current = current.next;
            }
            return -1;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值