Stack and queue usage in Java

Usage of stack and queue in Java

Stack implementation

Using Java's collection class Stack

  1. boolean isEmpty();// Determine whether the current stack is empty, equivalent to empty ();
  2. synchronized E peek();// Get the current top element
  3. Synchronized E pop();// Get the current top element and delete
  4. E push(E object);// Add elements to the top of the stack
  5. Synchronized 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));
    }
}

Insert picture description here

Use LinkedList to indirectly implement Stack

LinkedListIt is an inherited AbstractSequentialListdoubly linked list and can also be used as a stack, queue, or double-ended queue

  1. Stack method : push(e); Equivalent method : addFirst(e);// Add elements to the top of the stack
  2. Stack method : pop(); Equivalent method : removeFirst();// Get the current top element of the stack and delete it
  3. 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());//获得栈顶元素
    }
}

Insert picture description here

Queue implementation

Although there is an Queueinterface in Java, the implementation class of the queue is not given, but LinkedListthe Queueinterface is implemented . Because it LinkedListis a doubly linked list, it is very convenient to implement all the functions of the queue. When using Queue, try to avoid the use Collectionof 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>();

  1. Queue Method : offer(e); Equivalent Method : offer(e)/ offerLast(e);// add elements to the tail
  2. Queue method : poll(e); equivalent method : poll(e)/ pollFirst(e);// Get the first team and delete elements
  3. 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());//获得队首元素
    }
}

Insert picture description here

  • 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());//获得队首元素
    }
}

Insert picture description here

  • 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());//获得队首元素
    }
}

Insert picture description here

Published 395 original articles · won 130 · 200,000 views +

Guess you like

Origin blog.csdn.net/qq_40507857/article/details/86547895