📄 mywaitqueue.h
字号:
#pragma once
//电梯外部控制中心的等待队列节点类型
typedef struct {
enum direction eMyReqDirection;
int iMyReqFloor;
int iMyReqTime; //请求的等待时间
bool bMyReClaimed;//是否被回收的节点,等待时间过长则收回节点
} sOuterRequest;
//控制中心的等待队列
template <class T>
class CMyWaitQueue;
// 等待的节点定义
template <class T>
class CMyWaitQueueNode
{
friend CMyWaitQueue<T>;
private:
T data;
CMyWaitQueueNode<T>* next;
};
// 链表定义
template <class T>
class CMyWaitQueue
{
public:
CMyWaitQueue();
~CMyWaitQueue();
bool cutHead(T& x);//从头取出任务
bool appendTail(const T& temp);//在尾部加任务
int getLength();
int searchNode(const T& temp);
void refreshLink();
private:
CMyWaitQueueNode<T>* pLinkTail;//循环链表
int iLinkLength;
private:
bool isEmpty();
void removeAll();
public:
};
template <class T>
CMyWaitQueue<T>::CMyWaitQueue()
{
pLinkTail=NULL;
iLinkLength=0;
}
// 得到链表长度
template <class T>
int CMyWaitQueue<T>::getLength()
{
return iLinkLength;
}
template <class T>
CMyWaitQueue<T>::~CMyWaitQueue()
{
removeAll();
}
// 判断链表是否为空
template <class T>
bool CMyWaitQueue<T>::isEmpty()
{
return getLength()==0;
}
// 清空链表
template <class T>
void CMyWaitQueue<T>::removeAll()
{
if(isEmpty()) return;
CMyWaitQueueNode<T>* curNode=pLinkTail->next;
CMyWaitQueueNode<T>* tmpNode;
while(curNode)
{
tmpNode = curNode;
curNode=curNode->next;
delete tmpNode;
}
tmpNode = NULL;
pLinkTail = NULL;
iLinkLength =0;
}
// 向链表尾部加上一个元素
template <class T>
bool CMyWaitQueue<T>::appendTail(const T& temp)
{
CMyWaitQueueNode<T>* newNode=new CMyWaitQueueNode<T>;
if(!newNode) return FALSE;
newNode->data=temp;
if(isEmpty())
{
pLinkTail = newNode;
pLinkTail->next = pLinkTail;
}
else
{
newNode->next = pLinkTail->next;
pLinkTail->next = newNode;
pLinkTail = newNode;
}
iLinkLength++;
return TRUE;
}
// 在链表中寻找值为temp的节点
template <class T>
int CMyWaitQueue<T>::searchNode(const T& temp)
{
if(isEmpty()) return 0;
CMyWaitQueueNode<T>* current=pLinkTail->next;
int nPos=0;
for(int i=1; i<=iLinkLength; current=current->next,i++)
{
if(((current->data).eMyReqDirection==temp.eMyReqDirection) && ((current->data).iMyReqFloor==temp.iMyReqFloor))
{
nPos = i;
break;
}
}
nPos = ((nPos==iLinkLength+1) ?0:nPos);//返回0说明没有找到
return nPos;
}
// 每次取走队列的头进行任务的处理
template <class T>
bool CMyWaitQueue<T>::cutHead(T& temp)
{
if(isEmpty()) return FALSE;
CMyWaitQueueNode<T>* tmpNode =NULL;
if(pLinkTail->next==pLinkTail)//如果只有一个节点
{
temp = pLinkTail->data;
delete pLinkTail;
pLinkTail = NULL;
}
else
{
tmpNode = pLinkTail->next; //tmpNode指向队首节点
pLinkTail->next = tmpNode->next;
temp = tmpNode->data;
delete tmpNode;
tmpNode = NULL;
}
iLinkLength--;
return TRUE;
}
template <class T>
void CMyWaitQueue<T>::refreshLink()
{
if(isEmpty()) return;
CMyWaitQueueNode<T>* curNode=pLinkTail->next;
while(curNode && curNode->data.bMyReClaimed)
{
curNode->data.bMyReClaimed = FALSE;
curNode=curNode->next;
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -