对于队列而言,其是先进先出,对于栈而言,是先进后出
栈
队列
注意:当用LinkedList作为Queue的实现类时,其同时具有List集合的add,remove方法和Queue的poll和peek方法,注意当其作为一个队列使用时,注意不要使用add和remove方法
队列三种实现方式
补充:快排代码
package Sort;
public class 快速排序 {
public static void main(String[] args) {
int[] arr = {6,1,2,7,9,3,4,5,10,8};
sort(arr, 0, arr.length-1);
print(arr);
}
public static void sort(int[] arr,int leftBound,int rightBound) {
if(leftBound >= rightBound) return;
int mid= partition(arr, leftBound, rightBound); //得到轴的位置
sort(arr, leftBound, mid-1); //左边排序
sort(arr, mid+1, rightBound); //右边排序
}
static int partition(int[] arr, int leftBound,int rightBound) {
int pivot = arr[rightBound]; //指定数组最右边的数是用来比较的值 轴
int left = leftBound;
int right = rightBound - 1;
while (left <= right) {
while(left <= right && arr[left] <= pivot) left++;
while(left <= right && arr[right] > pivot) right--;
if (left < right) {
//如果 左边的数比右边的数小 两个数交换
swap(arr, left, right);
}
}
//把left最后一次指向的位置与pivot(轴)的位置交换
swap(arr, left, rightBound);
return left; //返回轴的位置
}
static void swap(int[] arr,int i,int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static void print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}