草草地写了一个。import java.util.NoSuchElementException;public class ArrayStack<E> {
private E[] elements; private int count;
@SuppressWarnings("unchecked") public ArrayStack(int size) { elements = (E[])new Object[size]; }
public ArrayStack() { this(16); }
/** * 栈中元素数量 * * @return */ public int size() { return count; } /** * 栈中是否为空(没有元素) * * @return */ public boolean isEmpty() { return (count == 0); }
/** * 返回顶层元素,并不弹出 * * @return */ public E peek() { if(isEmpty()) { throw new NoSuchElementException("stack is empty"); } return elements[count - 1]; }
/** * 弹出顶层元素 * * @return */ public E pop() { if(isEmpty()) { throw new NoSuchElementException("stack is empty"); } E e = elements[--count]; elements[count] = null; return e; }
/** * 将元素压入栈中 * * @param element * @return */ public E push(E element) { checkCapacity(); elements[count++] = element; return element; }
import java.util.NoSuchElementException;
public class ArrayStack<E> {
private int initalSize = 5;
private Object[] stack;
private int head;
private int tail;
public ArrayStack() {
initialize(null);
}
public ArrayStack(int newcapacity){
if (newcapacity < this.initalSize)
throw new IllegalArgumentException("The new capacity is too small!");
initalSize = newcapacity;
initialize(null);
}
public ArrayStack(E[] items) {
initialize(items);
}
public ArrayStack(Collection<E> collection) {
initialize(collection.toArray());
}
private void initialize(Object[] items){
if (items == null || items.length == 0){
stack = new Object[initalSize];
head = 0;
tail = 0;
}
else{
stack = new Object[items.length + 1];
System.arraycopy(items, 0, stack, 0, items.length);
head = items.length;
tail = 0;
}
}
@SuppressWarnings("unchecked")
public E pop(){
if (size() == 0)
throw new NoSuchElementException();
if (head == 0)
head = stack.length;
Object ret = stack[--head];
loseWeight();
return (E)ret;
}
public void push(E item){
increaseWeight();
stack[head++] = item;
if (head == stack.length)
head = 0;
}
@SuppressWarnings("unchecked")
public E peek(){
if (size() == 0)
throw new NoSuchElementException();
if (head == 0)
return (E)stack[stack.length - 1];
else
return (E)stack[head-1];
}
public boolean isEmpty(){
return (size() == 0);
}
public int size(){
return head >= tail ? head - tail : head + stack.length - tail;
}
public boolean increaseWeight(){
if (size() == stack.length - 1){
Object[] newStack = new Object[stack.length * 2];
System.arraycopy(stack, 0, newStack, 0, stack.length);
stack = newStack;
return true;
}
return false;
}
public boolean loseWeight(){
if (size() == stack.length / 4){
Object[] newStack = new Object[stack.length/2];
if (head >= tail){
System.arraycopy(stack, tail, newStack, 0, size());
}
else{
System.arraycopy(stack, tail, newStack, 0, stack.length-tail);
System.arraycopy(stack, 0, newStack, stack.length-tail, head);
}
tail = 0;
head = size();
return true;
}
return false;
}
}
//本篇文章来源于:开发学院 http://edu.codepub.com 原文链接http://edu.codepub.com/2009/1003/16063.php
private E[] elements;
private int count;
@SuppressWarnings("unchecked")
public ArrayStack(int size) {
elements = (E[])new Object[size];
}
public ArrayStack() {
this(16);
}
/**
* 栈中元素数量
*
* @return
*/
public int size() {
return count;
} /**
* 栈中是否为空(没有元素)
*
* @return
*/
public boolean isEmpty() {
return (count == 0);
}
/**
* 返回顶层元素,并不弹出
*
* @return
*/
public E peek() {
if(isEmpty()) {
throw new NoSuchElementException("stack is empty");
}
return elements[count - 1];
}
/**
* 弹出顶层元素
*
* @return
*/
public E pop() {
if(isEmpty()) {
throw new NoSuchElementException("stack is empty");
}
E e = elements[--count];
elements[count] = null;
return e;
}
/**
* 将元素压入栈中
*
* @param element
* @return
*/
public E push(E element) {
checkCapacity();
elements[count++] = element;
return element;
}
/**
* 检查内部存储数组的长度,长度不够时扩展 50%
*/
@SuppressWarnings("unchecked")
private void checkCapacity() {
if(elements.length > count) {
return;
}
E[] old = elements;
elements = (E[])new Object[elements.length * 3 / 2 + 1];
System.arraycopy(old, 0, elements, 0, old.length);
}
}
class MyStack
{
private String[] s;
private int cap;//指定数组容量(即大小)
private int top = -1;//用top变量始终保存栈顶元素的下标,默认为-1即开始时栈内没有任何元素
//无参构造(指定数组的默认容量为50)
public MyStack(){
this(50);//调用有参构造(传默认值给有参构造)
}
public MyStack(int cap){
this.cap = cap;
s = new String[cap];//使用指定容量创建数组
}
//判断栈内是否为空(即:如果当前下标为-1的话,则栈内为空)
public boolean isEmpty(){
if(top == -1){//如果等于-1则为空返回true
return true;
}else{
return false;//不为空返回false
}
}
//判断栈内是否已经存满(即:下标小于等于数组长度-1的话则没有满)
public boolean isFull(){
if(top <= (cap - 1)){
return false;
}else{
return true;//否表示已经满了
}
}
//入栈操作(接受一个数据元素进来,下标要先++,然后判断是否已经满了)
public boolean push(String value){
top++;
if(!isFull()){//不满的话,把值存进栈里
s[top] = value;
return true;
}else{
System.out.println("溢出!");//否则是溢出
top--;
return false;
}
}
//出栈操作(其实就是从数组里删除一个数据元素)
public boolean pop(){
if(!isEmpty()){//先判断栈是否已经空了,如果不空的话
s[top] = null;//把栈顶数据元素设置为null(即删除指向,使对象变成一个无用对象,方便GC后期尽快回收此对象)
top--;//删除上一个元素后top要--,因为top始终指向栈顶元素
return true;
}else{
System.out.println("已经到栈底!");
return false;
}
} public int getSize(){
return top + 1;
} public void clear(){
for(int i = 0; i < getSize(); i++){
s[i] = null;
}
top = -1;
} public String getTopValue(){
if(top == -1){
return null;
}else{
return s[top];
}
}
};public class MyStackTest
{
public static void main(String[] args)
{
MyStack stack = new MyStack(5);
System.out.println("开始入栈...");
stack.push("aaa");
System.out.println("栈的大小:" + stack.getSize());
System.out.println("栈顶元素:" + stack.getTopValue());
stack.push("111");
System.out.println("栈的大小:" + stack.getSize());
System.out.println("栈顶元素:" + stack.getTopValue());
stack.push("ss");
System.out.println("栈的大小:" + stack.getSize());
System.out.println("栈顶元素:" + stack.getTopValue());
stack.push("!");
System.out.println("栈的大小:" + stack.getSize());
System.out.println("栈顶元素:" + stack.getTopValue());
stack.push("555");
System.out.println("栈的大小:" + stack.getSize());
System.out.println("栈顶元素:" + stack.getTopValue());
stack.push("666");
System.out.println("栈的大小:" + stack.getSize());
System.out.println("栈顶元素:" + stack.getTopValue());
System.out.println("开始出栈...");
stack.pop();
System.out.println("栈的大小:" + stack.getSize());
System.out.println("栈顶元素:" + stack.getTopValue());
stack.pop();
System.out.println("栈的大小:" + stack.getSize());
System.out.println("栈顶元素:" + stack.getTopValue()); System.out.println("clear栈...");
stack.clear();
System.out.println("栈的大小:" + stack.getSize());
System.out.println("栈顶元素:" + stack.getTopValue());
}
}