java数组堆栈_Java数组实现堆栈和队列

数组堆栈:

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];

}

}

测试类:

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);         }     } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值