文章目录
栈是empty()
队列是isEmpty()
1. JDK:Stack()
数组实现栈
public static class ArrayStack {
private Integer[] arr;
private Integer size;
public ArrayStack(int initSize) {
if (initSize < 0) {
throw new IllegalArgumentException("The init size is less than 0");
}
arr = new Integer[initSize];
size = 0;
}
public Integer peek() {
if (size == 0) {
return null;
}
return arr[size - 1];
}
public void push(int obj) {
if (size == arr.length) {
throw new ArrayIndexOutOfBoundsException("The queue is full");
}
arr[size++] = obj;
}
public Integer pop() {
if (size == 0) {
throw new ArrayIndexOutOfBoundsException("The queue is empty");
}
return arr[--size];
}
}
2. JDK:队列
element()和remove()在队列为空的时候,会抛出异常。
数组实现队列
public static class ArrayQueue {
private Integer[] arr;
private Integer size;
private Integer first;
private Integer last;
public ArrayQueue(int initSize) {
if (initSize < 0) {
throw new IllegalArgumentException("The init size is less than 0");
}
arr = new Integer[initSize];
size = 0;
first = 0;
last = 0;
}
//插入数据
public boolean offer(Int obj){
if (size == arr.length) {
throw false;
}
size++;
arr[last] = obj;
last = last == arr.length - 1 ? 0:last + 1;
return true;
}
//返回队头但不删除,队列为空返回异常
public Integer element(){
if(size == 0)
throw new NoSuchElementException("The queue is empty");
return arr[first];
}
//返回队头但不删除,队列为空返回null
public Integer peek() {
if (size == 0) {
return null;
}
return arr[first];
}
//返回对头并删除,队列为空返回null
public Integer poll(){
if(size == 0)
return null;
size--;
int temp = first;
first = first == arr.length - 1 ? 0 : first + 1;
return arr[tmp];
}
//返回对头并删除,队列为空返回null
public Integer remove(){
if(size == 0)
throw new NoSuchElementException("The queue is empty");
size--;
int temp = first;
first = first == arr.length - 1 ? 0 : first + 1;
return arr[tmp];
}
}
例题:实现一个特殊的栈,在实现栈的基本功能的基础上,在实现返回占中最小元素的操作
要求:
1,pop, push,getMin操作的时间复杂度都是O(1)
2,设计的栈类型可以使用现成的数据结构。
MyStack2好理解一些
public static class MyStack1 {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack1() {
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public void push(int newNum) {
if (this.stackMin.isEmpty()) {
this.stackMin.push(newNum);
} else if (newNum <= this.getmin()) {
this.stackMin.push(newNum);
}
this.stackData.push(newNum);
}
public int pop() {
if (this.stackData.isEmpty()) {
throw new RuntimeException("Your stack is empty.");
}
int value = this.stackData.pop();
if (value == this.getmin()) {
this.stackMin.pop();
}
return value;
}
public int getmin() {
if (this.stackMin.isEmpty()) {
throw new RuntimeException("Your stack is empty.");
}
return this.stackMin.peek();
}
}
public static class MyStack2 {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack2() {
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public void push(int newNum) {
if (this.stackMin.isEmpty()) {
this.stackMin.push(newNum);
} else if (newNum < this.getmin()) {
this.stackMin.push(newNum);
} else {
int newMin = this.stackMin.peek();
this.stackMin.push(newMin);
}
this.stackData.push(newNum);
}
public int pop() {
if (this.stackData.isEmpty()) {
throw new RuntimeException("Your stack is empty.");
}
this.stackMin.pop();
return this.stackData.pop();
}
public int getmin() {
if (this.stackMin.isEmpty()) {
throw new RuntimeException("Your stack is empty.");
}
return this.stackMin.peek();
}
}
如何仅使用栈结构实现队列结构
出对头:
peek返回不删,空返回null
element返回不删,空异常
poll返回并删,空返回null
remove()返回并删,空异常
栈中的pop(), 空异常
思路:
队列操作
offer:进栈直接进左栈(pushStack)
peek,poll,element,remove: 直接从pop弹出
注意:从pushStack倒栈要全部倒完进popStack即可
public static class TwoStacksQueue {
private Stack<Integer> stackPush; //第一个进栈
private Stack<Integer> stackPop; //第二个栈出栈,直到为空,才能让第一个栈弹进第二个栈
public TwoStacksQueue() {
stackPush = new Stack<Integer>();
stackPop = new Stack<Integer>();
}
//进队列
public void push(int pushInt) {
stackPush.push(pushInt);
}
//出队列且删除
public int poll() {
if (stackPop.empty() && stackPush.empty()) {
throw null;
} else if (stackPop.empty()) {
while (!stackPush.empty()) { //全部进第二个栈
stackPop.push(stackPush.pop());
}
}
return stackPop.pop();
}
public int remove() {
if (stackPop.empty() && stackPush.empty()) {
throw new RuntimeException("Queue is empty!");
} else if (stackPop.empty()) {
while (!stackPush.empty()) { //全部进第二个栈
stackPop.push(stackPush.pop());
}
}
return stackPop.pop();
}
//出队列不删除,同上,只是返回不一样
public int peek() {
if (stackPop.empty() && stackPush.empty()) {
throw null;
} else if (stackPop.empty()) {
while (!stackPush.empty()) {
stackPop.push(stackPush.pop());
}
}
return stackPop.peek();
}
public int element() {
if (stackPop.empty() && stackPush.empty()) {
throw new RuntimeException("Queue is empty!");
} else if (stackPop.empty()) {
while (!stackPush.empty()) {
stackPop.push(stackPush.pop());
}
}
return stackPop.peek();
}
}
如何仅用队列结构实现栈结构
思路:
push:进队列,直接进左队列
pop和peek: 出队列,把左队列全部出队列进入辅助队列,只留一个元素。把左队列和辅助队列引用互换。
注:出队列,左队列要留一个元素即可
empty: 判断左队列是否为空。
public static class TwoQueuesStack {
private Queue<Integer> queue;
private Queue<Integer> help;
public TwoQueuesStack() {
queue = new LinkedList<Integer>();
help = new LinkedList<Integer>();
}
public void push(int pushInt) {
queue.add(pushInt);
}
//peek不删,空返回null
public int peek() {
if (queue.isEmpty()) {
throw null;
}
while (queue.size() != 1) {
help.add(queue.poll());
}
int res = queue.poll();
help.add(res);
swap();
return res;
}
public int pop() {
if (queue.isEmpty()) {
throw new RuntimeException("Stack is empty!");
}
while (queue.size() > 1) {
help.add(queue.poll());
}
int res = queue.poll();
swap();
return res;
}
private void swap() {
Queue<Integer> tmp = help;
help = queue;
queue = tmp;
}
}
Just test, you can ignore this code.
import java.util.Stack;
public class TwoStack_Queue {
public static class MyQueue{
Stack<Integer> pushStack = new Stack<>();
Stack<Integer> popStack = new Stack<>();
public boolean offer(int num){
pushStack.push(num);
return true;
}
public Integer peek(){
if(pushStack.empty() && popStack.empty()) {
return null;
}
if(popStack.empty()){
while(!pushStack.empty()){
popStack.push(pushStack.pop());
}
}
return popStack.peek();
}
public Integer element(){
if(pushStack.empty() && popStack.empty()) {
throw new RuntimeException("The Queue is empty!");
}
if(popStack.empty()){
while(!pushStack.empty()){
popStack.push(pushStack.pop());
}
}
return popStack.peek();
}
public Integer poll(){
if(pushStack.empty() && popStack.empty()) {
return null;
}
if(popStack.empty()){
while(!pushStack.empty()){
popStack.push(pushStack.pop());
}
}
return popStack.pop();
}
public Integer remove(){
if(pushStack.empty() && popStack.empty()) {
throw new RuntimeException("The queue is empty!");
}
if(popStack.empty()){
while(!pushStack.empty()){
popStack.push(pushStack.pop());
}
}
return popStack.pop();
}
public boolean empty(){
if(pushStack.empty() && popStack.empty())
return false;
return true;
}
}
public static void main(String[] args) {
TwoStack_Queue.MyQueue queue = new TwoStack_Queue.MyQueue();
Integer[] nums = {1,2,3,4,5};
for(int i = 0; i < nums.length;i++){
queue.offer(nums[i]);
}
System.out.println(queue.peek());
while(queue.empty())
System.out.print(queue.poll() + " ");
}
}
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class TwoQueue_Stack {
public static class MyStack{
Queue<Integer> pushQueue; //用链表实现的
Queue<Integer> help;
public MyStack(){
pushQueue = new LinkedList<Integer>();
help = new LinkedList<Integer>();
}
public void push(int num){
pushQueue.offer(num);
}
public Integer peek(){
if(pushQueue.isEmpty()){
return null;
}
if(pushQueue.size() == 1)
return pushQueue.peek();
while(pushQueue.size()!=1){
help.offer(pushQueue.poll());
}
int temp = pushQueue.peek();
help.offer(pushQueue.poll());
swap();
return temp;
}
public Integer pop(){
if(pushQueue.isEmpty()){
throw new RuntimeException("The Queue is empty");
}
if(pushQueue.size() == 1)
return pushQueue.poll();
while(pushQueue.size()!=1){
help.offer(pushQueue.poll());
}
int temp = pushQueue.poll();
swap();
return temp;
}
public boolean empty(){
if(pushQueue.size() == 0)
return false;
return true;
}
private void swap(){
Queue<Integer> temp;
temp = pushQueue;
pushQueue = help;
help = temp;
}
}
public static void main(String[] args) {
TwoQueue_Stack.MyStack stack = new TwoQueue_Stack.MyStack();
int[] a = {1,2,3,4,5};
for(int i = 0; i < a.length; i++){
stack.push(a[i]);
}
System.out.println(Arrays.toString(a));
System.out.println("栈顶元素为:" + stack.peek());
while(stack.empty()){
System.out.print(stack.pop() + " ");
}
}
}