【数据结构与算法】十七 栈 队列

【数据结构与算法】十七 栈 队列

后进先出(Last In First Out),简称为LIFO线性表。

实际应用

方法的调用 A -> B -> C

Java 数组实现栈

package com.cn.mark.algorithm.stack;

import java.lang.reflect.Array;

public class Stack<T> {

    private int max_length ;
    private int top;
    private T[] array;


    @SuppressWarnings("unchecked")
    public Stack(Class<T> componentType,int length){ 
        top = -1;
        max_length = length ; 
        array=(T[])Array.newInstance(componentType, length);  
    } 

    public boolean push(T value) {
        if (top + 1 < max_length) {
            top++;
            array[top] = value;
            return true;
        } else {
            System.out.println("static full");
            return false;
        }
    }

    public boolean pop() {
        if (is_empty()) {
            return false ; 
        } else {
            array[top] = null;
            top--;
            return true;
        }
    }

    public T get(){
        if (is_empty()) {
            return null;
        } else {
            return array[top];
        }
    }

    public boolean is_empty() {
        return -1 == top;
    }

    public static void main(String[] args) {
// Stack<Integer> s = new Stack<Integer>(Integer.class , 5);

// System.out.println(s.get_top());
// 
// System.out.println(s.pop());
// System.out.println(s.get());
// System.out.println(s.is_empty());
// 
// s.push(0);
// s.push(1);
// s.push(2);
// s.push(3);
// s.push(4);
// System.out.println(s.is_empty());
// 
// s.pop();
// System.out.println(s.get());
// System.out.println(s.is_empty());


        Stack<String> ss = new Stack<String>(String.class , 5);
        System.out.println(ss.is_empty());
        ss.push("0");
        ss.push("1");
        ss.push("2");
        ss.push("3");
        ss.push("4");
        ss.push("5");
        System.out.println(ss.is_empty());

        ss.pop();
        System.out.println(ss.get());
        System.out.println(ss.is_empty());
    }

}

队列

操作原则是先进先出的,所以队列又称作FIFO表(First In First Out)

实际应用

生产者将值放入队列中,消费者从队列取值.

JAVA code

package com.cn.mark.algorithm.queue;

public class LinkQueue<T> {

    private class Node {
        public T data;
        public Node next;

        public Node(T data, Node next) {
            this.data = data;
            this.next = next;
        }
    }

    private Node front;// 头
    private Node rear;// 尾
    private int size = 0;// 长度

    public LinkQueue() {
        Node n = new Node(null, null);
        front = rear = n;
    }

    /** * 入队 */
    public void enqueue(T data) {
        Node s = new Node(data, null);
        rear.next = s;
        rear = s;
        size++;
    }

    /** * 出队 */
    public T dequeue() {
        if (rear == front) {
            try {
                throw new Exception("堆栈为空");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        } else {
            Node p = front.next;
            T x = p.data;
            front.next = p.next;
            if (p.next == null)
                rear = front;
            p = null;
            size--;
            return x;
        }
    }

    /** * 队列长队 */
    public int size() {
        return size;
    }

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

    }

    public String toString() {
        if (isEmpty()) {
            return "[]";
        } else {
            StringBuilder sb = new StringBuilder("[");
            for (Node current = front.next; current != null; current = current.next) {
                sb.append(current.data.toString() + ", ");
            }
            int len = sb.length();
            return sb.delete(len - 2, len).append("]").toString();
        }
    }

    // 测试
    public static void main(String[] args) {
        LinkQueue<Integer> queue = new LinkQueue<Integer>();
        queue.enqueue(0);
        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);
        queue.enqueue(4);
        queue.enqueue(5);
        queue.enqueue(6);
        System.out.println(queue);
        System.out.println("出队:" + queue.dequeue());
        System.out.println("队列长度=" + queue.size());
        System.out.println(queue);
        System.out.println("出队:" + queue.dequeue());
        System.out.println("队列长度=" + queue.size());
        System.out.println(queue);
        System.out.println("出队:" + queue.dequeue());
        System.out.println("队列长度=" + queue.size());
        System.out.println(queue);
    }
}

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