队列和栈

队列是一种先进先出的数据结构,是只允许在一端进行插入,另一端进行删除操作的线性表,在队尾插入,队头删除,其本质仍是一个数组。栈是一种“先进后出”的一种数据结构,有压栈出栈两种操作方式。如下图:

队列的具体实现如下:

import java.util.Arrays;

class Queue{
    // 存储队列的元素
    private int[] queue;
    // 队头
    private int front;
    // 队尾
    private int rear;

    // 15
    public Queue() {
        this(15);
    }

    // 自定义队列大小
    public Queue(int size) {
        this.queue =new int[size];
        this.rear = 0;
        this.front = 0;
    }

    // 入队操作
    public void offer(int val){
       if(full()){
//如果队列满,则进行队列的扩容
           this.queue = Arrays.copyOf(this.queue,this.queue.length*2);
       }
       this.queue[rear] = val;
       this.rear = (this.rear + 1) % this.queue.length; //循环队列
    }

    // 出队,并把元素返回
    public int poll(){
        int val = queue[front];
        this.front++;
        return val;
    }

    // 查看队头元素
    public int peek(){
        return this.queue[front];
    }

    // 判断队满
    public boolean full(){
        return (this.rear + 1) % this.queue.length == this.front;
    }

    // 判断队空
    public boolean empty(){
        return rear == front;
    }
    public String toString() {
        String str=new String();
        for(int i=0;i<this.rear;i++) {
            str = str + this.queue[i] + " ";
        }
        return str;
    }
}

public class QueueTest {
    public static void main(String[] args) {
        Queue queue = new Queue();
        for (int i = 0; i < 20; i++) {
            queue.offer(i+1);
        }
        System.out.println(queue);

    }
}

 栈的具体实现如下: 

import java.util.Arrays;

/**
 * 顺序栈代码实现
 */
class SeqStack{
    // 存储栈元素的数组
    private int[] stack;
    // 标识栈顶的位置
    private int top;

    // 默认构造,初始大小为10
    public SeqStack() {

        this(10);
    }

    // 用户指定栈的初始大小
    public SeqStack(int size) {
        this.stack = new int[size];
        this.top = 0;
    }

    // 入栈操作
    public void push(int val){
        if (full()) {
            this.stack = Arrays.copyOf(this.stack, this.stack.length * 2);
        }
        this.stack[this.top++]=val;
    }

    // 出栈,并把出栈的元素返回
    public int pop(){
        this.top--;
        int val=this.stack[top];
        return val;
    }

    // 返回栈顶元素
    public int top(){
        return this.stack[top-1];
    }

    // 判断栈空
    public boolean empty(){
        return this.top == 0;
    }

    // 判断栈满
    public boolean full(){
        return this.stack.length==this.top;
    }

    // 返回栈元素的个数
    public int size(){
        return this.top;
    }

    // 自定义栈元素的打印方式
    @Override
    public String toString() {
        String str=new String();
        for(int i=0;i<this.top;i++) {
            str = str + this.stack[i] + " ";
        }
        return str;
    }
}

/**
 * 描述: 顺序栈代码测试
 */
public class SeqStackTest {
    public static void main(String[] args) {
        SeqStack stack = new SeqStack();
        for (int i = 0; i < 30; i++) {
            stack.push(i+1);
        }

        System.out.println(stack.size());

        while (!stack.empty()){
            System.out.print(stack.pop() + " ");
        }
        System.out.println();

        System.out.println(stack.size());
    }
}
原文地址:https://www.cnblogs.com/128-cdy/p/11676640.html