1.栈
package com.thinkofdatastruct.fourunit; /** * Created by happy on 2019/05/15 */ public class StackDemo { //数组实现 private long arr[]; private int top; //默认构造方法 public StackDemo(){ } //带参数的构造方法 public StackDemo(int maxSize){ arr = new long[maxSize]; top = -1; } //加入数据 public void push(int data){ arr[++top]= data; } //pop移除数据 public long pop(){ return arr[top--]; } //弹出所有数据 public void ouput(){ while(top>=0){ System.out.print(arr[top--]+" "); } } //查看数据 public long[] LookTopData(){ return arr; } //判断是否为空 public boolean isEmpty(){ return top == -1; } //判断是否满 public boolean isFull(){ return top == arr.length-1; } /** * 栈:先进后出,线性结构 * @param args */ public static void main(String[] args) { StackDemo sd = new StackDemo(10); sd.push(100); sd.push(101); sd.push(102); sd.push(103); sd.push(104); sd.push(105); sd.push(106); sd.push(107); sd.push(108); sd.push(109); /** * 查看结果 */ for (long l : sd.LookTopData()) { System.out.print(" "+l); } System.out.println("\n"+sd.pop()); for (long l : sd.LookTopData()) { System.out.print(" "+l); } } }
2.队列
package com.thinkofdatastruct.fourunit; /** * Created by happy on 2019/05/15 */ public class LineDemo { private long[] arr; private int top; private int front; private int end; //构建无参 public LineDemo(){} public LineDemo(int maxSize){ arr = new long[maxSize]; top = 0; front =0; end = -1; } //添加数据 public void insert(long data){ if(end ==arr.length-1){ end = -1; } arr[++end] = data; top++; } //删除数据,从对头删除 public long remove(){ long data = arr[front++]; if (front ==arr.length){ front = 0; } top--; return data; } public long[] look(){ return arr; } public boolean isEmpty(){ return top ==0; } public boolean isFull(){ return top == arr.length; } /** * 队列先进先出,线性结构 * @param args */ public static void main(String[] args) { LineDemo ld = new LineDemo(10); ld.insert(100); ld.insert(101); ld.insert(102); ld.insert(103); ld.insert(104); ld.insert(105); ld.insert(106); ld.insert(107); ld.insert(108); ld.insert(109); } }
启蒙于:https://blog.csdn.net/likunkun__/article/details/79950346
本系列只为自己查漏补缺,欢迎批评指正。