栈和队列 迈不过友情╰ 2022-06-18 07:54 302阅读 0赞 栈: package Suan; public class Stack { private int size; private int[] array; private int top; Stack(int size){ this.size=size; array=new int[size]; top=-1; } public void push(int i){ array[++top]=i; } public int pop(){//虽然数据还在,但是新加进来的数据会覆盖掉原数据 return array[top--]; } public boolean isEmpty(){ return top==-1; } public boolean isFull(){ return top==size-1; } public static void main(String[] args) { Stack a = new Stack(5); a.push(34); a.push(23); a.push(12); a.push(67); a.push(2); System.out.println(a.isFull()); System.out.println(a.pop()); } } 可以用于单词逆序,和分隔符匹配如()\{\}\[\]。 队列: package Suan; public class Queue { private int items; private int[] array; private int first; private int last; Queue(int size){ this.items=0; array=new int[size]; first=0; last=-1; } public void insert(int i){ if(last==array.length-1){ last=-1; } array[++last]=i; items++; } public int remove(){ int a = array[first++]; if(first==array.length){ first=0; } items--; return a; } public boolean isEmpty(){ return items==0; } public boolean isFull(){ return items==array.length; } } 优先级队列: package Suan; public class PriorityQueue { private int items; private int[] array; public PriorityQueue(int size) { array=new int[size]; items=0; } public void insert(int i){ int j=0; if(items==0){ array[items++]=i; }else{ for(j=items-1;j>=0;j--){ if(i>array[j]){ array[j+1]=array[j]; }else{ break; } } array[j+1]=i; items++; } } public int remove(){ return array[--items]; } public boolean isEmpty(){ return items==0; } public boolean isFull(){ return items==array.length; } public static void main(String[] args) { PriorityQueue a = new PriorityQueue(5); a.insert(34); a.insert(23); a.insert(45); a.insert(12); System.out.println(a.remove()); System.out.println(a.remove()); System.out.println(a.remove()); } }
相关 js:数组实现队列和栈、栈实现队列、队列实现栈 *目录** 一、利用数组结构实现大小固定的队列和栈 二、仅用队列结构实现栈结构 三、仅用栈结构实现队列结构 四、总结 ------------------... 悠悠/ 2024年04月17日 05:55/ 0 赞/ 245 阅读
相关 栈和队列 20.[\[LeetCode\] Valid Parentheses 验证括号][LeetCode_ Valid Parentheses] 给定一个只包括 `'('`,`') Dear 丶/ 2024年02月19日 13:28/ 0 赞/ 195 阅读
相关 栈和队列 > 栈 是限定 仅在表尾 进行插入和删除操作的线性表 > > 队列 是只允许 在一端 进行 插入 操作、而在 另一端 进行 删除 操作的线性表 第一部分 相关定义 秒速五厘米/ 2023年07月14日 15:57/ 0 赞/ 257 阅读
相关 栈和队列 物理结构与逻辑结构 把物质层面的人体比作数据存储的物理结构,那么精神层面的人格则是数据存储的逻辑结构。逻辑结构是抽象的概念,它依赖于物理结构而存在。 ![在这里插入图 柔光的暖阳◎/ 2023年07月02日 03:24/ 0 赞/ 140 阅读
相关 栈和队列 栈是限定在表尾进行插入或删除的线性表.因此,对于栈来说,表尾端有其特殊的含义,称为`栈顶`,相应地,表头端称为`栈底`.不含任何元素的栈称为空栈. 和线性表类似,栈也 ゝ一纸荒年。/ 2023年07月01日 12:56/ 0 赞/ 101 阅读
相关 实现栈和队列、用栈实现队列,用队列实现栈。 一、实现一个栈 就是一个指针下标,入栈加,出栈减。 / 我的栈 / public class MySt 一时失言乱红尘/ 2023年02月16日 12:15/ 0 赞/ 149 阅读
相关 栈和队列 栈: package Suan; public class Stack { private int size; 迈不过友情╰/ 2022年06月18日 07:54/ 0 赞/ 303 阅读
相关 队列组合(栈和队列) 题目描述 设计算法以求解从集合\{1…n\}中选取k(k<=n)个元素的所有组合。例如,从集合\{1…4\}中选取2个元素的所有组合的输出结果为:1 2,1 3,1 ╰半橙微兮°/ 2022年03月31日 07:42/ 0 赞/ 428 阅读
还没有评论,来说两句吧...