zoukankan      html  css  js  c++  java
  • 栈顺序存储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());
    }

    }




  • 相关阅读:
    PyQt作品 – PingTester – 多点Ping测试工具
    关于和技术人员交流的一二三
    Pyjamas Python Javascript Compiler, Desktop Widget Set and RIA Web Framework
    Hybrid Qt applications with PySide and Django
    pyjamas build AJAX apps in Python (like Google did for Java)
    PyQt 维基百科,自由的百科全书
    InfoQ:请问为什么仍要选择Java来处理后端的工作?
    Eric+PyQt打造完美的Python集成开发环境
    python select module select method introduce
    GUI Programming with Python: QT Edition
  • 原文地址:https://www.cnblogs.com/wrh526/p/2354601.html
Copyright © 2011-2022 走看看