Review and summary data structure: 4 stacks

A stack is a first in last out ordered list. The insertion and deletion of restricted elements can only be at the same end, that is, the top of the stack, and the other end is a fixed end, which is called the bottom of the stack. This article mainly introduces the array implementation and linked list implementation of the stack.

1 array implementation

To implement using an array, first you need an array to store data, and use maxSize to represent the size of the array, and then use top to represent the subscript of the element currently at the top of the stack. When popping or pushing into the stack, it is the top and Its corresponding element is processed, top=-1 at the time of initialization, indicating that the stack is empty.
The idea is relatively simple, just go to the code directly.

package com.datastructure.stack;

public class ArrayStack implements MyStack {
    
    
    private int maxSize; //栈容量
    private int[] stack; //存放栈元素
    private int top;  //栈顶元素的下标

    public ArrayStack(int maxSize) {
    
    
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
        top = -1;  //栈空时 top = -1
    }

    public boolean isFull() {
    
    
        return top == maxSize - 1;  //数组满了就是栈满了
    }

    public boolean isEmpty() {
    
    
        return top == -1;  
    }

    //push的时候先确定栈没有满
    //然后top+1,并将新元素存放到该下标处
    public void push (int value) {
    
    
        if(isFull()) {
    
    
            System.out.println("栈空间已满");
            return;
        }
        top++;
        stack[top] = value;
    }

    //先返回当前top所指的元素
    //然后top-1,指向新的top元素所在位置
    public int pop() {
    
    
        if(isEmpty()) {
    
    
            System.out.println("栈空");
            throw new RuntimeException("栈空,无法弹出数据");
        }
        return stack[top--];
    }

    public void show() {
    
    
        if(isEmpty()) {
    
    
            System.out.println("栈空");
            return;
        }
        for(int i=top; i>-1; --i) {
    
    
            System.out.printf("stack[%d]=%d\n",i,stack[i]);
        }
    }
}

2 One-way linked list implementation

Each stack element is represented by a data node. A simple stack node must have value to store the value and next to store the next element.

class StackNode {
    
    
    int value; //值
    StackNode next; //指向下一个节点
    StackNode(int value) {
    
    
        this.value = value;
        this.next = null;
    }
}

The only thing that needs to be clarified is which node in the linked list should top correspond to? It depends on what conditions top needs to meet:

  • Frequent insertion and deletion, so it is also necessary to quickly access this node;
  • Subsequent nodes can be easily accessed from the top node.

For a one-way linked list, the operation of inserting and deleting at any position is the same, but the access speed is different. The access speed of the node at the head of the linked list is obviously faster. For the second condition, only the head node can meet it.
So we use a one-way linked list with a head node to implement the stack. top is the head node. When the top node of the stack is , it top.nextmeans top.next == nullthat the stack is empty. Here, it is assumed that the stack will not be full. Of course, the stack size can also be set.

package com.datastructure.stack;

public class LinkedStack implements MyStack{
    
    
    StackNode top;//top是正真top节点的前一个节点,类似于头节点
    LinkedStack() {
    
    
        top = new StackNode(-1);
    }
    //入栈
    //将新节点插入到头节点后面
    @Override
    public void push(int value) {
    
    
        if (isFull()) {
    
    
            System.out.println("栈满");
            return;
        }
        StackNode newNode = new StackNode(value);
        newNode.next = top.next;
        top.next = newNode;
        System.out.println("新增元素:" + value);
    }

    //出栈
    //将 top.next 指向原 top.next 的后一个节点
    @Override
    public int pop() {
    
    
        if (isEmpty()){
    
    
            System.out.println("栈空");
            throw new RuntimeException("栈空,无法弹出");
        }
        int value = top.next.value;
        top.next = top.next.next;
        return value;
    }

    @Override
    public void show() {
    
    
        if(isEmpty()) {
    
    
            System.out.println("栈空");
            return;
        }
        System.out.println("当前栈为");
        StackNode temp = top.next;
        while (temp != null) {
    
    
            System.out.println(temp.value);
            temp = temp.next;
        }
    }

    //栈空
    @Override
    public boolean isEmpty() {
    
    
        return top.next == null;
    }

    //这里不设置栈大小,假设栈始终不满
    @Override
    public boolean isFull() {
    
    
        return false;
    }
}

class StackNode {
    
    
    int value; //值
    StackNode next; //指向下一个节点
    StackNode(int value) {
    
    
        this.value = value;
        this.next = null;
    }
}

Guess you like

Origin blog.csdn.net/hejnhong/article/details/125525467