色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

java棧和隊(duì)

Java棧和隊(duì)是常見的數(shù)據(jù)結(jié)構(gòu),它們都是一維線性結(jié)構(gòu)。

Java棧是一種先進(jìn)后出的數(shù)據(jù)結(jié)構(gòu),它可以理解為一堆盤子,新的盤子需要放在舊的盤子上方。棧有兩個(gè)基本操作,分別是進(jìn)棧和出棧。進(jìn)棧可以將數(shù)據(jù)壓入棧頂,而出棧則彈出棧頂?shù)臄?shù)據(jù)。

public class Stack {
private int[] data;
private int top;
private int size;
public Stack(int size) {
this.data = new int[size];
this.top = -1;
this.size = size;
}
// 進(jìn)棧
public boolean push(int value) {
if (top == size - 1) {
System.out.println("Stack is full");
return false;
}
data[++top] = value;
return true;
}
// 出棧
public int pop() {
if (top == -1) {
System.out.println("Stack is empty");
return -1;
}
return data[top--];
}
}

Java隊(duì)是一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu),它可以理解為一條隊(duì)伍,新的人需要排在隊(duì)尾,而離開隊(duì)伍的人則是隊(duì)首。

public class Queue {
private int[] data;
private int front;
private int rear;
private int size;
public Queue(int size) {
this.data = new int[size];
this.front = -1;
this.rear = -1;
this.size = size;
}
// 入隊(duì)
public boolean enqueue(int value) {
if (rear == size - 1) {
System.out.println("Queue is full");
return false;
}
data[++rear] = value;
if (front == -1) {
front = 0;
}
return true;
}
// 出隊(duì)
public int dequeue() {
if (front == -1) {
System.out.println("Queue is empty");
return -1;
}
int value = data[front];
if (front == rear) {
front = -1;
rear = -1;
} else {
front++;
}
return value;
}
}

Java棧和隊(duì)在實(shí)際應(yīng)用中具有廣泛的應(yīng)用,如計(jì)算表達(dá)式、操作系統(tǒng)的進(jìn)程調(diào)度、圖的深度優(yōu)先搜索和廣度優(yōu)先搜索等。