Demo entry 6684035

queueADT

   

Submitted by anonymous on Dec 14, 2017 at 09:18
Language: C. Code size: 1.3 kB.

Status InitQueue(LinkQueue &Q)
{
    // 构造一个空队列Q
    if(!(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode))))
        exit(OVERFLOW);
    Q.front->next=NULL;
    return OK;
}


Status QueueEmpty(LinkQueue Q)
{
    //若Q为空队列,则返回TRUE,否则返回FALSE
    if(Q.front==Q.rear)
        return TRUE;
    else
        return FALSE;
}


int QueueLength(LinkQueue Q)
{
    //求队列的长度
    int i=0;
    QueuePtr p;
    p=Q.front;
    while(Q.rear!=p)
    {
        i++;
        p=p->next;
    }
    return i;
}


Status GetHead(LinkQueue Q,QElemType &e)
{
    //若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR
    QueuePtr p;
    if(Q.front==Q.rear)
        return ERROR;
    p=Q.front->next;
    e=p->data;
    return OK;
}


Status EnQueue(LinkQueue &Q,QElemType e)
{
    //插入元素e为Q的新的队尾元素
    QueuePtr p;
    if(!(p=(QueuePtr)malloc(sizeof(QNode)))) // 存储分配失败
        exit(OVERFLOW);
    p->data=e;
    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
    return OK;
}


Status DeQueue(LinkQueue &Q,QElemType &e)
{
    //若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR
    QueuePtr p;
    if(Q.front==Q.rear)
        return ERROR;
    p=Q.front->next;
    e=p->data;
    Q.front->next=p->next;
    if(Q.rear==p)
        Q.rear=Q.front;
    free(p);
    return OK;
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).