栈
栈只允许访问一个数据项---最后插入的数据项。仅当这个数据项被移除后才能够进行第二个数据项的查看,以此类推。比如说信件处理问题,老师的作业批改问题等等,都可以用栈的思想来理解。
栈的Java实现
class StackX
{
private int maxSize;
private long[] stackArray;
private int top;
public StackX(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);
}
}
class StackApp
{
public static void main (String[] args)
{
StackX theStack = new StackX(10);
theStack.push(40);
theStack.push(30);
theStack.push(50);
theStack.push(70);
theStack.push(60);
while( !theStack.isEmpty() )
{
long value = theStack.pop();
System.out.print(value);
System.out.println(" ");
}
System.out.println(" ");
}
}
引用数据结构中的java实现代码,方便自己以后记忆。