队列-实现栈

来源:互联网 发布:虚拟充值软件靠谱吗 编辑:程序博客网 时间:2024/06/06 23:01
package d_queue.E_queue_stack;


public class Deque {
private int[] arr;
private int maxSize;
private int left;
private int right;
private int aItems;


public Deque(int s) {
maxSize = s;
arr = new int[maxSize];
right = 0;
left = -1;
aItems = 0;
}


public void push(int key) {
if (right == maxSize)
right = 0;
arr[right++] = key;
aItems++;


}


public int pop() {
int temp = -1;
if (aItems != 0) {


if (left == -1)
left = maxSize - 1;
// 锟斤拷锟秸碉拷锟斤拷锟斤拷锟�
while (arr[left--] == 0) {
if (left == -1)
left = maxSize - 1;
}
temp = arr[left+1];


}
aItems--;
return temp;
}


public boolean isEmpty() {
return aItems == 0;
}


public boolean isFull() {
return aItems == maxSize;
}


}package d_queue.E_queue_stack;


/**
 * 双锟剿讹拷锟斤拷 支锟街伙拷锟斤拷式
 * 
 * @author Administrator
 * 
 */
public class DequeApp {
public static void main(String[] args) {
Deque theDeque = new Deque(8);
theDeque.push(453);
theDeque.push(453);
theDeque.push(4);
theDeque.push(453);
theDeque.push(417);
theDeque.push(173);


// theDeque.insertRight(103);
// theDeque.insertRight(3);


while (!theDeque.isEmpty()) {
System.out.println(theDeque.pop());
}
}
}
0 0
原创粉丝点击