package com.java.stack;
/**
* @描述 栈示例
* @项目名称 Java_DataStruct
* @包名 com.java.stack
* @类名 MyStack
* @author chenlin
* @version 1.0
*/
public class CharStack {
private int maxSize;// 站的大小
private char[] arr;
private int top;// 指向栈顶元素
public CharStack(int size) {
this.maxSize = size;
this.arr = new char[maxSize];
this.top = -1;
}
/**
* 压入数据
*
* @param value
*/
public void push(char value) {
arr[++top] = value;
}
/**
* 弹出数据
*
* @param value
*/
public char pop() {
return arr[top--];
}
/**
* 访问数据
*
* @return
*/
public long peek() {
return arr[top];
}
/**
* 判断是否为空
*
* @return
*/
public boolean isEmpty() {
return (top == -1);
}
/**
* 是否满了
*
* @return
*/
public boolean isFull() {
return (top == maxSize - 1);
}
/**
* 把字符串反序
*
* @param text
* @return
*/
public String reverse(String text) {
for (int i = 0; i < text.length(); i++) {
push(text.charAt(i));
}
String result = "";
while (!isEmpty()) {
result += (char)pop();
}
return result;
}
public static void main(String[] args) {
CharStack stack = new CharStack(30);
while (!stack.isFull()) {
stack.push('l');
stack.push('a');
stack.push('k');
stack.push('m');
stack.push('n');
stack.push('e');
stack.push('b');
stack.push('f');
stack.push('d');
stack.push('e');
}
System.out.println("编程素材网测试结果:");
System.out.println("----------------------------弹出----------------------------");
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
System.out.println("----------------------------反序----------------------------");
System.out.println(stack.reverse("abcdefg"));
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有