• 栈顺序存储Java实现


    package com.wrh.lab.dataStructure.stackAndQueue;

    /**
    * the interface of the SeqStack
    *
    @author wrh
    *
    *
    @param <E>
    */

    public interface Stack<E> {

    /**
    * push the element to the stack
    *
    @param element
    */

    public void push(E element);

    /**
    * pop the element from the stack and
    * return the pop element
    */

    public E pop();

    /**
    *
    *
    @return the top value
    */

    public E getTop();

    /**
    *
    *
    @return true for empty and false for not empty
    */

    public boolean isEmpty();

    /**
    * clear the stack
    */

    public void clear();

    }
    package com.wrh.lab.dataStructure.stackAndQueue;

    /**
    *
    *
    @author wrh
    * the stack node
    */

    public class StackNode<E> {
    private E element;
    private StackNode<E> next;

    /**
    * constructor
    */

    public StackNode() {
    element = null;
    next = null;
    }

    public StackNode(E element, StackNode<E> next) {
    this.element = element;
    this.next = next;
    }

    public E getElement() {
    return element;
    }

    /**
    *
    @param element
    */

    public void setElement(E element) {
    this.element = element;
    }

    public StackNode<E> getNext() {
    return next;
    }

    public void setNext(StackNode<E> next) {
    this.next = next;
    }

    }
    package com.wrh.lab.dataStructure.stackAndQueueImpl;

    import com.wrh.lab.dataStructure.stackAndQueue.Stack;

    public class SeqStackImpl<E> implements Stack<E> {
    private static final int defaultSize = 3;
    private int size; //max size of the stack
    private int top; //index for top object
    private E[] listArray; //array hoding stack objects
    private static final int MAX_SIZE = 7;

    /**
    * constructor init a stack for the size defaultSize
    */

    public SeqStackImpl() {
    size = defaultSize;
    top = -1;
    listArray = (E[]) new Object[size];
    }

    /**
    * constructor
    * init a stack for the size of size
    *
    @param size
    */

    public SeqStackImpl(int size) {
    if (size < 1 || size > MAX_SIZE) {
    System.out.println("the size is not vailable");
    } else {
    this.size = size;
    top = -1;
    listArray = (E[]) new Object[size];
    }
    }


    @Override
    public void push(E element) {
    if (size < MAX_SIZE && top < size) {
    listArray[++top] = element;
    } else if (top < MAX_SIZE){
    resizeList(listArray);
    listArray[++top] = element;
    } else {
    System.out.println("the stack is full! ");
    }
    }

    @Override
    public E pop() {
    if (isEmpty()) { //if the stack is empty
    System.out.println("the stack is empty");

    return null;
    } else {

    return listArray[top--];

    }
    }

    @Override
    public E getTop() {
    return listArray[top];
    }

    /**
    * resize the list
    *
    @param entry
    */

    public void resizeList(E[] list) {
    int resize = size * 3 / 2 +1;
    Object[] seqList_upp = new Object[resize];
    System.arraycopy(list,0,seqList_upp,0,size);
    this.listArray = (E[]) seqList_upp;
    }

    @Override
    public boolean isEmpty() {
    return 0 == top;
    }

    @Override
    public void clear() {
    top = 0;
    }

    public static void main(String[] args) {
    Stack<Integer> s = new SeqStackImpl<Integer>(5);
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    s.push(5);
    System.out.println(s.isEmpty());
    System.out.println(s.pop());
    System.out.println(s.pop());
    s.clear();
    System.out.println(s.pop());
    }

    }




  • 相关阅读:
    Dockerfile
    最近遇到的jsfl开发问题总结
    【Distributed】大型网站高并发和高可用
    【Distributed】CDN
    【Distributed】限流技巧
    【Java并发】锁机制
    【Java并发】线程通信
    【Java并发】线程安全和内存模型
    【Java并发】基础
    【Redis】基本数据类型及命令操作(超详细)
  • 原文地址:https://www.cnblogs.com/wrh526/p/2354601.html
Copyright © 2020-2023  润新知