数据结构与算法--栈

栈,只有一端可以操作,也就是栈头

先进后出,后进先出

链表实现

package com.zwz.stack;

import java.util.Iterator;

public class StackServiceImpl<E> implements StackService<E>{

    static class Stack<E>{
        E value;
        Stack<E> next;

        public Stack(E value, Stack next) {
            this.value = value;
            this.next = next;
        }
    }

    int capacity = 10;
    int size = 0;

    Stack<E> head = new Stack<>(null,null);
    @Override
    public Boolean push(E value) {
        if(isFull()){
            return null;
        }
        head.next = new Stack<>(value,head.next);
        size++;
        return null;
    }

    @Override
    public E pull() {
        if (isEmpyt()){
            return null;
        }
        Stack<E> next = head.next;
        head.next = next.next;
        size--;
        return next.value;
    }

    @Override
    public E peek() {
        if (isEmpyt()){
            return null;
        }
        return head.next.value;
    }

    @Override
    public Boolean isFull() {
        return capacity == size;
    }

    @Override
    public Boolean isEmpyt() {
        return head.next == null;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            Stack<E> stack = head;
            @Override
            public boolean hasNext() {
                return stack.next != null;
            }

            @Override
            public E next() {
                E value = stack.next.value;
                stack = stack.next;
                return value;
            }
        };
    }
}

数组实现

package com.zwz.stack;

import java.util.Iterator;

public class StackServiceImpl2<E> implements StackService<E> {

    @SuppressWarnings("all")
    E[] node = (E[]) new Object[20];
    int index = 0;

    @Override
    public Boolean push(E value) {
        if (isFull()) {
            return false;
        }
        node[index] = value;
        index++;
        return true;
    }

    @Override
    public E pull() {
        if (isEmpyt()) {
            return null;
        }
        E e = node[index-1];
        index--;
        return e;
    }

    @Override
    public E peek() {
        if (isEmpyt()) {
            return null;
        }
        return node[index-1];
    }

    @Override
    public Boolean isFull() {
        return index == node.length;
    }

    @Override
    public Boolean isEmpyt() {
        return index == 0;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            E[] values = node;
            int size = values.length-1;
            @Override
            public boolean hasNext() {
                return size >= 0;
            }

            @Override
            public E next() {
                E value = values[size];
                size--;
                return value;
            }
        };
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值