关于Java算法与数据结构-栈的顺序存储实现

关于Java算法与数据结构-栈的顺序存储实现

package com.ssm.cts.test;

/**
 * CopyRright (c)2018-2028: chanpinxue.cn 
 * Project: cts 
 * Module Name: StackTest
 * Comments: 栈
 * JDK version used: JDK1.8 
 * Author: jzh 
 * Create Date: 2018-12-04
 * Modified By: jzh 
 * Modified Date: 2018-12-04
 * Why & What is modified:
 * Version: <1.0>
 */
public class StackTest {

  // 测试
  public static void main(String[] args) {
    // No enclosing instance of type StackTest is accessible. 
    // Must qualify the allocation with an enclosing instance of type StackTest (e.g. x.new A() where x is an instance of StackTest).
    StackTest test = new StackTest();
    StackArray stack = test.new StackArray(9);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(5);
        stack.push(6);
        stack.push(7);
        
        // 打印
        display("栈内元素:", stack);

        int top = (int) stack.peek();
        System.out.println("栈顶: " + top);

        // 栈顶出栈
        stack.pop();

        // 打印
        display("栈内元素:", stack);
  }
  
  // 显示
  public static void display(String str, StackArray stack) {
    System.out.println(str);
    for (int i = stack.getSize() - 1; i >= 0; i--) {
      System.out.print(stack.elements[i].toString() + ' ');
    }
    System.out.println();
  }
  
  // 栈基本功能接口
  public interface Stack {
    // 返回堆栈的大小
    public int getSize();

    // 判断堆栈是否为空
    public boolean isEmpty();

    // 数据元素e入栈
    public void push(Object e);

    // 栈顶元素出栈
    public Object pop() throws StackEmptyException;

    // 取栈顶元素
    public Object peek() throws StackEmptyException;
  }

  @SuppressWarnings("serial")
  public class StackEmptyException extends RuntimeException {
    public StackEmptyException(String err) {
      super(err);
    }
  }
  
  // 栈顺序存储实现
  public class StackArray implements Stack {
    //private final int LEN = 9; //数组的默认大小
    private Object[] elements; // 数据元素数组
    private int top; // 栈顶指针

    public StackArray(int LEN) {
      top = -1;
      elements = new Object[LEN];
    }

    // 返回堆栈的大小
    public int getSize() {
      return top + 1;
    }

    // 判断堆栈是否为空
    public boolean isEmpty() {
      return top < 0;
    }

    // 数据元素e入栈
    public void push(Object e) {
      if (getSize() >= elements.length)
        expandSpace();
      elements[++top] = e;
    }

    private void expandSpace() {
      Object[] a = new Object[elements.length * 2];
      for (int i = 0; i < elements.length; i++)
        a[i] = elements[i];
      elements = a;
    }

    // 栈顶元素出栈
    public Object pop() throws StackEmptyException {
      if (getSize() < 1)
        throw new StackEmptyException("错误,堆栈为空。");
      Object obj = elements[top];
      elements[top--] = null;
      return obj;
    }

    // 取栈顶元素
    public Object peek() throws StackEmptyException {
      if (getSize() < 1)
        throw new StackEmptyException("错误,堆栈为空。");
      return elements[top];
    }
  }

}

栈内元素:
7 6 5 3 2 1
栈顶: 7
栈内元素:
6 5 3 2 1

发表回复

您的电子邮箱地址不会被公开。