队列的基本实现

Queue.h

#pragma ince
typedef int DataType;
typedef struct QueueNode//队列节点
{
    QDataType _data;
    struct ListNode* _pnext;

}ListNode, *PListNode;
 struct Queue
{
    PListNode _phead;
    PListNode _ptail;
};

void QueueInit(Queue* q);//初始化队列
void QueuePush(Queue* q,DataType x);//入队列
void QueuePop(Queue* q);//出队列
DataType QueueFront(Queue* q);//取得对头元素
DataType QueueBack(Queue* q);//取得队尾元素
int QueueSize(Queue* q);//队列大小
int QueueEmpty(Queue* q);//判断队列是否为空


PListNode BuyListNode(QDataType data)//创建节点
{
    PListNode pNewNode = (PListNode)malloc(sizeof(ListNode));
    if(NUll == pNewNode)
    {
        assert(0);
        return NUll;
    }
    pNewNode->_data = data;
    pNewNode->_pnext = NUll;
    return pNewNode;
}

void QueueInit(Queue* q)//初始化队列
{
    assert(q);
    q->_phead = q->_ptail = BuyListNode(0);

}

void QueuePush(Queue* q,QDataType data)//入队列
{
    assert(q);
    q->_ptail = BuyListNode(data);
    q->_ptail = q->_ptail->_pnext;
}

void QueuePop(Queue* q)//出队列
{
    PListNode PDelNode = NULL;
    assert(q);
    PDelNode = q->_phead->_pnext;
    if(PDelNode)
    {
        q->_phead->_pnext = PDelNode->_pnext;
        free(PDelNode);
    }
}
int QueueSize(Queue* q)//队列大小
{
    PListNode pCur;
    int count = 0;
    assert(q);
    pCur = q->_phead->_pnext;
    while(pCur)
    {
        count++;
        pCur = pCur->_pnext;
    }
    return count;
}
int QueueEmpty(Queue* q)//判断队列是否为空
{
    return NULL = q->_phead->_pnext;
}
DataType QueueFront(Queue* q)//取得对头元素
{
    return q->_phead->_pnext->_data;
}
DataType QueueBack(Queue* q)//取得队尾元素
{
    return q->_ptail->_data;
}

猜你喜欢

转载自blog.csdn.net/qq_41112517/article/details/80292258