队列

队列

队列是一种表。
使用队列时插入在一端进行而删除在另一端进行。

操作

1.Enqueue(入队),它是在对列的末端(叫做队尾(rear))插入一个元素
2.Dequeue(出队),它是删除(或返回)在表的开头(叫做队头(front))的元素

数组实现:

//queue.h
typedef int ElementType;
struct QueueRecord;
typedef struct QueueRecord *Queue;

int IsEmpty( Queue Q );
int IsFull( Queue Q );
Queue CreateQueue( int MaxElements );
void DisposeQueue( Queue Q );
void MakeEmpty( Queue Q );
void Enqueue( ElementType X, Queue Q );
ElementType Front( ElementType X, Queue Q );
void Dequeue( Queue Q );
ElementType FrontAndDequeue( Queue Q );

队列所用到的结构体:

struct QueueRecord {
    int Capacity;
    int Front;
    int Rear;
    int Size;
    ElementType *Array;
};

测试队列是否为空:

int IsEmpty( Queue Q )
{
    return Q->Size == 0;
}

列是否已满:

int IsFull( Queue Q )
{
    return Q->Size == Q->Capacity;
}

创建一个空的队列:

Queue CreateQueue( int MaxElements )
{
    Queue Q;

    if( MaxElements < MinQueueSize )
        fprintf(stderr, "Queue size is too small");

    Q = (Queue) malloc( sizeof( struct QueueRecord) * MaxElements );
    if( Q == NULL )
        fprintf(stderr, "Out of space!!!");
    Q->Array = (ElementType *) malloc( sizeof( ElementType) );
    if( Q->Array == NULL )
        fprintf( stderr, "Out of space!!!" );
    Q->Capacity = MaxElements;
    MakeEmpty( Q );

    return Q;
}

构造一个空的对列

void MakeEmpty( Queue Q )
{
    Q->Size = 0;
    Q->Front = 1;
    Q->Rear = 0;
}

销毁一个队列:

void DisposeQueue( Queue Q )
{
    if( Q != NULL )
    {
        free( Q->Array );
        free( Q );
    }
}

入队

void Enqueue( ElementType X, Queue Q )
{
    if(IsFull( Q ))
        fprintf( stderr, "Full queue" );
    else
    {
        Q->Size++;
        Q->Rear = Succ( Q->Rear, Q );
        Q->Array[ Q->Array ] = X;
    }
}

入队的辅助函数:

static int Succ( int Value, Queue Q )
{
    if( ++Value == Q->Capacity )
        Value = 0;
    return Value;
}

查看队首元素:

ElementType Front( Queue Q )
{
    if(!IsEmpty( Q ))
        return Q->Array[ Q->Front ];
    fprintf( "Empty queue" );

    return 0;
}

返回并删去队首的元素:

ElementType FrontAndDequeue( Queue Q )
{
    ElementType X = 0;

    if( IsEmpty( Q ) )
        fprintf( "Empty queue");
    else{
        Q->Size--;
        X = Q->Array[ Q->Front ];
        Q->Front = Succ( Q->Front, Q);
    }

    return X;
}

可以将队列看做一个圈,Enqueue:如果队列没满,则在队尾存一个元素,Size++,Rear++,如果元素超过了 Capacity,则将其制0(圈嘛,最后一个元素的下一个即是第一个)。Dequeue:删去队尾的一个元素,Size–,Front++,如果元素超过了 Capacity,则将其制0。

队列的链式实现:

typedef  int QElemType;
typedef struct node {
    QElemType data;
    struct node * next;
} QNode, * QueuePtr;
typedef struct {
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;

//构造一个空的队列 
bool InitQueue(LinkQueue &Q);
//销毁队列Q,Q不存在 
bool DestoryQueue(LinkQueue &Q); 
//将Q清空为空队列
bool ClearQueue(LinkQueue &Q);
//若队列Q为空队列,则返回true,否则返回false
bool QueueEmpty(LinkQueue &Q);
//返回队列的长度
int Queuelength(LinkQueue &Q);
//若队列不空,则用e返回Q的队头
bool GetQueue(LinkQueue &Q, QElemType &e);
//插元素入元素e为Q的新的队尾元素
bool EnQueue(LinkQueue &Q, QElemType e);
//若对列不空。则删除Q的队头元素,则e返回其值,并返回true,否则返回false
bool Dequeue(LinkQueue &Q, QElemType &e);

初始化:

bool InitQueue(LinkQueue &Q)
{
    //构造一个空的对列Q
    Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
    //Q.front = Q.rear;
    if(!Q.front)
        exit(1);
    Q.front->next = NULL;

    return true;
}

销毁一个队列:

bool DestroyQueue(LinkQueue &Q)
{
    //销毁对列Q
    while(Q.front){
        Q.rear = Q.front->next;
        free(Q.front);
        Q.front = Q.rear;
    }

    return true;
}

入队:

bool EnQueue(LinkQueue &Q, QElemType e){
    //插入元素e为Q的新的队尾元素
    QueuePtr p;
    p = (QueuePtr) malloc(sizeof(QNode));
    if(!p)
        exit(1);
    p->data = e;
    p->next = NULL;
    Q.rear->next = p;
    Q.rear = p;
    return true;
}

出队:

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

询问是否为空

bool QueueEmpty(LinkQueue &Q){
    if(Q.front->next == NULL)
        return true;
    else
        return false;
}

队列的长度:

int Queuelength(LinkQueue &Q){
    int count = 0;
    QueuePtr p;
    p = Q.front;
    while(p != Q.rear){
        count++;
        p = p->next;
    }

    return count;
}

查看队首的元素:

bool GetQueue(LinkQueue &Q, QElemType &e){
    if(Q.front->next != NULL)
        e = Q.front->next->data;
    else
        return false;
    return true;
}

一个测试函数:

int main(void)
{
    LinkQueue Q;
    InitQueue(Q);
    for(int i = 0; i < 10; i++)
    {
        QElemType e;
        printf("请输入一个数字:");
        scanf("%d", &e); 
        if(EnQueue(Q, e) )
            printf("成功");
    }
    QElemType e;
    if (Dequeue(Q, e))
        printf("成功\n");
    printf("出队:%d, 队列的长队%d\n", e, Queuelength(Q));
    GetQueue(Q, e);
    printf("队头的元素是:%d\n", e); 
    if(QueueEmpty(Q))
        printf("队列为空\n");
    else
        printf("队列不为空\n");
    for(int i = 0; i < 9; i++){
        if(Dequeue(Q, e))
            printf("成功\n");
        printf("出队:%d\n", e); 
    }
    if(QueueEmpty(Q))
        printf("队列为空\n");
    else
        printf("队列不为空\n");
    return 0;   
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值