数据结构之链表、栈和队列 java代码实现

定义抽象节点类Node:

package cn.wzbrilliant.datastructure;

/**
 * 节点
 * @author ice
 *
 */
public abstract class Node {
    private Node next;

    public Node(){
        next=null;
    }

    public void setNext(Node nextNode){
        next=nextNode;
    }

    public Node getNext(){
        return next;
    }
}

链表类,实现了插入首尾节点、指定位置节点,删除节点、指定位置节点,链表的逆序以及判空操作:

package cn.wzbrilliant.datastructure;

/**
 * 链表
 * @author ice
 *
 */
public class Link {

    protected Node head;
    protected Node tail;
    protected int size;

    public Link() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }

    public void addAtFirst(Node node){
        node.setNext(head);
        head=node;
        if(size==0)
            tail=node;
        size++;
    }

    public void addAtLast(Node node){
        if(size==0){
            head=tail=node;
        }else{
            tail.setNext(node);
            tail=node;
        }
        node.setNext(null);
        size++;
    }

    public void removeFirst(){
        if(size==0)
            throw new RuntimeException("link size is 0...");
        head=head.getNext();
        if(size==1){
            tail.setNext(null);
        }
        size--;
    }

    public void removeLast(){
        if(size==0)
            throw new RuntimeException("link size is 0...");

        if(size==1){
            head=tail=null;
            size--;
            return ;
        }

        Node node=head;
        while(node.getNext()!=tail){
            node=node.getNext();
        }
        node.setNext(null);
        tail=node;
        size--;
    }

    /**
     * 队列逆序
     */
    public void reverse() {
        Node preNode, node, tempNode;
        if (size == 0)
            return;
        preNode = head;
        node = preNode.getNext();
        preNode.setNext(null);
        tail = preNode;
        while (node != null) {
            tempNode = node.getNext();
            node.setNext(preNode);
            preNode = node;
            node = tempNode;
        }
        head = preNode;
    }

    /**
     * 在第index个节点后插入newNode
     * 
     * @param newNode
     * @param index
     */
    public void insert(Node newNode, int index) {
        if (index < 0 || index > size)
            throw new RuntimeException("索引错误");

        if (index == 0) {
            newNode.setNext(head);
            head = newNode;
            size++;
            return;
        }

        if (index == size) {
            newNode.setNext(null);
            tail.setNext(newNode);
            tail = newNode;
            size++;
            return;
        }

        Node node = head;
        for (int i = 1; node != null; i++, node = node.getNext()) {
            if (i == index) {
                newNode.setNext(node.getNext());
                node.setNext(newNode);
                size++;
                return;
            }
        }

    }

    /**
     * 移除Node节点    Node节点需重写equals()方法
     * 
     * @param node
     */
    public void remove(Node node) {
        if (node == null || size == 0)
            throw new RuntimeException("remove error...");
        for (Node temp = head; temp != null; temp = temp.getNext()) {
            if (temp == head) {
                if (temp.equals(node)) {
                    head = head.getNext();
                    size--;
                    continue;
                }
            }
            if (temp.getNext().equals(node)) {
                temp.setNext(temp.getNext().getNext());
                size--;
            }

        }
    }

    public Node getFirst() {
        if (size == 0)
            return null;
        return head;
    }

    public Node getLast() {
        if (size == 0)
            return null;
        return tail;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        if (size == 0)
            return true;
        return false;
    }
}

栈类,实现了入栈、出战、获取栈顶元素以及判空的操作:

package cn.wzbrilliant.datastructure;

/**
 * 栈
 * @author ice
 *
 */
public class Stack {
    private int size;
    private Node top;

    public Stack() {
        size = 0;
        top = null;
    }

    public void push(Node node) {
        node.setNext(top);
        top = node;
        size++;
    }

    public Node pop() {
        if (top == null)
            return null;
        Node node = top;
        top = top.getNext();
        size--;
        return node;
    }

    public Node top() {
        return top;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        if (size == 0)
            return true;
        return false;
    }
}

队列类,实现了入队、出队、判空的操作:

package cn.wzbrilliant.datastructure;

/**
 * 队列
 * @author ice
 *
 */
public class Queue {

    private Node head;
    private Node tail;
    private int size;

    public Queue() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }

    public void enQueue(Node node) {
        tail.setNext(node);
        tail = node;
        size++;
    }

    public Node deQueue() {
        if (size == 0)
            return null;
        Node node = head;
        head = head.getNext();
        size--;
        return node;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        if (size == 0)
            return true;
        return false;
    }

}
坚持原创技术分享,您的支持将鼓励我继续创作!
0%