Jianzhi Offer 09 Use two stacks to solve the queue problem

title

Pre-knowledge

queue

Queue is an interface that implements queues in java. It has only 6 methods in total, and we generally only use 3 of them. The implementation classes of Queue are LinkedList and PriorityQueue. The most commonly used implementation class is LinkedList.

Insert picture description here

The 6 method classifications of Queue:

Push element (add): add(), offer() are the
same: the capacity is not exceeded, the element is pushed from the end of the queue, and the element pushed in is returned.
Difference: when the capacity is exceeded, the add() method will throw an exception, and offer() returns false

Pop-up element (delete): remove() and poll() are the
same: when the capacity is greater than 0, delete and return the deleted element at the head of the line.
Difference: when the capacity is 0, remove() will throw an exception, poll() returns false

Get the head element (not deleted): element() and peek() are the
same: when the capacity is greater than 0, both return the head element. But it is not deleted.
Difference: When the capacity is 0, element() will throw an exception, and peek() will return null.

In addition to the basic Collection operations, the queue also provides unique insert, extract, and check operations (as above). Each method has two forms: one throws an exception (when the operation fails), and the other returns a special value (null or false, depending on the operation). The latter form of insert operation is specifically designed for capacity-limited Queue implementations; in most implementations, the insert operation will not fail.

Throw an exception Return special value
insert add(e) offer(e)
delete remove() poll()
an examination element() peek()

Deque has three uses:

Ordinary queue (one end enters the other end):
Queue queue = new LinkedList()or Deque deque = new LinkedList()
double-ended queue (both ends can enter and exit)
Deque deque = new LinkedList()
stack
Deque deque = new LinkedList()
Deque stack operation methods: push(), pop(), peek().

Deque is a linear collection that supports inserting and removing elements at both ends. The name deque is an abbreviation for "double ended queue" and is usually pronounced "deck". Most Deque implementations have no fixed limit on the number of elements they can contain, but this interface supports both deques with capacity restrictions and deques without fixed size restrictions.

This interface defines methods for accessing elements at both ends of the deque. Provide methods for inserting, removing, and inspecting elements. Each method has two forms: one form throws an exception when the operation fails, and the other form returns a special value (null or false, depending on the operation). The latter form of insert operation is designed for use with capacity-limited Deque implementations; in most implementations, insert operations cannot fail.

Insert element

addFirst(): Insert an element to the head of the line, if the element is empty, NPE occurs

addLast(): Insert an element to the end of the queue, if it is empty, NPE occurs

offerFirst(): Insert an element to the head of the line, if the insertion is successful, return true, otherwise return false

offerLast(): Insert an element to the end of the queue, return true if the insertion is successful, otherwise return false

Remove element

removeFirst(): Return and remove the head element, if the element is null, NoSuchElementException will occur

removeLast(): Return and remove the element at the end of the queue. If the element is null, a NoSuchElementException will occur

pollFirst(): returns and removes the head element of the queue, if the queue has no elements, returns null

pollLast(): returns and removes the last element of the queue, if the queue has no elements, returns null

Get element

getFirst(): Get the head element but not remove it. If there is no element in the queue, NoSuchElementException will occur

getLast(): Get the last element of the queue but not remove it. If there is no element in the queue, a NoSuchElementException will occur

peekFirst(): Get the head element of the queue but do not remove it, if there is no element in the queue, it returns null

peekLast(): Get the last element of the queue but do not remove it, if there is no element in the queue, it returns null

Stack operation

pop(): Pop the elements in the stack, that is, return and remove the head element, which is equivalent to removeFirst(). If there are no elements in the queue, NoSuchElementException will occur

push(): Push an element into the stack, that is, add an element to the head of the queue, which is equivalent to addFirst(). If the element is null, NPE will occur, and if the stack space is limited, an IllegalStateException will occur

Jianzhi Offer 09 Use two stacks to solve the queue problem

import java.util.LinkedList;
import java.util.Queue;

public class CQueue {
    
    
    Queue<Integer> stack1 = null;
    Queue<Integer> stack2 = null;
    public CQueue() {
    
    
        stack1 = new LinkedList();
        stack2 = new LinkedList();
    }

    public void appendTail(int value) {
    
    
//        stack1作队列头
        stack1.add(value);
    }


    public int deleteHead() {
    
    
//        stack2作为队列尾
        int da = 0 ;
        if ( !stack2.isEmpty()){
    
    
            return stack2.remove();
        }else {
    
    
            if (stack1.isEmpty()){
    
    
                return -1;
                
            }else{
    
    
                while (stack1.size() != 0 ){
    
    
                    stack2.add(stack1.remove());
                    
                }
                return stack2.remove();
            }
        }

    }
}

Reprinted:
https://blog.csdn.net/devnn/article/details/82591349

https://blog.csdn.net/devnn/article/details/82716447

Guess you like

Origin blog.csdn.net/qq_43458555/article/details/108065590