链接队列(Link Queue)——队列的链接实现

之前讨论的连续实现的队列结构(队列(Queue)——先进先出(FIFO)的数据结构(Data Structures))与连续栈结构存在着同样的缺点,即都需要大片连续的存储空间。而这个问题在队列结构里更加严重。解决的办法则与连续栈结构问题的办法一样:使用链接队列。链接队列实现除了不需要连续空间外,其所支持的操作类型和数量与连续队列完全一样。

连接队列的类定义与连续队列相比,数据成员为头尾两个指向Node结构的指针,这里的Node既可以是节点的定义,也可以死任意用户定义的结构。

下面是链接队列类定义。

//Link Queue in C++

typedef int QueueEntry;
const int success = 0;
const int overflow = 1;
const int underflow = 2;
const int failure = -1;
const int NULL = 0;

const int maxqueue = 100;										//队列的最大尺寸

struct Node														//链接队列的节点定义
{
	QueueEntry data;
	Node * next;
};

class Queue
{
public:
	Queue::Queue()												//构建函数,初始化一个空队列
	{
		head = tail = NULL;
	}

	bool Queue::empty() const									//检查队列是否为空
	{
		if (tail == NULL || head == NULL)
			return true;
		return false;
	}

	int Queue::append(const QueueEntry &item)					//将元素item插入队列队尾
	{
		Node*new_tail = new Node;
		if (new_tail == NULL)
			return overflow;
		else
		{
			new_tail->data = item;
			new_tail->next = NULL;
		}
		if (tail == NULL)
			head = tail = new_tail;
		else
		{
			tail->next = new_tail;
			tail = new_tail;
		}
		return success;
	}

	int Queue::serve()											//删除队头元素
	{
		if (head == NULL)
			return underflow;
		Node * old_head = head;
		head = old_head->next;
		if (head == NULL)
			tail = NULL;
		delete old_head;
		return success;
	}

	int Queue::retrieve(QueueEntry &item) const					//取出队头元素
	{
		if (head == NULL)
			return underflow;
		item = head->data;
		return success;
	}

	Queue::~Queue()												//构析函数
	{
		if (head != NULL)
		{
			if(head->next = NULL)
				delete head;
			else
			{
				Node *old_head = head;
				head = head->next;
				do {
					delete old_head;
					old_head = head;
					head = head->next;
				} while (head->next != NULL);
			}
		}
	}
	
protected:
	Node * head, * tail;
};


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java的多线程来实现下载队列。以下是一个简单的示例代码: ```java import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; public class DownloadQueue { private Queue<String> queue; public DownloadQueue() { queue = new ConcurrentLinkedQueue<>(); } public void enqueue(String url) { queue.add(url); } public void startDownloads(int numThreads) { for (int i = 0; i < numThreads; i++) { Thread thread = new Thread(new DownloadTask()); thread.start(); } } private class DownloadTask implements Runnable { @Override public void run() { while (true) { String url = queue.poll(); if (url == null) { break; } // 下载操作,这里只打印url System.out.println("Downloading: " + url); // 模拟下载过程 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { DownloadQueue downloadQueue = new DownloadQueue(); downloadQueue.enqueue("http://example.com/file1"); downloadQueue.enqueue("http://example.com/file2"); downloadQueue.enqueue("http://example.com/file3"); downloadQueue.enqueue("http://example.com/file4"); downloadQueue.enqueue("http://example.com/file5"); downloadQueue.startDownloads(3); } } ``` 在上面的示例中,`DownloadQueue`类使用`ConcurrentLinkedQueue`作为队列,以确保线程安全。`enqueue`方法用于将下载链接加入队列中。`startDownloads`方法会启动指定数量的线程来进行下载任务。 `DownloadTask`是一个内部类实现了`Runnable`接口,用于执行实际的下载操作。每个线程会不断从队列中取出链接进行下载,直到队列为空。 在`main`方法中,我们创建了一个`DownloadQueue`实例,并加入了一些下载链接。然后调用`startDownloads`方法启动了3个下载线程。 请注意,这只是一个简单的示例,实际的下载操作需要根据具体需求来实现

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值