zoukankan      html  css  js  c++  java
  • Java栈实现

    1. 定义栈所有方法接口:
    package com.dataStructure.stack;

    /**
    * 类功能描述:
    *
    * @version 1.0.0
    * @auther Create by Barry
    * @date Create on 2018/3/12.
    * @history
    */
    public interface Stack {
    public boolean push(Object element);

    public Object pop();

    public Object peek();

    public int elementCount();

    public boolean isEmpty();

    public void showElements();
    }

    2. 基于数组的栈实现

    package com.dataStructure.stack;

    /**
    * 类功能描述:
    *
    * @version 1.0.0
    * @auther Create by Barry
    * @date Create on 2018/3/12.
    * @history
    */
    public class ArrayStack implements Stack{
    private int maxSize;
    private Object data[];
    private int top = -1;

    public ArrayStack(int maxSize){
    data = new Object[maxSize];
    this.maxSize = maxSize;
    }

    @Override
    public boolean push(Object element){
    if(top == maxSize-1){
    return false;
    }
    data[++top] = element;
    return true;
    }

    @Override
    public Object pop(){
    if(this.isEmpty()){
    return -1;
    }
    Object returnData = data[top];
    top--;
    return returnData;
    }

    @Override
    public Object peek(){
    if(this.isEmpty()){
    return -1;
    }
    return data[top];
    }

    @Override
    public int elementCount(){
    return top+1;
    }

    @Override
    public boolean isEmpty(){
    if(top == -1){
    return true;
    }
    return false;
    }

    @Override
    public void showElements(){
    System.out.println("--------Show all element start--------");
    for(int i=top; i>=0; i--){
    System.out.println(data[i]);
    }
    System.out.println("--------Show all element end--------");
    }

    public static void main(String[] args){
    Stack stack = new ArrayStack(11);

    /*System.out.println("=============================test 1 start--------------");
    stack.push(1);
    stack.showElements();
    System.out.println(stack.elementCount());
    System.out.println("=============================test 1 end--------------");
    System.out.println("=============================test 2 start--------------");
    stack.push(2);
    stack.showElements();
    System.out.println(stack.elementCount());
    System.out.println("=============================test 2 end--------------");
    System.out.println("=============================test 3 start--------------");
    stack.pop();
    stack.showElements();
    System.out.println(stack.elementCount());
    System.out.println("=============================test 3 end--------------");*/
    /*System.out.println("=============================test 4 start--------------");
    for(int i=1; i<=12; i++ ){
    stack.push(i);
    }
    stack.showElements();
    System.out.println(stack.elementCount());
    System.out.println("=============================test 4 end--------------");*/

    String str = "1234321";
    System.out.println(ArrayStack.isSysmetryStr(str));
    }

    /**
    * 用栈判断字符串是否为对称串,
    * 即从左到右和从右到左读取是一样的
    * 1234321为对称串
    * @param str
    * @return
    */
    public static boolean isSysmetryStr(String str){
    if(str == null || str.length() == 0){
    return true;
    }

    Stack stack = new ArrayStack(str.length());
    for(int i=0; i < str.length(); i++){
    stack.push((int)str.charAt(i));
    }

    for(int i=0; i < str.length(); i++){
    Object data = stack.pop();
    if((int)data == (int)str.charAt(i)){
    return false;
    }
    }

    return true;
    }
    }

    3. 基于链表的栈实现
    package com.dataStructure.stack;

    /**
    * 类功能描述:
    *
    * @version 1.0.0
    * @auther Create by Barry
    * @date Create on 2018/3/12.
    * @history
    */
    public class LinkedStack implements Stack{
    private Node header;
    private int elementCount = 0;
    private int maxSize;

    public LinkedStack(int maxSize){
    this.maxSize = maxSize;
    }

    @Override
    public boolean push(Object data) {
    if(elementCount == maxSize){
    throw new RuntimeException("Stack is Full");
    }
    header = new Node(data, header);
    elementCount++;
    return true;
    }

    @Override
    public Object pop() {
    if(isEmpty()){
    throw new RuntimeException("Stack is empty");
    }
    Object returnData = header.data;
    header = header.next;
    elementCount--;
    return returnData;
    }

    @Override
    public Object peek() {
    return header.data;
    }

    @Override
    public int elementCount() {
    return elementCount;
    }

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

    @Override
    public void showElements() {
    while (header != null){
    System.out.println(header.data);
    header = header.next;
    }
    }

    class Node{
    private Object data;
    private Node next;

    public Node(Object data){
    this(data, null);
    }

    public Node(Object data, Node next){
    this.data = data;
    this.next = next;
    }
    }

    public static void main(String[] args) {
    Stack stack = new ArrayStack(5);
    for(int i=1; i<=5; i++){
    stack.push(i);
    }
    stack.pop();
    stack.showElements();
    }

    }
  • 相关阅读:
    华为OJ机试训练(一)
    mount CIFS return ERR -12 and report Cannot allocate memory
    ftk学习记(icon篇)
    使用jquery-mockjax模拟ajax请求做前台測试
    Objective-C 内存管理之 _ARC
    [LeetCode]Decode Ways
    设计模式六大原则——迪米特法则(LoD)
    ACM/ICPC2014鞍山现场赛E hdu5074Hatsune Miku
    2015届校园招聘笔试/面试 基础知识点 总结
    依据Path取Json指定节点的值
  • 原文地址:https://www.cnblogs.com/barrywxx/p/8552860.html
Copyright © 2011-2022 走看看