1.栈
2.队列
栈:一种先进后出的线性数据结构。也是一种特殊的线性表结构,其特殊性在于限定插入和删除数据元素的操作只能在线性表的一端进行。
1 public class Stack{ 2 private long[] arr; 3 private int top; 4 5 public Stack(){ 6 arr = new long[10]; 7 top = -1; 8 } 9 public Stack(int maxSize){ 10 arr = new long[maxSize]; 11 top = -1; 12 } 13 //insert 14 public void push(int value){ 15 arr[++top] = value; 16 } 17 //delete 18 public long pop(){ 19 return arr[top--]; 20 } 21 //show 22 public long peek(){ 23 return arr[top]; 24 } 25 //isEmpty 26 public boolean isEmpty(){ 27 return top == -1; 28 } 29 //isFull 30 public boolean isFull(){ 31 return top == arr.length-1; 32 } 33 }
队列:队列(Queue)也是一种运算受限的线性表,它的运算限制与栈不同,是两头都有限制,插入只能在表的一端进行(只进不出),而删除只能在表的另一端进行(只出不进),允许删除的一端称为队尾(rear),允许插入的一端称为队头 (Front),队列的操作原则是先进先出的,所以队列又称作FIFO表(First In First Out)
1 public class Queue{ 2 private long[] arr; 3 private int length; 4 private int front; 5 private int end; 6 7 public Queue(){ 8 arr = new long[10]; 9 length=0; 10 front=0; 11 end=-1; 12 } 13 public Queue(int maxSize){ 14 arr = new long[maxSize] 15 length=0; 16 front=0; 17 end=-1; 18 } 19 public void insert(long value){ 20 arr[++end] = value; 21 length++; 22 } 23 //循环插入 24 public void insert(long value){ 25 if(end = arr.length-1){ 26 end = -1; 27 } 28 arr[++end] = value; 29 length++; 30 } 31 public long remove(){ 32 length--; 33 return arr[front++]; 34 } 35 //循环删除 36 public long remove(){ 37 long value = arr[front++]; 38 if(front == arr.length-1){ 39 front=0; 40 } 41 } 42 public long peek(){ 43 return arr[front]; 44 } 45 public boolean isEmpty(){ 46 return length == 0; 47 } 48 public boolean isFull(){ 49 return length == arr.length; 50 } 51 }