Data Structures and Algorithms (III) stack - sequential storage structure

Stack

Speaking of the stack, it is learned to understand object-oriented, object-oriented said the main function onto the stack, the stack into the constructor, the constructor popped, are in operation on the stack, such as our advanced stack the main function, the constructor again push, we can not turn the main function popped after only wait popped constructor, then operate. Thereby obtaining a defined stack: the stack is defined in a linear table insertion and deletion only in the trailer.

This application LIFO stack data structure is very common, such as bullet clip, clip of bullets into the last shot is always the first to go.

We allow insertion and deletion of one end called the top of the stack (top), and the other end is called the end of the stack (bottom), the stack is also known as LIFO linear table, that there is a linear relationship between the stack elements, but it It is a special linear form only. It is defined in said insert and delete operations table tail linear table, where the table refers to the end of the stack, rather than the bottom of the stack.

Insert stack, called the stack, also referred to as a push, push. Similar bullets into the clip.
Here Insert Picture Description
Stack delete operation, called to stack, also known as popping, like the clip of bullets out.
Here Insert Picture Description
Abstract data type stack, which we define as the interface Stack, implemented in the Java language, basic contents are as follows :( which methods are abstract, there is public abstract, here we omitted)

package DS01.动态数组;
/*
线性表的一种特殊情况 栈
ArrayList当做ArrayStack的成员变量
 */
public interface Stack<E> extends Iterable<E>{
    //获取栈的元素的个数
    int getSize();

    //判断栈是否为空
    boolean isEmpty();

    //将元素e进栈
    void push(E e);

    //弹栈一个元素并返回
    E pop();

    //查看当前栈顶元素
    E peek();

    //清空栈
    void clear();
}

Then write the order stack ArrayStack, it implements the interface Stack, Stack is implemented by the internal order of the sequence table, before I wrote the order table ArrayList, only need to create ArrayList object list, repeatable method invocation list of direct methods.

import java.util.Iterator;
/*
顺序栈内部是有顺序表实现的
ArrayStack
    ArrayList
 */
public class ArrayStack<E> implements Stack<E> {
    private ArrayList<E> list;
    //创建一个默认大小的栈
    public ArrayStack(){
        //创建顺序表对象list
        list = new ArrayList<>();
    }
    //创建一个容量由用户指定的顺序栈
    public ArrayStack(int capacity){
        list = new ArrayList<>(capacity);
    }

    //获取有效元素个数这个方法,顺序表中有,直接调用
    @Override
    public int getSize() {
        return list.getSize();
    }
    
    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }
    
    @Override
    public void push(E e) {
        //进栈从表尾进,调用addLast
        list.addLast(e);
    }

    @Override
    public E pop() {
        //出栈元素从表尾出,调用removeLast
        return list.removeLast();
    }

    //查看当前栈顶
    @Override
    public E peek() {
        return list.getLast();
    }

    @Override
    public void clear() {
        //清空栈和清空顺序表一样,直接调用
        list.clear();
    }

    @Override
    public Iterator<E> iterator() {
        //迭代器也是直接调用
        return list.iterator();
    }
    //顺序栈的toString方法,重新写 
    public String toString(){
        //创建StringBuilder对象,不需要额外创字符串,在原来的上加字符
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("ArrayStrck: %d/%d\n",getSize(),list.getCapacity()));
        //用数组的形式输出
        sb.append('[');
        if(isEmpty()){  //若为空,打印 []
            sb.append(']');
        }else{
            for(int i=0;i<getSize();i++){
                //在原来上加字符
                sb.append(list.get(i));
                if(i==list.getSize()-1){    
                    sb.append(']'); //遍历到最后一个元素打印']'
                }else{
                    sb.append(','); //不然在每个元素中间打','
                }
            }
        }
        //调用对象的tostring方法返回对象
        return sb.toString();
    }
}

The basic method of the stack order finished, you can write a test class to test the accuracy of each method.

Published 70 original articles · won praise 56 · views 1987

Guess you like

Origin blog.csdn.net/qq_43624033/article/details/103533965