文章标题
来源:互联网 发布:张良一生 知乎 编辑:程序博客网 时间:2024/06/08 12:09
第三讲《栈和队列》
栈
package ch03;public class MyStack { //底层实现是一个数组 private long[] arr; //代表一个栈顶 private int top; /** * 默认的构造方法 */ public MyStack() { arr = new long[10]; //初始化的时候栈顶是没有任何数据的 top = -1; } /** * 带参数的构造方法,参数为数组初始化大小 */ public MyStack(int maxsize) { arr = new long[maxsize]; top = -1; } /** * 往栈里面添加数据(压栈) */ public void push(int value) { //首先让top递增 让 arr[0]=? arr[1]=? arr[++top] = value; } /** * 移除数据 */ public long pop() { //top递减 return arr[top--]; } /** * 查看数据 */ public long peek() { return arr[top]; } /** * 判断是否为空 */ public boolean isEmpty() { //=-1代表为空 return top == -1; } /** * 判断栈是否满 */ public boolean isFull() { return top == arr.length - 1; }}--------------------------------------------------------package ch03;public class TestMyStack { public static void main(String[] args) { //初始值为4,有四个数据 MyStack ms = new MyStack(4); ms.push(23); ms.push(12); ms.push(1); ms.push(90); System.out.println(ms.isEmpty());//false 不为空 System.out.println(ms.isFull());//true 满了 System.out.println(ms.peek());//查看栈顶结果为:90 //一直取数据 直到为空 while(!ms.isEmpty()) { System.out.print(ms.pop() + ","); //90,1,12,23 先进后出 } System.out.println(ms.isEmpty());//true System.out.println(ms.isFull());//false 数据全部弹出 }}
队列
package ch03;/* * 列队: * 1.先进先出,后进后出(类比买票) */public class MyQueue { //底层是一个数组来保存数据 private long[] arr; //有效数据的大小 //什么是有效数据大小?类比排队的长度 private int elements; //列队的对头 private int front; //列队的对尾 private int end; /** * 默认的构造方法 */ public MyQueue() { //初始化大小为10 arr = new long[10]; elements = 0; front = 0; //为啥对尾为-1? end = -1; } /** * 带参数的构造方法 */ public MyQueue(int maxsize) { arr = new long[maxsize]; elements = 0; front = 0; end = -1; } /** * 添加数据 从对尾插入(类比买票时排队) */ public void insert(long value) { arr[++end] = value; elements++; } /** *删除数据,从对头删除 */ public long remove() { elements--; return arr[front++]; } /** * 查看数据,从对头查看 */ public long peek() { return arr[front]; } /** * 判断是否为空 */ public boolean isEmpty() { //当有效元素为0时 为空 return elements == 0; } /** * 判断是否队列满了 */ public boolean isFull() { return elements == arr.length; }}--------------------------------------------------------package ch03;public class TestMyQueue { public static void main(String[] args) { MyCycleQueue mq = new MyCycleQueue(4); mq.insert(23); mq.insert(45); mq.insert(13); mq.insert(1); System.out.println(mq.isFull());//true System.out.println(mq.isEmpty());//false System.out.println(mq.peek());//23 对头是23(最先插入的) //只要不为空就循环移除 while (!mq.isEmpty()) { System.out.print(mq.remove() + " "); //打印结果:23 45 13 1 符合先进先出策略 } //此时为空,如果再添加不满的数据,不是循环队列的话会报错 mq.ins`这里写代码片`ert(23); mq.insert(45); mq.insert(13); mq.insert(1); while (!mq.isEmpty()) { System.out.print(mq.remove() + " "); } }}
循环队列
“`
package ch03;
/*
* 列队类:循环队列
*/
public class MyCycleQueue {
//底层使用数组
private long[] arr;
//有效数据的大小
private int elements;
//队头
private int front;
//队尾
private int end;
/** * 默认构造方法 */public MyCycleQueue() { arr = new long[10]; elements = 0; front = 0; end = -1;}/** * 带参数的构造方法,参数为数组的大小 */public MyCycleQueue(int maxsize) { arr = new long[maxsize]; elements = 0; front = 0; end = -1;}/** * 添加数据,从队尾插入 */public void insert(long value) { if(end == arr.length - 1) { end = -1; } arr[++end] = value; elements++;}/** * 删除数据,从队头删除 */public long remove() { //首先取得要删除的数据 long value = arr[front++]; if(front == arr.length) { front = 0; } elements--; return value;}/** * 查看数据,从队头查看 */public long peek() { return arr[front];}/** * 判断是否为空 */public boolean isEmpty() { return elements == 0;}/** * 判断是否满了 */public boolean isFull() { return elements == arr.length;}
}
“`## 第三讲《栈和队列》 ##
阅读全文
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 难过TAT
- 欢迎使用CSDN-markdown编辑器
- 文章标题
- org.apache.jasper.el.ELContextImpl cannot be cast to org.apache.jasper.runtime.ELContextImpl
- Find The Bone (Codeforces-796B)
- 文章标题
- java动态代理
- MySQL常用命令
- 文章标题
- 向EBS中注册表和字段脚本
- 文章标题
- 【JavaSE笔记】多线程(二)多线程
- HDU
- hdu 4751 染色法判断二分图