队列:先进先出的一种思想,就和我们现实世界排队一样,先到的人先处理。
队列基本操作:长度,是否为空,是否为满,遍历显示,添加元素,获取对头元素
实现方式:数组,链表,两个栈
创建队列类,包含属性及方法
public class ArrayQueue {
private int maxSize;
private int front;
private int rear;
private int[] arr;
//创建队列的构造器
public ArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
arr = new int[maxSize];
front = -1;//指向队列头部,front指向队列头的前一个位置
rear = -1;//指向队列尾部
}
//队列是否满了
public boolean isFull() {
return rear == maxSize - 1;
}
//队列是否为空
public boolean isEmpty(){
return rear==front;
}
//往队列中添加数
public void add(int n){
//首先判断是否满了
if(isFull()){
return;
}
rear++;
arr[rear]=n;
}
//获取队头的数据
public int get(){
//判断是否为空
if(isEmpty()){
System.out.println("队列为空!");
}
front++;
return arr[front];
}
//遍历队列
public void show(){
if(isEmpty()){
System.out.println("队列为空!");
return;
}
for (int i=0;i<arr.length;i++){
System.out.printf("arr[%d]=%d\n",i,arr[i]);
}
}
//获取队头的数据
public int head(){
if(isEmpty()){
throw new RuntimeException("队列空的,不能取值");
}
return arr[front+1];
}
}
public class LinkedQueueTest {
public static void main(String[] args) {
LinkedQueue linkedQueue =new LinkedQueue();
linkedQueue.add("zico");
linkedQueue.add("DEAN");
linkedQueue.add("zion");
linkedQueue.add("loco");
linkedQueue.add("simon");
linkedQueue.add("gray");
linkedQueue.printQueue();
linkedQueue.peek();
linkedQueue.pop();
linkedQueue.printQueue();
linkedQueue.peek();
linkedQueue.length();
}
}
// 节点类
class Node {
public String data;
public Node next;
// 构造器
public Node() {
this.data = null;
this.next = null;
}
}
// 链表队列类
// 链表不指定长度
class LinkedQueue {
public Node front;
public Node rear;
public int maxSize;
// 构造函数
public LinkedQueue() {
Node node = new Node();
front = rear = node;
maxSize = 0;
}
// 队列是否为空
public boolean isEmpty() {
//两种方式均可
//return front == rear;
return front.next == null;
}
// 入队列
public void add(String name) {
Node temp = new Node();
temp.data = name;
rear.next = temp;
rear = temp;
maxSize++;
}
// 出队列
public void pop() {
if (isEmpty()) {
System.out.println("队列为空!");
return;
}
Node temp = front.next;
System.out.println("出队列:" + temp.data);
front.next = temp.next;
if (rear == temp) {
rear = front;
}
maxSize--;
}
// 队列长度
public void length() {
System.out.println("队列长度:" + maxSize);
}
//遍历队列
public void printQueue() {
for (Node current = front.next; current != null; current = current.next) {
System.out.print(current.data + " ");
}
System.out.println();
}
// 队列头元素
public void peek(){
System.out.println("头元素:"+ front.next.data);
}
}
import java.util.Stack;
public class StackQueueTest {
public static void main(String[] args) {
// Your MyQueue object will be instantiated and called as such:
MyQueue obj = new MyQueue();
obj.push(1);
int param_2 = obj.pop();
System.out.println(param_2);
obj.push(2);
int param_3 = obj.peek();
System.out.println(param_3);
boolean param_4 = obj.empty();
System.out.println(param_4);
}
}
class MyQueue {
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
/**
* Initialize your data structure here.
*/
public MyQueue() {
}
/**
* Push element x to the back of queue.
*/
public void push(int x) {
stack1.push(x);
}
/**
* Removes the element from in front of queue and returns that element.
*/
public int pop() {
if (stack2.size() == 0) {
while (!stack1.isEmpty()) {
int temp = stack1.peek();
stack2.push(temp);
stack1.pop();
}
}
int res = stack2.peek();
stack2.pop();
return res;
}
/**
* Get the front element.
*/
public int peek() {
if (stack2.size() == 0) {
while (!stack1.isEmpty()) {
int temp = stack1.peek();
stack2.push(temp);
stack1.pop();
}
}
int res = stack2.peek();
return res;
}
/**
* Returns whether the queue is empty.
*/
public boolean empty() {
return stack1.isEmpty() &stack2.isEmpty();
}
}
小组队列