关于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();
        StackSLinked stackSLinked = test.new StackSLinked();
        stackSLinked.push(11);
        stackSLinked.push(22);
        stackSLinked.push(33);
        stackSLinked.push(55);
        stackSLinked.push(66);
        stackSLinked.push(77);
        
        // 打印
        display("栈的链式存储实现-栈内元素:", stackSLinked.top);

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

        // 打印
        display("栈的链式存储实现-栈内元素:", stackSLinked.top);
        
  }
  
  // 显示
  public static void display(String str, SLNode node) {
    System.out.println(str);
    while (node != null) {
      System.out.print(node.getData() + " ");
      node = node.next;
    }
    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 interface Node {
    // 获取结点数据域
    public Object getData();

    // 设置结点数据域
    public void setData(Object obj);
  }
  
  public class SLNode implements Node {
    private Object element;
    private SLNode next;

    public SLNode() {
      this(null, null);
    }

    public SLNode(Object ele, SLNode next) {
      this.element = ele;
      this.next = next;
    }

    public SLNode getNext() {
      return next;
    }

    public void setNext(SLNode next) {
      this.next = next;
    }

    /**************** Methods of Node Interface **************/
    public Object getData() {
      return element;
    }

    public void setData(Object obj) {
      element = obj;
    }
  }

  // 栈的链式存储实现
  public class StackSLinked implements Stack {
    private SLNode top; // 链表首结点引用
    private int size; // 栈的大小

    public StackSLinked() {
      top = null;
      size = 0;
    }

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

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

    // 数据元素e入栈
    public void push(Object e) {
      SLNode q = new SLNode(e, top);
      top = q;
      size++;
    }

    // 栈顶元素出栈
    public Object pop() throws StackEmptyException {
      if (size < 1)
        throw new StackEmptyException("错误,堆栈为空。");
      Object obj = top.getData();
      top = top.getNext();
      size--;
      return obj;
    }

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

}

 

发表回复

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