TestStackOfInteger
来源:互联网 发布:河南软件开发公司 编辑:程序博客网 时间:2024/05/22 02:20
public class TestStackOfInteger {
public static void main(String[] args) {
StackOfInteger stack = new StackOfInteger(0);
for(int i = 0; i < 10; i++)
stack.push(i);
System.out.println(stack.getSize());
while(!stack.empty())
System.out.print(stack.pop() + " ");
System.out.println();
}
public class StackOfInteger {
private int[] elements;
private int size = 0;
/** Construct a stack with the default capacity 16 */
public StackOfInteger() {
this(16);
}
/** Construct a stack with the capacity */
public StackOfInteger(int capacity) {
if(capacity < 0)
capacity = 0;
elements = new int[capacity];
}
/** Push a new integer into the top of the stack */
public void push(int value) {
if(elements.length == size) {
int[] newElements = new int[elements.length * 2 + 1];
for(int i = 0; i < size; i++)
newElements[i] = elements[i];
elements = newElements;
}
elements[size++] = value;
}
/** Return and remove the top element from the stack */
public int pop() {
return elements[--size];
}
/** Return the top element frome the stack */
public int peek() {
return elements[size - 1];
}
/** Test whether the stack is empty */
public boolean empty() {
return size==0;
}
/** Return the number of elements in the stack */
public int getSize() {
return size;
}
/** Return the capacity of elements in the stack */
public int getCapacity() {
return elements.length;
}
}
10
9 8 7 6 5 4 3 2 1 0
public static void main(String[] args) {
StackOfInteger stack = new StackOfInteger(0);
for(int i = 0; i < 10; i++)
stack.push(i);
System.out.println(stack.getSize());
while(!stack.empty())
System.out.print(stack.pop() + " ");
System.out.println();
}
}
/**栈(stack)是一种以“后进先出”的方式存放数据的数据结构
* 栈的应用有很多。例如:编译器使用栈来处理方法的调用。当调用某个方法时
* 方法的参数和局部变量都被压入栈中。当一个方法调用另一个方法时,
* 新方法的参数和局部变量被压入栈中。当方法完成它的工作,返回它的调用者时,
* 从栈中释放与它相关的空间
*/
public class StackOfInteger {
private int[] elements;
private int size = 0;
/** Construct a stack with the default capacity 16 */
public StackOfInteger() {
this(16);
}
/** Construct a stack with the capacity */
public StackOfInteger(int capacity) {
if(capacity < 0)
capacity = 0;
elements = new int[capacity];
}
/** Push a new integer into the top of the stack */
public void push(int value) {
if(elements.length == size) {
int[] newElements = new int[elements.length * 2 + 1];
for(int i = 0; i < size; i++)
newElements[i] = elements[i];
elements = newElements;
}
elements[size++] = value;
}
/** Return and remove the top element from the stack */
public int pop() {
return elements[--size];
}
/** Return the top element frome the stack */
public int peek() {
return elements[size - 1];
}
/** Test whether the stack is empty */
public boolean empty() {
return size==0;
}
/** Return the number of elements in the stack */
public int getSize() {
return size;
}
/** Return the capacity of elements in the stack */
public int getCapacity() {
return elements.length;
}
}
10
9 8 7 6 5 4 3 2 1 0
0 0
- TestStackOfInteger
- 怎么让百度收录网站的图片呢?
- PRML阅读记(3)
- 给技术人上的管理课:平衡和集中
- 任务栏信息栏中图标闪动
- “bash: fork: Resource temporarily unavailable”的解决方案
- TestStackOfInteger
- 安装mysql5.6.17遇到安装意外终止 1067
- hdu 1264 Counting Squares(扫描线)
- 老三篇原文
- UVa 446 - Kibbles "n" Bits "n" Bits "n" Bits
- Android-Popupwindow和Dialog做弹出窗口
- mybatis学习笔记之trim标签---小笔记
- AVR之BOOTLOADER技术详解
- KMP字符串匹配算法学习总结