Talking about stacks and queues (java version)

stack concept

Stack: A special linear list that allows insertion and removal of elements only at one fixed end. One end where data insertion and deletion are performed is called the top of the stack, and the other end is called the bottom of the stack. The data elements in the stack follow the LIFO (Last In First Out) principle.

push and pop

Push stack: It is the insertion operation of the stack, also called push, push, and put to the top
of the stack. Pop: The deletion operation of the stack is called pop, that is, the elements at the top of the stack go out.

insert image description here

Implementation stack

It can be implemented by a sequence list or a linked list. The sequence list is relatively simple. I use the sequence list to simulate the implementation here, which is easier to understand.

public class MyStack {
    
    
    public int[] elem; // 模拟栈的数组
    public int usedSize; //栈元素

    public MyStack(){
    
    
        this.elem = new int[5]; // 给5个元素的空间
    }

    public void push(int val){
    
      //模拟进栈
        if(isFull()){
    
     //判断栈没有
            this.elem = Arrays.copyOf(elem,2 * elem.length); // 如果满了,我们这里给二倍扩容
        }

        this.elem[usedSize] = val; //数组下标从0开始,我们这里让进栈的元素加到末尾
        usedSize++;           //进栈元素之后记得让原来的元素加一
    }

    public boolean isFull(){
    
     //判断栈满没有
        return this.usedSize == elem.length;
    }

    public int pop(){
    
      //模拟出栈操作
        if(isEmpty()){
    
      //判断栈元素为不为空
            throw new RuntimeException("栈为空!"); //栈为空后,抛一个异常出去,表明栈为空
        }
        int oldVal = this.elem[usedSize-1]; // 记录栈顶的元素
        usedSize--;  //直接让元素减一,栈顶元素就消失了
        return oldVal;  //返回刚刚我们记录的栈顶元素
    }

    public boolean isEmpty(){
    
     //判断栈元素为不为空
        return usedSize == 0;
    }
    
    public int peek(){
    
    
        if(isEmpty()){
    
      //判断栈元素为不为空
            throw new RuntimeException("栈为空!"); //栈为空后,抛一个异常出去,表明栈为空
        }
        return this.elem[usedSize-1];//直接返回栈顶元素
    }
}

Test the stack just mocked:

public class TestDemo {
    
    
    public static void main(String[] args) {
    
    
        MyStack stack = new MyStack();
        stack.push(5);
        System.out.println(stack.pop());
        stack.push(4);
        System.out.println(stack.peek());
        stack.push(3);
        System.out.println(stack.peek());
        stack.push(2);
        System.out.println(stack.peek());
        stack.push(1);
        System.out.println(stack.peek());

    }
}

insert image description here

public class TestDemo {
    
    
    public static void main(String[] args) {
    
    
        MyStack stack = new MyStack();
        stack.push(5);
        System.out.println(stack.pop());
        stack.push(4);
        System.out.println(stack.peek());
        stack.push(3);
        System.out.println(stack.peek());
        stack.push(2);
        System.out.println(stack.peek());
        stack.push(1);
        System.out.println(stack.peek());
        stack.pop();  //这儿先出了一个栈顶元素1
        System.out.println("出的第二个栈顶元素: "+stack.pop());  //接着这儿又出了一个栈顶元素 并且打印  为2

    }
}

insert image description here

queue concept

Queue: A special linear table that only allows data insertion operations at one end and deletion data operations at the other end. The queue has a first-in-first-out (FirstIn First Out)
entry queue: the end of the insertion operation is called the tail (Tail/Rear) )
out of the queue: the end of the deletion operation is called the head of the queue (Head/Front)
insert image description here

insert image description here

Common queue simulation implementation

I use a singly linked list to implement it here, which is relatively simple and easy to understand:

class Node{
    
    
    public int val; //节点值
    public Node next; //指向下一个节点

    public Node(int val){
    
    
        this.val = val;
    }

}

public class MyQueue {
    
    

    public Node head; //头结点(队头)
    public Node last; //尾结点(队尾)

    public void offer(int x){
    
     // 模拟入队
        if(head == null){
    
      //第一次入队头队尾都为空
            head = new Node(x);
            last = head;
        }else{
    
     //从队尾入队
            last.next = new Node(x);  //原队尾节点指向新节点
            last = last.next; //新节点变为尾结点(队尾)
        }
    }

    public int poll(){
    
      //模拟出队
        if(head == null){
    
      //队列为空,返回空
            throw new RuntimeException("队列为空");
        }else {
    
    
            Node node = head; //记录头结点(队头)
            head = head.next; //原头结点指向下一个节点
            return node.val; //返回记录的头结点(队头)
        }
    }


    public int peek() {
    
     //模拟队头元素
        if (head == null) {
    
      //队列为空,返回空
            throw new RuntimeException("队列为空");
        } else {
    
    
            return head.val;  //直接返回队头
        }
    }
}

test:

public class TestDemo {
    
    

    public static void main(String[] args) {
    
    
        MyQueue queue = new MyQueue();
        queue.offer(1);
        System.out.println(queue.peek());
        queue.offer(2);
        System.out.println(queue.peek());
        queue.offer(3);
        System.out.println(queue.poll());
        System.out.println(queue.poll());
    }
}

insert image description here

Guess you like

Origin blog.csdn.net/chenbaifan/article/details/123533392
Recommended