标签:java 基础
一、简单实现
/**
* 栈
* 以数组为存储的栈
* 说明:
* 1.入栈
* 2.出栈
* 3.是否为空
* 4.是否已满
*
*/
public class Stack
{
private int maxSize;
private long[] stackArray;
private int top;
public Stack(int s)
{
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(long j)
{
stackArray[++top] = j;
}
public long pop()
{
return stackArray[top--];
}
public long peek()
{
return stackArray[top];
}
public boolean isEmpty()
{
return top == -1;
}
public boolean isFull()
{
return top == maxSize - 1;
}
}
二、以链表的存储结构的栈
public class StackLink
{
private LinkedList<Long> dataList;
public StackLink()
{
dataList = new LinkedList<Long>();
}
public void push(long j)
{
dataList.addFirst(j);
}
public long pop()
{
return dataList.removeFirst();
}
public boolean isEmpty()
{
return dataList.isEmpty();
}
}
标签:java 基础
原文地址:http://881206524.blog.51cto.com/10315134/1788447