Array and linked list implementation of stack

1. Features and Common Operations

        A stack is a dynamic collection. As a basic data type in a data structure, it implements a last-in, first-out (last-in, first-out, abbreviated as LIFO) strategy.

        The insert operation on the stack is often called push, and the delete operation without parameters is often called pop. The stack structure is similar to the plates stored in the kitchen in our life, and the last plate to be pushed is at the top of the plate. When we need to use the plate, the first one to take out (pop) is also the one placed at the top.

2. Two implementations

1, using an array to achieve

        We define an array with a container size of 15, and define an index top pointing to the next stack position, and the initial index position is 0. When we push into the stack, place the pushed object at the position pointed to by top, and the top index will be +1 after completion. When we push the stack 5201 in turn, top is the fifth position corner mark 4;

When we pop the stack, the index top is first -1 and then points to the current top element of the stack, and then returns the current top element of the stack.

push operation

pop operation

Code implementation (regardless of container overflow)

Custom stack implementation class MyStackUseArray

package com.xiaohui.basics.stack.array;


/**
 * 自定义栈,array实现(不考虑角标越界问题)
 * @author huizi
 */
public class MyStackUseArray{

    /**
     * 当前栈顶下一个位置角标
     */
    private int index = 0;

    /**
     * 默认的栈容器大小
     */
    private Integer[] array = new Integer[15];

    /**
     * 进栈操作
     *
     * @param val 进栈对象
     * @return 进栈对象
     */
    public Integer push(Integer val){
        array[index++] = val;
        return val;
    }

    /**
     * 出栈操作
     *
     * @return Integer 栈顶元素
     */
    public Integer pop(){
        Integer popVal = array[--index];
        array[index] = null;
        return popVal;
    }
}

test class

package com.xiaohui.basics.stack;

import com.xiaohui.basics.stack.array.MyStackUseArray;
import com.xiaohui.basics.stack.linked.Node;

/**
 * 自定义栈演示
 *
 * @author huizi
 */
public class Application {

    public static void main(String[] args) {
        stackArrayTest();
    }

    private static void stackArrayTest() {
        MyStackUseArray stackUseArray = new MyStackUseArray();
        stackUseArray.push(5);
        stackUseArray.push(2);
        stackUseArray.push(0);
        stackUseArray.push(1);

        Integer pop = stackUseArray.pop();
        System.out.println(pop);
        Integer pop2 = stackUseArray.pop();
        System.out.println(pop2);
        Integer pop3 = stackUseArray.pop();
        System.out.println(pop3);
    }
}

Console output:

D:\Java\jdk1.8.0_131\bin\java.exe ...
1
0
2

Process finished with exit code 0

2, using a linked list to achieve

        Using the linked list structure does not need to specify the size of the container. The linked list is composed of nodes (including the storage value object and the pointer reference of the next node of the node) linked in turn. According to the characteristics of the stack, we know that there needs to be a reference (that is, a pointer) that records the current top element of the stack to point to the top of the current stack top element. Whenever we push the stack, we need to modify the pointer of the next node of the currently pushed object to the object pointed to by the top element top of the current record stack. And point top to the newly pushed object. When popping the stack, we first return the object pointed to by top, and modify top to point to the object pointed to by the next node of the returned object.

Push operation:

Pop operation:

Code

Define node information Node:

package com.xiaohui.basics.stack.linked;

/**
 * 节点元素对象
 *
 * @author huizi
 */
public class Node {

    /**
     * 下一个节点
     */
    private Node next;

    /**
     * 当前节点值
     */
    private Integer value;

    public Node(Node next, Integer value) {
        this.next = next;
        this.value = value;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }
}

 Custom stack implementation:

package com.xiaohui.basics.stack.linked;

/**
 * 栈使用链表实现
 *
 * @author huizi
 */
public class MyStackUserLinked {
    /**
     * 指向栈顶元素的引用
     */
    private Node top = new Node(null,null);

    public Node push(Node val){
        // 获取当前栈顶对象,并这设置为进栈对象的下一个节点
        Node next = top.getNext();
        val.setNext(next);

        // 将栈顶引用指向进栈对象
        top.setNext(val);
        return val;
    }

    public Node pop(){
        // 获取当前栈顶元素
        Node topNode = top.getNext();
        // 设置新的栈顶元素
        Node newTopNode = topNode.getNext();
        top.setNext(newTopNode);
        return topNode;
    }
}

test code

package com.xiaohui.basics.stack;

import com.xiaohui.basics.stack.linked.MyStackUserLinked;
import com.xiaohui.basics.stack.linked.Node;

/**
 * 自定义栈演示
 *
 * @author huizi
 */
public class Application {

    public static void main(String[] args) {
        stackLinkedTest();
    }

    private static void stackLinkedTest() {
        MyStackUserLinked linkedStack = new MyStackUserLinked();
        linkedStack.push(new Node(null,5));
        linkedStack.push(new Node(null,2));
        linkedStack.push(new Node(null,0));
        linkedStack.push(new Node(null,1));

        Node pop = linkedStack.pop();
        System.out.println(pop.getValue());
        Node pop2 = linkedStack.pop();
        System.out.println(pop2.getValue());
        Node pop3 = linkedStack.pop();
        System.out.println(pop3.getValue());
    }
}

 printout:

D:\Java\jdk1.8.0_131\bin\java.exe ...
1
0
2

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/liuhenghui5201/article/details/124439628