栈:一种特殊的线性表,只允许在一端进行插入和删除元素。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的元素遵守后进先先出的原则。
压栈:栈的插入操作。
出栈:栈的删除操作。
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出入队列;
入队列:进行插入操作的一段称为队尾。
出队列:进行删除操作的一端称为队头。
使用队列实现栈
class MyStack {
private LinkedList<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
//queue.addLast(x);
queue.addLast(x);
}
public int pop() {
int size = queue.size();
for(int i = 0 ; i <size-1;i++){
int v = queue.pollFirst(); //移除列表的第一个元素
queue.addLast(v); //从末尾入队列
}
return queue.pollFirst(); //此时队列的首元素就是栈顶元素
}
/** Get the top element. */
public int top() {
int size = queue.size();
for(int i = 0;i<size-1;i++){
int v = queue.pollFirst();
queue.addLast(v);
}
int v = queue.pollFirst();
queue.addLast(v);
return v;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
}
使用栈实现队列:使用一个备用栈实现队列
class MyQueue {
private Stack<Integer> s1 = new Stack<>();
private Stack<Integer> s2 = new Stack<>();
int front;
/** Initialize your data structure here. */
public MyQueue() {
}
/** Push element x to the back of queue. */
public void push(int x) {
if(s1.isEmpty())
front =x;
s1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(s2.isEmpty()){
while(!s1.isEmpty()){
s2.push(s1.pop());
}
}
return s2.pop();
}
/** Get the front element. */
public int peek() {
if(!s2.isEmpty()){
return s2.peek();
}
return front;
}
/** Returns whether the queue is empty. */
public boolean empty() {
return s1.isEmpty() && s2.isEmpty();
}
}
设计循环队列:
class MyCircularQueue {
private int[] data;
private int size;
private int head;
private int tail;
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
this.data = new int[k];
this.size = 0;
this.head = -1;
this.tail = -1;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if(size == data.length)
return false;
tail = (tail+1)%data.length;
data[tail] = value;
if(size==0)
head = tail;
size++;
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if(size==0)
return false;
head = (head+1)%data.length;
size--;
return true;
}
/** Get the front item from the queue. */
public int Front() {
if(size == 0)
return -1;
return data[head];
}
/** Get the last item from the queue. */
public int Rear() {
if(size==0)
return -1;
return data[tail];
}
/** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
return size==0;
}
/** Checks whether the circular queue is full or not. */
public boolean isFull() {
return size==data.length;
}
}
最小栈问题
class MinStack { //一个辅助栈 存栈中的最小值 若数据栈中值大于辅助栈 继续最小值压栈
private Stack<Integer> data = new Stack<>();
private Stack<Integer> helper = new Stack<>();
/** initialize your data structure here. */
public MinStack() {
}
public void push(int x) {
data.add(x);
// if(helper.isEmpty()||helper.peek()>=x){
//helper.add(x);
//}else{
// helper.add(helper.peek());
// }
if(helper.isEmpty()||helper.peek()>=x){
helper.add(x);
}
}
public void pop() {
// if(!data.isEmpty()){
// data.pop();
// helper.pop();
//}
if(!data.isEmpty()){
int top = data.pop();
if(top == helper.peek()){
helper.pop();
}
}
// if(data.peek()!=helper.peek()){
// data.pop();
// }else{
// data.pop();
// helper.pop();
// }
}
public int top() {
if(!data.isEmpty()){
return data.peek();
}
throw new RuntimeException("Empty Stack");
}
public int getMin() {
if(!helper.isEmpty()){
return helper.peek();
}
throw new RuntimeException("Empty Stack");
}
}