每天一个数据结构----队列的顺序存储结构实现(纯代码)

//

//  main.c

//  Queue 循环队列

//

//

//  Created by Jacobs.Guo on 2018/5/7.

//  Copyright © 2018年 yage guo. All rights reserved.

//



#include <stdio.h>



#define OK 1

#define ERROR 0

#define TRUE 1

#define FALSE 0



#define MAXSIZE 10

typedef int Status;

typedef int QElemType;



typedef struct

{

    QElemType data[MAXSIZE];

    int head;

    int tail;

}Queue;



//初始化队列

Status InitQueue(Queue *Q)

{

    Q->head = 0;

    Q->tail = 0;

    

    return OK;

}

//队列是否已经满员

Status FullQueue(Queue *Q)

{

    if ( (Q->tail+1)%MAXSIZE == Q->head )

        return OK;

    else

        return FALSE;

    

}

//入队列操作

Status EnQueue(Queue *Q,QElemType e)

{

    if (FullQueue(Q)) return ERROR;

    

    Q->data[Q->tail] = e;

    

    Q->tail = (Q->tail+1)%MAXSIZE;

    return OK;

}

//队列长度

int LengthQueue(Queue Q)

{

    return (Q.tail-Q.head+MAXSIZE)%MAXSIZE;

}

//出队列操作

Status DeQueue(Queue *Q,QElemType *e)

{

    if (Q->head == Q->tail) return ERROR;

   

    *e = Q->data[Q->head];

    Q->head = (Q->head+1)%MAXSIZE;

    

    return OK;

}





int main() {

    Queue Q;

    int i;QElemType e;

    printf("初始化队列Q...\n");

    InitQueue(&Q);



    if (FullQueue(&Q)) printf("队列Q为空...\n");

    else printf("队列Q非空...\n");

    printf("队列Q的长度为:%d\n",LengthQueue(Q));

    

    

    printf("将1 2 3 4 5 做顺序入队列操作...\n");

    for (i = 0;i<5;i++)

    {

        EnQueue(&Q,i+1);

    }

    printf("队列Q的长度为:%d\n",LengthQueue(Q));

    

    DeQueue(&Q, &e);

    printf("%d已经出队列...\n",e);

    

    return 0;

}



 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值