JAVA数据结构之链表

目录

先写链结点Node

双端链表和双向链表

先写链结点Node

浅谈ArrayList和LinkedList


手写linkList

先写链结点Node

Node包含一个数据域和一个指针域

package com.zxl.ch03;

/**
 * 链结点,相当于车厢
 */
public class Node {
    public long data;//数据域
    public Node next;//结点域或者指针域

    public Node(long value){
        this.data = value;
    }
    public void display(){
        System.out.print(this.data+" ");
    }
}
package com.zxl.ch03;

import com.zxl.ch03.Node;

/*
 * 链表,相当于火车
 */
public class LinkList {
	//头结点
	private Node first;

	public LinkList() {
		first = null;
	}

	/**
	 * 从头结点进行插入
	 */
	public void insertFirst(long value) {
		Node node = new Node(value);
		node.next = first;
		first = node;
	}

	/**
	 * 删除头结点
	 */
	public Node deleteFirst() {
		Node tmp = first;
		first = tmp.next;
		return tmp;
	}

	/**
	 * 显示方法从头节点开始显示
	 */
	public void display() {
		Node current = first;
		while(current != null) {
			current.display();
			current = current.next;
		}
		System.out.println();
	}

	/**
	 * 查找方法,从头结点开始查找
	 */
	public Node find(long value) {
		Node current = first;
		while(current.data != value) {
			if(current.next == null) {
				return null;
			}
			current = current.next;
		}
		return current;
	}

	/**
	 * 删除方法,根据数据域来进行删除
	 */
	public Node delete(long value) {
		Node current = first;
		Node previous = first;//保存要删除得结点得前置结点
		//从头结点循环找出要删除得结点并得到他的前置结点
		while(current.data != value) {
			if(current.next == null) {
				return null;
			}
			previous = current;
			current = current.next;
		}

		if(current == first) {
			first = first.next;//如果直接删除得是头结点,则直接头节点指向next
		} else {
			previous.next = current.next;//将前置结点得next指向删除结点得next
		}
		return current;

	}
}

JAVA数据结构之链表_第1张图片

双端链表和双向链表

先写链结点Node

Node包含一个数据域和两个指针域(一个指向下一个结点、一个指向上一个结点)

package com.zxl.ch04;

/*
 * 链结点,相当于是车厢
 */
public class Node {
	//数据域
	public long data;
	//指针域
	public Node next;
	public Node previous;

	public Node(long value) {
		this.data = value;
	}

	/**
	 * 显示方法
	 */
	public void display() {
		System.out.print(data + " ");
	}
}
package com.zxl.ch04;


/*
 * 双端链表
 */
public class FirstLastLinkList {
    //头结点
    private Node first;
    //尾结点
    private Node last;

    public FirstLastLinkList() {
        first = null;
        last = null;
    }

    /**
     * 插入一个结点,在头结点进行插入
     */
    public void insertFirst(long value) {
        Node node = new Node(value);
        if(isEmpty()) {
            last = node;
        }
        node.next = first;
        first = node;
    }

    /**
     * 插入一个结点,从尾结点进行插入
     */
    public void insertLast(long value) {
        Node node = new Node(value);
        if(isEmpty()) {
            first = node;
        } else {
            last.next = node;
        }
        last = node;
    }

    /**
     * 删除一个结点,在头结点后进行删除
     */
    public Node deleteFirst() {
        Node tmp = first;
        if(first.next == null) {
            last = null;
        }
        first = tmp.next;
        return tmp;
    }

    /**
     * 显示方法
     */
    public void display() {
        Node current = first;
        while(current != null) {
            current.display();
            current = current.next;
        }
        System.out.println();
    }

    /**
     * 查找方法
     */
    public Node find(long value) {
        Node current = first;
        while(current.data != value) {
            if(current.next == null) {
                return null;
            }
            current = current.next;
        }
        return current;
    }

    /**
     * 删除方法,根据数据域来进行删除
     */
    public Node delete(long value) {
        Node current = first;
        Node previous = first;
        while(current.data != value) {
            if(current.next == null) {
                return null;
            }
            previous = current;
            current = current.next;
        }

        if(current == first) {
            first = first.next;
        } else {
            previous.next = current.next;
        }
        return current;

    }

    /**
     * 判断是否为空
     */
    public boolean isEmpty() {
        return (first == null);
    }
}

JAVA数据结构之链表_第2张图片

package com.zxl.ch04;


/*
 * 双向链表
 */
public class DoubleLinkList {
    //头结点
    private Node first;
    //尾结点
    private Node last;

    public DoubleLinkList() {
        first = null;
        last = null;
    }

    /**
     * 插入一个结点,在头结点后进行插入
     */
    public void insertFirst(long value) {
        Node node = new Node(value);
        if(isEmpty()) {
            last = node;
        } else {
            first.previous = node;
        }
        node.next = first;
        first = node;
    }

    /**
     * 插入一个结点,从尾结点进行插入
     */
    public void insertLast(long value) {
        Node node = new Node(value);
        if(isEmpty()) {
            first = node;
        } else {
            last.next = node;
            node.previous = last;
        }
        last = node;
    }

    /**
     * 删除一个结点,在头结点后进行删除
     */
    public Node deleteFirst() {
        Node tmp = first;
        if(first.next == null) {
            last = null;
        } else {
            first.next.previous = null;
        }
        first = tmp.next;
        return tmp;
    }

    /**
     * 删除结点,从尾部进行删除
     */
    public Node deleteLast() {
        if(first.next == null) {
            first = null;
        } else {
            last.previous.next = null;
        }
        last = last.previous;
        return last;
    }

    /**
     * 显示方法
     */
    public void display() {
        Node current = first;
        while(current != null) {
            current.display();
            current = current.next;
        }
        System.out.println();
    }

    /**
     * 查找方法
     */
    public Node find(long value) {
        Node current = first;
        while(current.data != value) {
            if(current.next == null) {
                return null;
            }
            current = current.next;
        }
        return current;
    }

    /**
     * 删除方法,根据数据域来进行删除
     */
    public Node delete(long value) {
        Node current = first;
        while(current.data != value) {
            if(current.next == null) {
                return null;
            }
            current = current.next;
        }

        if(current == first) {
            first = first.next;
        } else {
            current.previous.next = current.next;
        }
        return current;

    }

    /**
     * 判断是否为空
     */
    public boolean isEmpty() {
        return (first == null);
    }
}

JAVA数据结构之链表_第3张图片

浅谈ArrayList和LinkedList

Linkedlist,双向链表,优点,增加删除,用时间很短,但是因为没有索引,对索引的操作,比较麻烦,只能循环遍历,但是每次循环的时候,都会先判断一下,这个索引位于链表的前部分还是后部分,每次都会遍历链表的一半 ,而不是全部遍历。 
双向链表,都有一个previous和next, 链表最开始的部分都有一个fiest和last 指向第一个元素,和最后一个元素。增加和删除的时候,只需要更改一个previous和next,就可以实现增加和删除,所以说,LinkedList对于数据的删除和增加相当的方便

而ArrayList底层是数组,查找快,增加和删除则不如LinkedList

你可能感兴趣的:(Java数据结构,数据结构,链表,java,算法)