Usage of stack and queue in Java
Stack implementation
Using Java's collection class Stack
boolean isEmpty();
// Determine whether the current stack is empty, equivalent to empty ();synchronized E peek();
// Get the current top elementSynchronized E pop();
// Get the current top element and deleteE push(E object);
// Add elements to the top of the stackSynchronized int search(Object o);
// Find the position of the element in the stack, counting from the bottom to the top of the stack
import java.util.Stack;
public class Solution{
public static void main(String[] args){
Stack<Integer> sk=new Stack<Integer>();
System.out.println(sk.isEmpty());//判断栈是否为空,true
for(int i=0;i<10;i++){
int e=sk.push(i+20);//将元素加入栈顶,并返回栈顶元素
System.out.println(i+": "+e);
}
System.out.println(sk.empty());//判断栈是否为空,false
System.out.println(sk.pop());//返回栈顶元素,并删除
System.out.println(sk.peek());//返回当前栈顶元素
System.out.println("first:"+sk.search(20));//查找栈中元素的位置
System.out.println("last:"+sk.search(29));
}
}
Use LinkedList to indirectly implement Stack
LinkedList
It is an inherited AbstractSequentialList
doubly linked list and can also be used as a stack, queue, or double-ended queue
- Stack method :
push(e);
Equivalent method :addFirst(e);
// Add elements to the top of the stack - Stack method :
pop();
Equivalent method :removeFirst();
// Get the current top element of the stack and delete it - Stack method :
peek();
Equivalent method :peekFirst();
// Get the top element of the current stack
import java.util.LinkedList;
import java.util.Stack;
public class Solution{
public static void main(String[] args){
LinkedList<Integer> sk=new LinkedList<Integer>();
System.out.println(sk.isEmpty());//判断是否为空,true
//System.out.println(sk.empty());错误,没有这个方法
for(int i=0;i<10;i++){
//int e=sk.addFirst(i+20);错误,没有返回值
sk.addFirst(i+20);//向栈顶添加元素
System.out.println(i+": "+(i+20));
}
System.out.println("peekFirst:"+sk.peekFirst());
System.out.println("removeFirst:"+sk.removeFirst());//获得栈顶元素并删除
System.out.println("peekFirst:"+sk.peekFirst());//获得栈顶元素
}
}
Queue implementation
Although there is an Queue
interface in Java, the implementation class of the queue is not given, but LinkedList
the Queue
interface is implemented . Because it LinkedList
is a doubly linked list, it is very convenient to implement all the functions of the queue. When using Queue
, try to avoid the use Collection
of the add()
sum remove()
method, because the add()
sum remove()
method will throw an exception when it fails; use offer()
to add elements, use poll()
to get and delete elements, you can judge whether it is by the return value.
Queue definition:Queue<E> queue=new LinkedList<E>();
- Queue Method :
offer(e);
Equivalent Method :offer(e)
/offerLast(e);
// add elements to the tail - Queue method :
poll(e);
equivalent method :poll(e)
/pollFirst(e);
// Get the first team and delete elements - Queue method :
peek(e);
equivalent method :peek(e)
/peeFirst(e);
// team to get the first element
- code 1
import java.util.LinkedList;
import java.util.Queue;
public class Solution{
public static void main(String[] args){
Queue<Integer> queue=new LinkedList<Integer>();
System.out.println(queue.isEmpty());//判断是否为空,true
//System.out.println(queue.empty());//错误,没有这个方法
for(int i=0;i<10;i++){
//int e=sk.addFirst(i+20);错误,没有返回值
queue.offer(i+20);//向队尾添加元素
System.out.println(i+": "+(i+20));
}
System.out.println("peekFirst:"+queue.peek());
System.out.println("removeFirst:"+queue.poll());//获得队首元素并删除
System.out.println("peekFirst:"+queue.peek());//获得队首元素
}
}
- code 2
import java.util.LinkedList;
import java.util.Queue;
public class Solution{
public static void main(String[] args){
LinkedList<Integer> queue=new LinkedList<Integer>();
System.out.println(queue.isEmpty());//判断是否为空,true
//System.out.println(queue.empty());//错误,没有这个方法
for(int i=0;i<10;i++){
//int e=sk.addFirst(i+20);错误,没有返回值
queue.offer(i+20);//向队尾添加元素
System.out.println(i+": "+(i+20));
}
System.out.println("peekFirst:"+queue.peek());
System.out.println("removeFirst:"+queue.poll());//获得队首元素并删除
System.out.println("peekFirst:"+queue.peek());//获得队首元素
}
}
- code 3
import java.util.LinkedList;
import java.util.Queue;
public class Solution{
public static void main(String[] args){
LinkedList<Integer> queue=new LinkedList<Integer>();
System.out.println(queue.isEmpty());//判断是否为空,true
//System.out.println(queue.empty());//错误,没有这个方法
for(int i=0;i<10;i++){
//int e=sk.addFirst(i+20);错误,没有返回值
queue.offerLast(i+20);//向队尾添加元素
System.out.println(i+": "+(i+20));
}
System.out.println("peekFirst:"+queue.peekFirst());
System.out.println("removeFirst:"+queue.pollFirst());//获得队首元素并删除
System.out.println("peekFirst:"+queue.peekFirst());//获得队首元素
}
}