Java基础数据结构:栈和队列

此篇仅为自己写过的代码存档,电脑上存太多回乱,但又想以后翻出来看看故不想删除

1、栈(先进后出,后进先出)

package com.Homeworks;
import java.util.Random;
import java.util.Arrays;

public class SeqStack<T>{
    private T[] stack;
    private int top;
    public SeqStack(){
        this(10);
    }
    public SeqStack(int size){
        this.stack = (T[])new Object[size];
        this.top = 0;
    }
    public void push(T val){
        if(full()){
            this.stack = Arrays.copyOf(this.stack,this.stack.length*2);
        }
        this.stack[this.top] = val;
        this.top ++;
    }
    public void pop(){
        if(empty()){
            return;
        }
        this.top --;
    }
    public T top(){
        return this.stack[this.top - 1];
    }
    public boolean full(){
        return this.top == this.stack.length;
    }
    public boolean empty(){
        return this.top == 0;
    }

    public static void main(String[] args) {
        SeqStack<Integer> s1 = new SeqStack();
        Random rand = new Random();
        for(int i = 1;i <= 10;i++){
            int data = rand.nextInt(100);
            System.out.print(data+"  ");
            s1.push(data);
        }
        System.out.println();

        while(!s1.empty()){
            int top = s1.top();
            System.out.print(top+"  ");
            s1.pop();
        }
        System.out.println();
    }
}

2、队列(先进先出,后进后出)

package com.Homeworks;
import java.util.Random;
import java.util.Arrays;

class Queue<E>{
    private E[] que;// 存储队列元素的数组
    private int front; // 表示队头的位置
    private int rear;// 表示队尾的位置

    public Queue(){//默认构造
        this(10);
    }
    public Queue (int size){//用户定义数组大小
        this.que = (E[])new Object[size];
        this.front = this.rear = 0;
    }
    //入队操作
    public void offer(E val){
        if(full()){//如果满,二倍扩容
            this.que = Arrays.copyOf(this.que,this.que.length);
        }
        this.que[this.rear] = val;
        this.rear = (this.rear + 1)%this.que.length;
    }
    //出队操作
    public E poll(){
        if(empty()){//队列为空
            return null;
        }
        E front = this.que[this.front];
        this.front = (this.front + 1)%this.que.length;
        return front;
    }

    //获取栈顶元素
    public E peek(){
        if(empty()) {
            return null;
        }
        return this.que[this.front];
    }
    public boolean full(){
        return (this.rear + 1) % this.que.length == this.front;
    }
    public boolean empty(){
        return this.front == this.rear;
    }

    public static void main(String[] args) {
        Queue<Integer> q1 = new Queue();
        Random rand = new Random();
        for(int i = 1; i < 10;i++){
            int data = rand.nextInt(100);
            System.out.print(data + "  ");
            q1.offer(data);
        }
        System.out.println();

        while(!q1.empty()){
            int peek = q1.peek();
            System.out.print(peek+"  ");
            q1.poll();
        }
        System.out.println();
       }
   }

3、两个栈实现一个队列

package com.Homeworks;

class Queue<E>{
    private SeqStack<E> s1;
    private SeqStack<E> s2;

    //构造函数 offer poll peek full empty
    public Queue(){
        this.s1 = new SeqStack<>();
        this.s2 = new SeqStack<>();
    }

    public void offer(E val){
        this.s1.push(val);
    }

    public E poll(){
        E data = null;
        while(!this.s1.empty()){
            data = s1.top();
            s1.pop();
            if(s1.empty()){
                break;
            }
            this.s2.push(data);
        }

        while(!this.s2.empty()){
            this.s1.push(s2.top());
            s2.pop();
        }
        return data;
    }

    public boolean full(){
        return this.s1.full();
    }

    public boolean empty(){
        return this.s1.empty();
    }
}

/**
 * 描述:
 *
 * @Author shilei
 * @Date 2019/4/27
 */
public class TwoSeqStackToQueue {
    public static void main(String[] args) {
        Queue<Integer> q = new Queue<>();
        for(int i=0; i<10; ++i){
            q.offer(i);
        }

        while(!q.empty()){
            System.out.print(q.poll() + " ");
        }
        System.out.println();
    }
}

4、两个队列实现一个栈

package com.Homeworks;

class SeqStack1<E>{
    private Queue<E> que1; // 存放栈的元素
    private Queue<E> que2; // 做一个辅助操作

    public SeqStack1(){
        this.que1 = new Queue<>();
        this.que2 = new Queue<>();
    }

    public SeqStack1(int size){
        this.que1 = new Queue<>(size);
        this.que2 = new Queue<>(size);
    }

    public void push(E val){
        this.que1.offer(val);
    }

    public E pop(){
        // 从que1出队,把最后一个出队的元素返回
        E data = null;
        /**
         * 把que1里面的所有元素出队,放入que2里面,
         * 然后把que1最后一个出队的元素直接返回,不用放入que2
         */
        while(!this.que1.empty()){
            data = this.que1.poll();
            if(this.que1.empty()){
                break;
            }
            this.que2.offer(data);
        }

        // 获取该出栈的元素以后,再把que2的元素再放入que1里面
        while(!this.que2.empty()){
            this.que1.offer(this.que2.poll());
        }

        return data;
    }

    public E top(){
        // 从que1出队,把最后一个出队的元素返回
        E data = null;

        while(!this.que1.empty()){
            data = this.que1.poll();
            this.que2.offer(data);
        }

        // 获取该出栈的元素以后,再把que2的元素再放入que1里面
        while(!this.que2.empty()){
            this.que1.offer(this.que2.poll());
        }

        return data;
    }

    public boolean full(){
        return this.que1.full();
    }

    public boolean empty(){
        return this.que1.empty();
    }
}
public class TwoQueueToSeqStack {
    public static void main(String[] args) {
        SeqStack<Integer> s = new SeqStack<>();
        for(int i=0; i<10; ++i){
            s.push(i);
        }

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

本帖只用于自己借鉴,谢谢!

发布了45 篇原创文章 · 获赞 11 · 访问量 4843

猜你喜欢

转载自blog.csdn.net/weixin_44187963/article/details/90139385