Java —— 用Java实现单链表和双链表

单链表

package practice;

import java.util.Objects;

public class Linked {
    private Node first;
    private int size;
    /**
     * 获取链表的长度
     *
     * @return
     */
    public int size() {
        return size;
    }
    /**
     * 返回链表是否是空的
     *
     * @return
     */
    public boolean isEmpty() {

        return size==0;
    }
    /**
     * 返回链表是否包含某个对象
     *
     * @param o
     * @return
     */
    public boolean contains(Object o) {
        Node t = first;
        while (t.next!=null){
            if (t.value.equals(0)){
                return true;
            }
            t = t.next;
        }
        return false;
    }
    /**
     * 添加元素到链表中
     *
     * @param o
     * @return
     */
    public boolean add(Object o) {
// Object --> 放到Node对象里面去
        Node node = new Node(o, null);
        if (first == null) {
            first = node;
            size++;
            return true;
        }
        Node t = first;
        while (t.next != null) {
            t = t.next;
        }
        t.next = node;
        size++;
        return true;
    }
    /**
     * 删除某个元素
     *
     * @param o
     * @return
     */
    public boolean remove(Object o) {
        if (size==0){
            return false;
        }
        if (Objects.equals(o,first.value)&&first.next==null){
            first=null;
            size--;
            return true;
        }
        if (first.value.equals(o)){
            first = first.next;
            size--;
            return true;
        }
        Node t = first;
        while (!t.next.value.equals(o)){
            t = t.next;
            if (t==null){
                return false;
            }
        }
        t.next = t.next.next;
        size--;
        return true;
    }
    /**
     * 获取指定位置的元素
     *
     * @param index
     * @return
     */
    public Object get(int index) {
        if (index>size){
            return null;
        }
        if (index==1){
            return false;
        }
        Node t = first;
        for (int i = 1; i <index ; i++) {
            t = t.next;
        }
        return t;
    }
    /**
     * 返回指定元素的下标
     *
     * @param o
     * @return
     */
    public int indexOf(Object o) {
        int index = 1;
        Node t = first;
        for (int i = 0; i < size; i++) {
            if (t.value.equals(o)){
                return index;
            }
            t = t.next;
            index++;
        }
        return 0;
    }
    /**
     * 将链表转换成字符串 Linked[元素, 元素, ...]
     *
     * @return
     */
    @Override
    public String toString() {
        String str = "";
        Node t = first;
        for (int i = 0; i < size; i++) {
            if (t.next==null){
                str = str+t.value;
            }else {
                str=str+t.value+",";
            }

            t = t.next;

        }
        return "Linked["+ str +"]";
    }
    class Node {
        private Object value;
        private Node next;
        public Node(Object value, Node next) {
            this.value = value;
            this.next = next;
        }
        public String toString() {
            return "值是"+value;
        }
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Node node = (Node) o;
            return Objects.equals(value, node.value) && Objects.equals(next,
                    node.next);
        }
    }
}
package practice;

public class LinkedTest {
    public static void main(String[] args) {
        Linked link = new Linked();
        link.add(1);
        link.add(2);

        link.add(3);
        link.add(4);
        link.remove(4);
        System.out.println(link.size());
        System.out.println(link.isEmpty());
        System.out.println(link.contains(2));
        System.out.println(link.get(2));
        System.out.println(link.indexOf(4));
        System.out.println(link.toString());
    }
}

双链表

package pm.structure;

import java.util.Objects;

public class DoubleLinked {
    private Node first;
    private Node last;
    private int size;

    /**
     * 在双链表中添加结点
     * @param obj  要添加结点的value
     * @return   返回是否添加成功
     */
    public boolean add(Object obj){
        Node node = new Node(null,obj,null);
        if (first == null){
            first = node;
            last = first;
            size++;
            return true;
        }
        last.next=node;
        node.prev = last;
        last = node;
        size++;
        return true;
    }

    /**
     *  删除第一个结点
     * @return  是否删除成功
     */
    public Object removeFirst(){
        if (first==null){
            return false;
        }
        Node node = first;
        first.next.prev = null;
        first = first.next;
        size--;
        return node.value;
    }

    /**
     * 删除最后一个结点
     * @return   是否删除成功
     */
    public Object removeLast(){
        if (first==null){
            return false;
        }
        Node node = last;
        last.prev.next = null;
        last = last.prev;
        size--;
        return node.value;
    }

    /**
     * 删除某个节点
     * @param obj  要删除value为obj的结点
     * @return  是否删除成功
     */
    public Object remove(Object obj){
        if (Objects.equals(first.value,obj)){
            removeFirst();
        }
        if (Objects.equals(last.value,obj)){
            removeLast();
        }
        Node t = first;
        while (!Objects.equals(t.next.value,obj)){
            t = t.next;
            if (t.next==null){
                return false;
            }
        }
        Node node = t.next;
        t.next = t.next.next;
        t.next.prev = t;
        size--;
        return node.value;

    }

    /**
     * 获取链表的长度
     * @return
     */
    public int size(){
        return size;
    }

    /**
     * 返回链表是否为空
     * @return
     */
    public boolean isEmpty(){
        return size==0;
    }

    /**
     * 返回链表是否包含某个对象
     * @param obj
     * @return
     */
    public boolean contains(Object obj){
        Node t = first;
        while(!Objects.equals(t.value,obj)){
            t = t.next;
            if (t==null){
                return false;
            }
        }
        return true;
    }

    /**
     * 获取指定位置的元素
     * @param index
     * @return
     */
    public Object get(int index){
        if (index>=size){
            return null;
        }
        Node t = first;
        for (int i = 0;i<index;i++){
            t = t.next;
        }
        return t.value;
    }

    /**
     * 返回指定元素的下标
     * @param o
     * @return
     */
    public int indexOf(Object obj){
        Node t = first;
        int index = 0;
        while (!Objects.equals(t.value,obj)){
            t = t.next;
            index++;
            if (t == null){
                return -1;
            }
        }
        return index;

    }
    /**
     * 将链表转换成字符串 doubleLinked[元素, 元素, ...]
     * @return
     */
    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder("doubleLinked[");
        Node t = first;
        while (t.next!=null){
            sb=sb.append(t.value+",");
            t = t.next;
        }
        sb = sb.append(t.value+"]");
        return sb.toString();
    }
    class Node{
        private Node prev;
        private Object value;
        private Node next;

        public Node(Node prev, Object value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }
}
package pm.structure;

public class DoubleLinkedTest {
    public static void main(String[] args) {
        DoubleLinked doubleLink = new DoubleLinked();
        doubleLink.add("lyy");
        doubleLink.add("fhh");
        doubleLink.add("gtx");
        doubleLink.add("lmy");
        doubleLink.add("lyn");
        System.out.println(doubleLink.contains("yx"));
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值