Data structure (VI) of the stack

Stack

Stacks and queues are two important data structures from the data structure point of view, stacks and queues are linear form, wherein basic operations its particularity stacks and queues to a subset of linear operation table, they are linear operations are limited table, it can be referred to define a data structure, but from the perspective of the data type, they are two important linear table abstract data types differ Because they are widely used among various software systems, object-oriented so programming, they are multi-data type.

Defining abstract data types stack

Stack : linear table is defined deletion or insertion end of the table only therefore, the stack, the trailing end of the table have a special meaning, called. Top of the stack , respectively, referred to as the end header stack low , does not contain empty list element is referred to as an empty stack.
Suppose stack S = (a1, a2, ... , an), said element a1 is low stack, an element of the stack. press stack elements a1, a2, ..., an order intake stack, the first element of the stack should be popped element. in other words, the stack is modified LIFO principle performed (as shown in Figure I). Thus, the stack also known , last-out (LIFO) linear table (LIFO structure referred to), which is characterized by the available image showing a railroad dispatch station.
Here Insert Picture Description(Figure 1)
the basic operation of the stack insertion or deletion in addition to an outer top of the stack , as well as stack initialization, and sentenced empty, take the top of the stack yuan and so on.

Stack achieve a variety of ways:
a: Array

package main.com.cs.stack;

import java.util.Arrays;
/**
 * 数组实现栈
 * @param <T>
 */
class Mystack1<T> {
    //实现栈的数组
    private Object[] stack;
    //数组大小
    private int size;
 
    Mystack1() {
        stack = new Object[10];//初始容量为10
    }
 
    //判断是否为空
    public boolean isEmpty() {
        return size == 0;
    }
 
    //返回栈顶元素
    public T peek() {
        T t = null;
        if (size > 0)
            t = (T) stack[size - 1];
        return t;
    }
    
    public void push(T t) {
        expandCapacity(size + 1);
        stack[size] = t;
        size++;
    }
 
    //出栈
    public T pop() {
        T t = peek();
        if (size > 0) {
            stack[size - 1] = null;
            size--;
        }
        return t;
    }
 
    //扩大容量
    public void expandCapacity(int size) {
        int len = stack.length;
        if (size > len) {
            size = size * 3 / 2 + 1;//每次扩大50%
            stack = Arrays.copyOf(stack, size);
        }
    }
}
 
public class ArrayStack {
    public static void main(String[] args) {
        Mystack1<String> stack = new Mystack1<>();
        System.out.println(stack.peek());
        System.out.println(stack.isEmpty());
        stack.push("java");
        stack.push("is");
        stack.push("beautiful");
        stack.push("language");
        System.out.println(stack.pop());
        System.out.println(stack.isEmpty());
        System.out.println(stack.peek());
    }
}

II: list

package main.com.cs.stack;

/**
 * 链表实现栈
 *
 * @param <T>
 */
class Mystack2<T> {
    //定义链表
    class Node<T> {
        private T t;
        private Node next;
    }
 
    private Node<T> head;
 
    //构造函数初始化头指针
    Mystack2() {
        head = null;
    }
 
    //入栈
    public void push(T t) {
        if (t == null) {
            throw new NullPointerException("参数不能为空");
        }
        if (head == null) {
            head = new Node<T>();
            head.t = t;
            head.next = null;
        } else {
            Node<T> temp = head;
            head = new Node<>();
            head.t = t;
            head.next = temp;
        }
    }
 
    //出栈
    public T pop() {
        T t = head.t;
        head = head.next;
        return t;
    }
 
    //栈顶元素
    public T peek() {
        T t = head.t;
        return t;
    }
 
    //栈空
    public boolean isEmpty() {
        if (head == null)
            return true;
        else
            return false;
    }
}
 
public class LinkStack {
    public static void main(String[] args) {
        Mystack2 stack = new Mystack2();
        System.out.println(stack.isEmpty());
        stack.push("Java");
        stack.push("is");
        stack.push("beautiful");
        System.out.println("栈顶:" + stack.peek());
        System.out.println("出栈:" + stack.pop());
        System.out.println("出栈:" +stack.pop());
        System.out.println("是否为null:" + stack.isEmpty());
        System.out.println("出栈:" + stack.pop() );
        System.out.println(stack.isEmpty());
    }
}

Three: linkList

package main.com.cs.stack;

import java.util.LinkedList;
 
/**
 * LinkedList实现栈
 *
 * @param <T>
 */
class ListStack<T> {
    private LinkedList<T> ll = new LinkedList<>();
 
    //入栈
    public void push(T t) {
        ll.addFirst(t);
    }
 
    //出栈
    public T pop() {
        return ll.removeFirst();
    }
 
    //栈顶元素
    public T peek() {
        T t = null;
        //直接取元素会报异常,需要先判断是否为空
        if (!ll.isEmpty())
            t = ll.getFirst();
        return t;
    }
 
    //栈空
    public boolean isEmpty() {
        return ll.isEmpty();
    }
}
 
public class LinkedListStack {
    public static void main(String[] args) {
        ListStack<String> stack = new ListStack();
        System.out.println(stack.isEmpty());
        System.out.println(stack.peek());
        stack.push("java");
        stack.push("is");
        stack.push("beautiful");
        System.out.println(stack.peek());
        System.out.println(stack.pop());
        System.out.println(stack.isEmpty());
        System.out.println(stack.peek());
    }
}
Published 69 original articles · won praise 6 · views 2498

Guess you like

Origin blog.csdn.net/qq_40539437/article/details/104015644