Queue


class Queue{

private int maxSize;

private long[] queArray;

private int front;

private int rear;

private int nItems;

public Queue(int s){

maxSize=s;

queArray=new long[maxSize];

front=0;

rear=-1;

nItems=0;

}

public void insert(long j){

if(rear==maxSize-1)//wraparound

rear=-1;

queArray[++rear]=j;

nItems++;

}

public long remove(){//take item from front of queue

long temp=queArray[front++];

if(front==maxSize)

front=0;

nItems--;

return temp;

}

public long peekFront(){

return queArray[front];

}

public boolean isEmpty(){

return (nItems==0);

}

public boolean isFull(){

return (nItems==maxSize);

}

public int size(){

return nItems;

}

}


public class queue {

public static void main(String[] args){

Queue theQueue=new Queue(5);

theQueue.insert(10);

theQueue.insert(20);

theQueue.insert(30);

theQueue.remove();

theQueue.remove();

theQueue.insert(50);

theQueue.insert(60);

while(!theQueue.isEmpty()){

long n=theQueue.remove();

System.out.print(n+" ");

}

System.out.println("");

}

}


///
/Priority Queue
///


public class queue {

public static void main(String[] args){

PriorityQ thePQ=new PriorityQ(5);

thePQ.insert(30);

thePQ.insert(50);

thePQ.insert(10);

while(!thePQ.isEmpty()){

long item=thePQ.remove();

System.out.print(item+" ");

}

System.out.println("");

}

}

class PriorityQ{

//array in sorted order,from max at 0 to min at size-1

private int maxSize;

private long[] queArray;

private int nItems;

public PriorityQ(int s){

maxSize=s;

queArray=new long[maxSize];

nItems=0;

}

public void insert(long item){

int j;

if(nItems==0)

queArray[nItems++]=item;

else{

for(j=nItems-1;j>=0;j--){

if(item>queArray[j])

queArray[j++]=queArray[j];

else

break;

}

}

}

public long remove(){

return queArray[--nItems];

}

public long peekMin(){

return queArray[nItems-1];

}

public boolean isEmpty(){

return (nItems==0);

}

public boolean isFull(){

return (nItems==maxSize);

}

}




### Python `queue.Queue` 的使用方法 #### 创建队列对象 为了创建一个 `queue.Queue` 对象,可以简单地调用其构造函数而无需传递任何参数。这会初始化一个新的、空的FIFO(先进先出)队列实例。 ```python import queue q = queue.Queue() ``` #### 向队列入数据 (put 方法) 通过 `put(item)` 方法向队列中添加项目。此操作会在必要时阻塞直到有可用的空间为止(如果指定了最大尺寸并已满),除非设置了超时时间[^2]。 ```python for i in range(5): q.put(i) ``` #### 从队列取数据 (get 方法) 利用 `get()` 方法可以从队列移除并返回最前面的一项;当队列为非空时立即完成该动作,否则将一直等待直至获取到一项或发生指定的超时期限。 ```python while not q.empty(): item = q.get() print(f'Got {item} from the queue.') ``` #### 检查队列是否为空 (empty 方法) 可以通过 `empty()` 来判断当前队列里是否有剩余项存在。注意即使刚刚执行过一次成功的 `get()`, 队列也可能瞬间被其他线程填充而不为空。 ```python if not q.empty(): print('The queue is not empty yet.') else: print('The queue has been emptied.') ``` #### 获取队列大小 (qsize 方法) 虽然不是所有平台上都支持精确计数,但是大多数情况下还是能够依靠 `qsize()` 获得近似的未处理条目数量作为参考依据。 ```python print(f'There are approximately {q.qsize()} items waiting in the queue.') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值