当前位置 : 主页 > 编程语言 > java >

java实现stack数据结构

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt package com.trs.exercise;public class Stack {private String[] stackArray;private int top;private int size;/** * * @param stackSize */Stack(int stackSize) {top = -1;stackArray = new String[stackSize];size = stackSize;}/** * * @
gistfile1.txt
package com.trs.exercise;

public class Stack {
	private String[] stackArray;
	private int top;
	private int size;

	/**
	 * 
	 * @param stackSize
	 */
	Stack(int stackSize) {
		top = -1;
		stackArray = new String[stackSize];
		size = stackSize;
	}

	/**
	 * 
	 * @param input
	 * @return
	 */
	boolean push(String input) {
		if (top <= size - 2) {
			stackArray[++top] = input;
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 
	 * @return
	 */
	String pop() {
		return stackArray[top--];
	}

	/**
	 * 
	 * @return
	 */
	String peek() {
		return stackArray[top];
	}

	/**
	 * 
	 * @return
	 */
	boolean isEmpty() {
		if (top == -1)
			return true;
		else
			return false;
	}

	void print() {
		for (int i = 0; i < top + 1; i++) {
			System.out.println(stackArray[i]);
		}

	}
}
网友评论