队列(Java代码实现)

注意:由于本文不是循环队列,所以队满后再出队,之后就不能再入队了,因为队满以后front和rear均指向队尾


import java.util.Scanner;
//测试类
public class Test {
	public static void main(String[] args) {	
		ArrayQueue queue=new ArrayQueue(3);
		char key=' ';
		Scanner scan = new Scanner(System.in);
	    boolean loop=true;
	while(loop) {
		System.out.println("输入's':显示队列");
		System.out.println("输入'e':退出程序");
		System.out.println("输入'a':添加数据到队列");
		System.out.println("输入'g':从队列取出数据");
		System.out.println("输入'h':查看队列头的数据");
		key=scan.next().charAt(0);//读取一个字符
		switch(key) {
		case 's':
			queue.showQueue();
			break;
		case 'a':
			System.out.println("输入一个数");
			int value = scan.nextInt();
			queue.addQueue(value);
			break;
		case 'g':
			try {
				int res=queue.getQueue();
				System.out.printf("取出的数据是%d\n", res);
			}catch(Exception e) {
				System.out.println(e.getMessage());
			}
			break;
		case 'h':
			try {
				int res = queue.headQueue();
				System.out.printf("队列头的数据是%d\n", res);
			}catch (Exception e) {
				System.out.println(e.getMessage());
			}
			break;
		case 'e':
			scan.close();
			loop=false;
			break;
		default:
			break;
		}
		
	}
	System.out.println("程序退出~");
	
	}

}


//队列类
	class ArrayQueue{
		private int maxSize;
		private int front;
		private int rear;
		private int[] arr;
	
//入队
public ArrayQueue(int arrMaxSize) {
	maxSize=arrMaxSize;
	arr=new int[maxSize];
	front=-1;
    rear=-1;
}
		//判满
		public boolean isFull() {
			return rear==maxSize-1;
		}
		
		//判空
		public boolean isEmpty() {
			return rear==front;
		}
		
		//构造器
		public void addQueue(int n)
		{
		if(isFull()) {
			System.out.println("队列满,不能加入数据~");
			return;
		}
		rear++;
		arr[rear]=n;
		}
		
		//出队
		public int getQueue() {
			if(isEmpty()) {
				throw new RuntimeException("队列空,不能取数据");
			}
			front++;
            return arr[front];    		
		}
		
		//打印输出队列数据
		public void showQueue() {
			if(isEmpty()) {
			System.out.println("队列空,没有数据");
			return;
		}
		for(int i=0; i<arr.length; i++)
			 System.out.printf("arr[%d]=%d\n", i, arr[i]);
	}
	
		//输出队首数据
	public int headQueue() {
		if(isEmpty()) {
			throw new RuntimeException("队列空,没有数据");
		}
		return arr[front+1];
	}
	
	
	
}

构造器

//构造器
		public void addQueue(int n)
		{
		if(isFull()) {
			System.out.println("队列满,不能加入数据~");
			return;
		}
		rear++;
		arr[rear]=n;
		}

 判断队列是否已满

//判满
		public boolean isFull() {
			return rear==maxSize-1;
		}

判断队列是否为空 

//判空
		public boolean isEmpty() {
			return rear==front;
		}

入队算法 

//入队
		public void addQueue(int n)
		{
		if(isFull()) {
			System.out.println("队列满,不能加入数据~");
			return;
		}
		rear++;
		arr[rear]=n;
		}

出队算法 

//出队
		public int getQueue() {
			if(isEmpty()) {
				throw new RuntimeException("队列空,不能取数据");
			}
			front++;
            return arr[front];    		
		}

 打印输出队列数据

//打印输出队列数据
		public void showQueue() {
			if(isEmpty()) {
			System.out.println("队列空,没有数据");
			return;
		}
		for(int i=0; i<arr.length; i++)
			 System.out.printf("arr[%d]=%d\n", i, arr[i]);
	}

输出打印队首的数据 

//输出队首数据
	public int headQueue() {
		if(isEmpty()) {
			throw new RuntimeException("队列空,没有数据");
		}
		return arr[front+1];
	}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you with that. Here's the Java code for implementing a blocking queue using Jedis: ```java import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.exceptions.JedisDataException; public class JedisBlockingQueue<T> { private final JedisPool jedisPool; private final String queueName; private final int timeout; public JedisBlockingQueue(JedisPool jedisPool, String queueName, int timeout) { this.jedisPool = jedisPool; this.queueName = queueName; this.timeout = timeout; } public void enqueue(T item) { try (Jedis jedis = jedisPool.getResource()) { jedis.rpush(queueName, item.toString()); } } public T dequeue() throws InterruptedException { try (Jedis jedis = jedisPool.getResource()) { while (true) { // Perform a BLPOP operation with the configured timeout // This operation will block until an item is available in the queue // or until the timeout is reached java.util.List<String> result = jedis.blpop(timeout, queueName); // Check if the result is null, which means the operation timed out if (result == null) { continue; } // Extract the item from the result String item = result.get(1); // Handle the case where the item is null or invalid if (item == null) { continue; } // Successfully extracted the item from the queue, return it return (T) item; } } catch (JedisDataException e) { // The BLPOP operation will throw a JedisDataException if the connection is closed // This can happen if another client closes the connection, or if the Redis server // shuts down Thread.sleep(1000); return null; } } } ``` Note that this implementation assumes that the items being stored in the queue are strings (hence the use of `item.toString()` when enqueuing), but you can modify it to handle other types if needed. Also, the `timeout` parameter specifies the maximum amount of time (in seconds) to block when waiting for an item to become available in the queue. Hope this helps!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值