数据结构—栈

栈,特点是先进后出,所以出栈也就是删除就只能按顺序出栈,不能从栈内部出。其他的没有特别的。

栈是可以用数组,也可以用链表实现,以下是用单链表实的。

编码思路:

1、建一个单链表,使用泛型,所以链表节点需要重写equals方法

2、栈是先进后出,所以只定义一个头结点 top。

3、入栈:a、如果栈顶为空 则 top=newNode;b、如果栈顶top不为空,则newNode.next=top;top=newNode;也就是每次入栈都要更新头结点。

4、出栈:a、如果只剩头结点top则top=null;b、如果头结点也没有,那么出栈为空(这是为了避免空指针)c、如果不只有头结点,则返回头结点的值,然后top=top.next;

5、只获取栈头结点的值;

代码:

package datastu.stack;

public class Node {
    Node next;
    T value;
    public Node(T value){
        this.value=value;
    }
    public Node(Node next, T value) {
        super();
        this.next = next;
        this.value = value;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((value == null) ? 0 : value.hashCode());
        result = prime * result + ((next == null) ? 0 : next.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Node other = (Node) obj;
        if (value == null) {
            if (other.value != null)
                return false;
        } else if (!value.equals(other.value))
            return false;
        if (next == null) {
            if (other.next != null)
                return false;
        } else if (!next.equals(other.next))
            return false;
        return true;
    }
    
}
package datastu.stack;

public class Stack {
    private Node top;
    private int size = 0;

    /**
     * 返回栈的大小
     * 
     * @return
     */
    public int getSize() {
        return this.size;
    }

    /**
     * 判断是否为空
     * 
     * @return
     */
    public boolean isEmpty() {
        return this.top != null;
    }

    /**
     * 入栈
     * 
     * @param value
     */
    public void push(T value) {
        Node newNode = new Node<>(value);
        if (this.top == null) {
            top = newNode;
            this.size++;
            return;
        }
        newNode.next = top;
        top = newNode;
        this.size++;
    }

    /**
     * 获取栈元素,出栈
     * 
     * @return
     */
    public T pop() {
        if (top == null) {
            return null;
        }
        Node tem = top;
        top = tem.next;
        this.size--;
        return tem.value;

    }

    /**
     * 获取栈元素,不出栈
     * 
     * @return
     */
    public T peek() {
        if (top == null) {
            return null;
        }
        return top.value;

    }

    public static void main(String[] args) {
        Stack stack = new Stack();
        for ( int i = 0; i < 10; i++ ) {
            stack.push(i);
        }
        System.out.println(stack.getSize());
        int size = stack.getSize();
        for ( int i = 0; i < size+1; i++ ) {
            System.out.println(stack.pop());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值