数据结构 编写一个类,用两个栈实现队列,支持队列的基本操作(add 、poll、peek)

程序员代码面试指南(左程云)读书笔记

第一章

2.编写一个类,用两个栈实现队列,支持队列的基本操作(add 、poll、peek

import java.util.Stack;


public class MyStack_02_1 {
public Stack<Integer> stackPush;
public Stack<Integer> stackPop;
public MyStack_02_1(){
this.stackPush=new Stack<Integer>();
this.stackPop=new Stack<Integer>();
}
//add
public void add(int pushInt){
stackPush.push(pushInt);
}

//poll
public int poll(){
if(stackPop.empty()&&stackPush.empty()){
throw new RuntimeException("the stack is empty!");
}else if(stackPop.empty()){
while(!stackPush.empty()){
stackPop.push(stackPush.pop());
}
}
return stackPop.pop();

}

//peek
public int peek(){
if(stackPop.empty()&&stackPush.empty()){
throw new RuntimeException("the stack is empty!");
}else if(stackPop.empty()){
while(!stackPop.empty()){
stackPop.push(stackPush.pop());
}
}
return stackPop.peek();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 下面是一个实现队列的简单Java程序: ``` import java.util.LinkedList; import java.util.Queue; public class QueueExample { public static void main(String[] args) { Queue<Integer> queue = new LinkedList<>(); // 添加元素到队列 queue.add(10); queue.add(20); queue.add(30); System.out.println("队列元素:" + queue); // 获取队首元素但不删除 int head = queue.peek(); System.out.println("队首元素:" + head); System.out.println("队列元素:" + queue); // 获取队首元素并删除 head = queue.poll(); System.out.println("队首元素:" + head); System.out.println("队列元素:" + queue); } } ``` 运行结果: ``` 队列元素:[10, 20, 30] 队首元素:10 队列元素:[10, 20, 30] 队首元素:10 队列元素:[20, 30] ``` ### 回答2: 在Java中,我们可以使用数组或链表来实现队列。这里我以数组实现队列为例进行说明。 首先,我们需要定义一个Queue类来表示队列。该类包含一个数组来存储队列中的元素,以及两个指针front和rear来指示队列的头部和尾部。我们还可以定义一个变量size来记录队列中元素的个数,方便后续的操作。 一个简单的实现如下: ``` public class Queue { private int maxSize; // 队列的容量 private int[] queueArray; // 存储队列元素的数组 private int front; // 队头指针 private int rear; // 队尾指针 private int size; // 队列中元素的个数 public Queue(int maxSize) { this.maxSize = maxSize; queueArray = new int[maxSize]; front = 0; rear = -1; size = 0; } public boolean isEmpty() { return size == 0; } public boolean isFull() { return size == maxSize; } public void enqueue(int element) { if (!isFull()) { rear = (rear + 1) % maxSize; queueArray[rear] = element; size++; System.out.println("元素 " + element + " 入队成功"); } else { System.out.println("队列已满,无法入队"); } } public int dequeue() { if (!isEmpty()) { int element = queueArray[front]; front = (front + 1) % maxSize; size--; return element; } else { System.out.println("队列为空,无法出队"); return -1; } } public int peek() { if (!isEmpty()) { return queueArray[front]; } else { System.out.println("队列为空"); return -1; } } } ``` 我们可以在主函数中进行测试: ``` public static void main(String[] args) { Queue queue = new Queue(5); System.out.println("队列是否为空:" + queue.isEmpty()); queue.enqueue(1); queue.enqueue(2); queue.enqueue(3); System.out.println("队列的头元素:" + queue.peek()); System.out.println("队列是否已满:" + queue.isFull()); System.out.println("队列长度:" + queue.size); queue.dequeue(); System.out.println("队列的头元素:" + queue.peek()); System.out.println("队列长度:" + queue.size); } ``` 运行上述程序,可以看到队列的入队、出队和获取队头元素的操作都是按照队列的特性来进行的,从而实现队列的功能。 ### 回答3: 编写一个Java程序实现队列,需要定义一个Queue类来表示队列,以下是一个简单的实现示例: ```java public class Queue { private int maxSize; // 队列的最大容量 private int front; // 队头的索引 private int rear; // 队尾的索引 private int[] queueArray; // 存放队列元素的数组 public Queue(int size) { maxSize = size; queueArray = new int[maxSize]; front = 0; rear = -1; } public void insert(int value) { if (rear == maxSize - 1) { System.out.println("队列已满,无法插入新的元素!"); return; } queueArray[++rear] = value; } public int remove() { if (isEmpty()) { System.out.println("队列为空,无法移除元素!"); return -1; } int temp = queueArray[front++]; return temp; } public int peekFront() { return queueArray[front]; } public boolean isEmpty() { return (rear == front - 1); } } ``` 以上代码定义了一个Queue类,包含插入元素、移除元素、获取队头元素和判断队列是否为空的方法。在插入元素时,先检查队列是否已满;在移除元素时,先检查队列是否为空。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值