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;
    /**
    *
    @author wrh
    * the implementation of the linked stack
    */

    import com.trs.lab.dataStructure.stackAndQueue.Stack;
    import com.trs.lab.dataStructure.stackAndQueue.StackNode;

    public class LinkedStackImpl<E> implements Stack<E> {
    private StackNode<E> top;
    private int size;

    //constructor
    public LinkedStackImpl() {

    top = null;
    }

    @Override
    public void push(E element) {
    top = new StackNode(element, top); //the next point to the former top and the new node is top
    }


    @Override
    public E pop() {
    if (null != top) {
    E t = top.getElement();
    top = top.getNext();
    return t;
    } else {
    System.out.println("the stack is null");
    return null;
    }
    }

    @Override
    public E getTop() {
    if (isEmpty()) {
    System.out.println("the stack is null");
    return null;
    } else {
    return top.getElement();
    }
    }

    @Override
    public boolean isEmpty() {
    // TODO Auto-generated method stub
    return top == null;

    }

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

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

    System.out.println(s.pop());
    System.out.println(s.pop());
    System.out.println(s.pop());
    }
    }




  • 相关阅读:
    C#学习(五)- 正则表达式等
    C#学习(四)
    C#学习(三)
    C#学习(二)
    终于装好了VS2013,开始!(一)
    简短的开始,C#学习分享地。
    java虚拟机之虚拟机类加载机制
    在用mybatis向MySQL数据库中插入时间时报错:Incorrect datetime value: '' for column '' at row 1
    什么是高并发 ,一些常见的处理方式
    基本类型和引用类型的区别
  • 原文地址:https://www.cnblogs.com/wrh526/p/2354606.html
Copyright © 2011-2022 走看看