#include "stdafx.h"
#include "Queue.h"
#include "malloc.h"
#include "stdlib.h"
#include "memory.h"
CQueue::CQueue()
{
m_pHead=NULL;
m_pLastAccess=NULL;
}
CQueue::~CQueue()
{
PQUEUE_ELEMENT pCur=m_pHead,pTemp;
while(pCur!=NULL)
{
pTemp=pCur->next;
delete pCur;
pCur=pTemp;
}
}
int CQueue::Push(unsigned int nValue,
unsigned int nIndex,
ELEMENT_TYPE eWeight
)
{
PQUEUE_ELEMENT pAdd,pCur=m_pHead,pPre=0;
while(pCur!=NULL&&pCur->eWeight<eWeight)
{
pPre=pCur;
pCur=pCur->next;
}
pAdd=(PQUEUE_ELEMENT)new QUEUE_ELEMENT;
pAdd->nParent=nValue;
pAdd->nIndex=nIndex;
pAdd->eWeight=eWeight;
pAdd->next=pCur;
if(pPre==0)
m_pHead=pAdd;
else
pPre->next=pAdd;
return 1;
}
int CQueue::Pop(unsigned int *npValue,
unsigned int *npIndex,
ELEMENT_TYPE *epWeight,
bool bModify,
bool bFirstGet
)
{
PQUEUE_ELEMENT pTemp;
if(bModify)
pTemp=m_pHead;
else
{
if(bFirstGet)
m_pLastAccess=m_pHead;
pTemp=m_pLastAccess;
}
if(pTemp==NULL)
return -1;
if(npValue!=0)
*npValue=pTemp->nParent;
if(npIndex!=0)
*npIndex=pTemp->nIndex;
if(epWeight!=0)
*epWeight=pTemp->eWeight;
if(bModify)
{
m_pHead=pTemp->next;
delete pTemp;
}
else
{
m_pLastAccess=pTemp->next;
}
return 1;
}
bool CQueue::IsEmpty(bool bBrowsed)
{
if(bBrowsed==true)
return (m_pLastAccess==NULL);
return (m_pHead==NULL);
}
bool CQueue::IsSingle()
{
return (m_pHead!=NULL&&m_pHead->next==NULL);
}