数据结构--栈和队列

1、栈

栈是一种后进先出的数据结构,简称LIFO
允许插入和删除的一段称为栈顶,另一端为栈底,不含任何元素的栈称为空栈。
栈的基本操作:插入称为入栈、删除叫做出栈。
1.1 线性栈的基本操作

public class Stack {
    private int maxLen = 0; //栈的最大长度
    private int index = -1; //栈顶
    private int data[] = null;

    public Stack(){
        this(10);
    }

    public Stack(int initLen){
        if(initLen <= 0){
            throw new RuntimeException("stack len is must greater than 0");
        }

        maxLen = initLen;
        data = new int[initLen];
    }

    public boolean push(int data){
        if(index == maxLen-1){
            System.out.println("stack is full");
            return false;
        }

        this.data[++index] = data;
        return true;
    }

    public int pop(){
        if(index == -1){
            System.out.println("stack is empty");
            return -1;
        }

        return this.data[index--];
    }

    public int getLen(){
        return this.index;
    }
}

调用:

public static void main(String[] args) {
        Stack stack = new Stack(3);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);

        for (int i = 0; i < 4; i++) {
            System.out.println(stack.pop());
        }

    }

1.2 链表结构的栈
数据的存储方式使用链表结构。与线性栈类似

2、队列

队列是一种先进先出的数据结构(FIFO),只允许在一端插入,称为队尾。在另一端删除,称为队头。

2.1 线性队列
类似线性表。取出队头数据时,会将后续数据向前移动

2.3 循环链表
因为线性队列取出队头,后续数据会向前移动,效率不高。可以动态调整队头、队尾的位置,提高效率
这里写图片描述

//循环队列操作
public class Queue {
    private int maxLen;
    private int head;   //队头
    private int tail;   //队尾
    private int data[] = null;
    private int dataLen; //元素个数

    public Queue(){
        this(10);
    }

    public Queue(int initLen){
        if(initLen <= 0){
            System.out.println("queue init len must greater than 0");
            return;
        }

        maxLen = initLen;
        data = new int[initLen];
    }

    //入队
    public boolean enQueue(int data){
        //队列空出一个,用于判断队列是否满。区别于队列空的判断
        if((tail+1+maxLen)%maxLen == head){
            System.out.println("queue is full now");
            return false;
        }

        this.data[tail] = data;
        tail = (tail+1)%maxLen;
        return true;
    }

    //出队
    public int deQueue(){
        //头和尾相同,说明是空队列
        if(tail == head){
            System.out.println("queue is empty now");
            return -1;
        }

        int tmp = this.data[head];
        head = (head+1)%maxLen;
        return tmp;
    }

    //队列长度
    public int getLen(){
        return (tail+maxLen - head)%maxLen;
    }
}

参考大话数据结构4.12
测试:

public static void main(String[] args) {
        Queue queue = new Queue(5);

        queue.enQueue(1);
        queue.enQueue(2);
        queue.enQueue(3);
        queue.enQueue(4);
        queue.enQueue(5);

        for (int i = 0; i < 3; i++) {
            System.out.println(queue.deQueue());
        }

        queue.enQueue(6);
        queue.enQueue(7);

        System.out.println("len==>"+queue.getLen());

        for (int i = 0; i < 5; i++) {
            System.out.println(queue.deQueue());
        }
    }

2.3 链表队列
类似单链表。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值