Java数据结构之链表实现栈和队列

栈的实现

// 栈的基本方法
public interface Stack<E> {

    void push(E e);

    E pop();

    E peek();

    int getSize();

    boolean isEmpty();
}
// 链表的基本方法的实现
public class LinkedList<E> {

    private Node dummyHead;
    private int size;

    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    public LinkedList() {
        dummyHead = new Node(null, null);
        size = 0;
    }

    public int getSize() {
        return size;
    }

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


    /**
     * 添加
     * 不是一个常用的操作
     * @param index
     * @param e
     */
    public void add(int index, E e) {
        if(index < 0 || index > size)
            throw new IllegalArgumentException("Add failed, Illegal index.");
        Node prev = dummyHead;
        for(int i = 0; i < index; i++) prev = prev.next;
        prev.next = new Node(e, prev.next);
        size++;
    }

    /**
     * 在链表头部添加数据
     * @param e
     */
    public void addFirst(E e) {
        add(0, e);
    }

    /**
     * 在链表末尾添加元素
     * @param e
     */
    public void addList(E e) {
        add(size, e);
    }

    /**
     * 获取索引对应的值
     * @param index
     * @return
     */
    public E get(int index) {
        if(index < 0 || index > size)
            throw new IllegalArgumentException("Add failed, Illegal index.");
        Node cur = dummyHead.next;
        for(int i = 0; i < index; i++) cur = cur.next;
        return cur.e;
    }

    /**
     * 获取第一个元素
     * @return
     */
    public E getFirst() {
        return get(0);
    }

    /**
     * 获取最后一个元素
     * @return
     */
    public E getLast() {
        return get(size-1);
    }

    /**
     * 修改第index位置的元素
     * @param index
     * @param e
     */
    public void set(int index, E e) {
        if(index < 0 || index > size)
            throw new IllegalArgumentException("Add failed, Illegal index.");
        Node cur = dummyHead.next;
        for(int i = 0; i < index; i++) cur = cur.next;
        cur.e = e;
    }

    /**
     * 遍历链表中的元素
     * @param e
     * @return
     */
    public boolean contains(E e) {
        Node cur = dummyHead.next;
        while(cur != null) {
            if(cur.e.equals(e))
                return true;
            cur = cur.next;
        }
        return false;
    }


    /**
     * 删除指定位置的元素
     * @param index
     * @return
     */
    public E remove(int index) {
        if(index < 0 || index > size)
            throw new IllegalArgumentException("Add failed, Illegal index.");
        Node prev = dummyHead;
        for(int i = 0; i < index; i++) prev = prev.next;
        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;
        size--;
        return retNode.e;
    }

    /**
     * 删除第一个元素
     * @return
     */
    public E removeFirst() {
        return remove(0);
    }

    /**
     * 删除最后一个
     * @return
     */
    public E removeLast() {
        return remove(size - 1);
    }


    /**
     * 遍历链表
     * @return
     */
    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyHead.next;
        while(cur != null) {
            res.append(cur + "-->");
            cur = cur.next;
        }
        res.append("NULL");
        return res.toString();
    }
    
}
// 借助链表实现栈操作
public class LinkedListStack<E> implements Stack<E> {

    private LinkedList<E> list;

    public LinkedListStack() {
        list = new LinkedList<>();
    }

    /**
     * 入栈
     * @param e
     */
    @Override
    public void push(E e) {
        list.addFirst(e);
    }

    /**
     * 出栈
     * @return
     */
    @Override
    public E pop() {
        return list.removeFirst();
    }

    /**
     * 获取栈顶元素
     * @return
     */
    @Override
    public E peek() {
        return list.getFirst();
    }

    /**
     * 获取栈数量
     * @return
     */
    @Override
    public int getSize() {
        return list.getSize();
    }

    /**
     * 判断栈为空
     * @return
     */
    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    /**
     * 遍历栈元素
     * @return
     */
    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Stack top ");
        res.append(list);
        return res.toString();
    }


    public static void main(String[] args) {
        LinkedListStack<Integer> stack = new LinkedListStack<>();
        for(int i = 0; i < 5; i++) {
            stack.push(i);
            System.out.println(stack);
        }
        stack.pop();
        System.out.println(stack);
    }
}

队列的实现

// 队列基本操作
public interface Queue<E> {

    int getSize();

    boolean isEmpty();

    void enqueue(E e);

    E dequeue();

    E getFront();

}
// 队列的基本操作
public class LinkedListQueue<E> implements Queue<E> {
    // 定义的节点
    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    // 定义一个头指针和尾指针
    private Node head, tail;
    private int size;

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

    /**
     * 获取队列元素的数量
     * @return
     */
    @Override
    public int getSize() {
        return size;
    }

    /**
     * 判断队列是否为空
     * @return
     */
    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 入队操作
     * @param e
     */
    @Override
    public void enqueue(E e) {
        if(tail == null) {
            tail = new Node(e);
            head = tail;
        }else{
            tail.next = new Node(e);
            tail = tail.next;
        }
        size++;
    }

    /**
     * 出队元素
     * @return
     */
    @Override
    public E dequeue() {
        if(isEmpty())
            throw new IllegalArgumentException("不能出队!");
        Node retNode = head;
        head = head.next;
        retNode.next = null;
        if(head == null)
            tail = null;
        size--;
        return retNode.e;
    }

    /**
     * 获取队首
     * @return
     */
    @Override
    public E getFront() {
        if(isEmpty())
            throw new IllegalArgumentException("不能出队!");
        return head.e;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Queue front ");
        Node cur = head;
        while(cur != null){
            res.append(cur + "-->");
            cur = cur.next;
        }
        res.append("NULL tail");
        return res.toString();
    }

    public static void main(String[] args) {
        LinkedListQueue<Integer> queue = new LinkedListQueue<>();
        for(int i = 0; i < 10; i++) {
            queue.enqueue(i);
            System.out.println(queue);
            if(i % 3 == 2) {
                queue.dequeue();
                System.out.println(queue);
            }
        }
    }
}

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