Mobile development stack-Basic queue knowledge learning and basic queue knowledge
Stack
First-in-first-out and later-in-first-out
Package ch03; public class MyStack {// The underlying implementation is an array private long [] arr; private int top; // stack top/*** default constructor */public MyStack () {arr = new long [10]; top =-1 ;} /*** construction method with parameters. The parameter is the array initialization size */public MyStack (int maxsize) {arr = new long [maxsize]; top =-1 ;} /*** add data */public void push (int value) {arr [++ top] = value;}/*** remove data */public long pop () {return arr [top --];}/*** View data */public long peek () {return arr [top];} /*** determine whether it is empty */public boolean isEmpty () {return top =-1;}/*** determine whether it is full */public boolean isFull () {return top = arr. length-1 ;}}
Queue
First-in, first-out, and then-out
Package ch03;/** queue class */public class MyQueue {// The underlying layer uses the array private long [] arr; // the size of valid data private int elements; // private int front of the team head; // private int end of the team end;/*** default constructor */public MyQueue () {arr = new long [10]; elements = 0; front = 0; end =-1;}/*** construction method with parameters, the parameter is the size of the array */public MyQueue (int maxsize) {arr = new long [maxsize]; elements = 0; front = 0; end =-1;}/*** add data, insert */public void insert (long value) {arr [++ end] = value; elements ++;}/*** from the end of the team to delete the data, delete */public long remove () {elements --; return arr [front ++];}/*** from the team header to view data, view */public long peek () {return arr [front];}/*** from the queue header to determine whether it is null */public boolean isEmpty () {return elements = 0;}/*** determines if it is full */public boolean isFull () {return elements = arr. length ;}}
Cyclic queue
When inserting data, when the end of the team points to the length of the array, set the end value to-1.
When deleting data, when the length of the array is exceeded, set the value to 0.
Package ch03;/** queue class */public class MyCycleQueue {// The underlying layer uses the array private long [] arr; // the size of valid data private int elements; // private int front of the team head; // private int end of the team end;/*** default constructor */public MyCycleQueue () {arr = new long [10]; elements = 0; front = 0; end =-1;}/*** construction method with parameters, the parameter is the size of the array */public MyCycleQueue (int maxsize) {arr = new long [maxsize]; elements = 0; front = 0; end =-1;}/*** add data, insert */public void insert (long value) {if (end = arr. length-1) {end =-1;} arr [++ end] = value; elements ++;}/*** delete data, delete */public long remove () {long value = arr [front ++]; if (front = arr. length) {front = 0;} elements --; return value;}/*** View data, View data from the team header */public long peek () {return arr [front];}/*** determines whether it is empty */public boolean isEmpty () {return elements = 0 ;} /*** determine if it is full */public boolean isFull () {return elements = arr. length ;}}