数组堆栈:
public class Stack {
private long[] stackArray;
private int top;
private int maxSize;
public Stack(int maxSize){
this.maxSize = maxSize;
top = -1;
stackArray = new long[maxSize];
}
public void push(long value){
stackArray[++top] = value;
//maxSize++;
}
public long pop(){
return stackArray[top--];
}
public boolean isEmpty(){
return top == -1;
}
public boolean isFull(){
return maxSize == (top+1);
}
public long getTopValue(){
return stackArray[top];
}
}
测试类:
<pre name="code" class="java">class stackApp{
public static void main(String args[]){
Stack stack = new Stack(10);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
while(!stack.isEmpty()){
long value = stack.pop();
System.out.println(" "+value);
}//while
}
}
数组队列:
class queue{
private long[] array;
private int front;
private int rear;
private int nElements;
private int maxSize;
public queue(int s){
maxSize = s;
array = new long[maxSize];
front = 0;
rear = -1;
nElements = 0;
}
public void insert(long value){
if(rear == maxSize-1){
rear = -1;
}
array[++rear] = value;
nElements++;
}
public long remove(){
if(front+1 == maxSize){
front = 0;
}
nElements--;
return array[front++];
}
public long getFrontElement(){
return array[front];
}
public boolean isEmpty(){
return (nElements == 0);
}
public boolean isFull(){
return (maxSize == nElements);
}
public int getNElements(){
return nElements;
}
}
测试类:
class queueApp{
public static void main(String args[]){
queue que = new queue(10);
que.insert(10);
que.insert(20);
que.insert(30);
que.insert(40);
que.insert(50);
while(!que.isEmpty()){
long temp = que.remove();
System.out.println(" "+temp);
}
}
}