循环队列--队列的顺序存储表示形式[原创]

1
  1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /*==============循环队列--队列的顺序存储表示形式====================*/
  2 #include  < stdio.h >
  3 #define  MAXQSIZE 20
  4
  5 ExpandedBlockStart.gifContractedBlock.giftypedef  struct {/**//*====循环队列的类型定义===*/
  6    char *base;
  7    int front;
  8    int rear;
  9}
sqque;
 10 ExpandedBlockStart.gifContractedBlock.gif /**/ /*==========常用的被调用函数定义=================*/
 11 int  quelength(sqque q);
 12
 13 ExpandedBlockStart.gifContractedBlock.gif void  print(sqque q) {
 14    int a,i;
 15    i=quelength(q);
 16    a=q.front;
 17    printf("The queue:");
 18ExpandedSubBlockStart.gifContractedSubBlock.gif    while(i--){
 19        printf("%c ",q.base[a]);
 20        a=(a+1)%MAXQSIZE;
 21    }

 22}

 23
 24 ExpandedBlockStart.gifContractedBlock.gif /**/ /*=============对循环队列进行操作的函数定义==============*/
 25
 26 ExpandedBlockStart.gifContractedBlock.gif int  initque(sqque  * q) {/**//*初始化一个循环队列*/
 27    q->base=(char *)malloc(MAXQSIZE*sizeof(char));
 28    if(!q->base) exit(0);
 29    q->front=q->rear=0;
 30    return 1;
 31}

 32
 33
 34 ExpandedBlockStart.gifContractedBlock.gif int  quelength(sqque q) {/**//*返回循环队列的元素个数*/
 35ExpandedSubBlockStart.gifContractedSubBlock.gif    return (q.rear-q.front+MAXQSIZE)%MAXQSIZE;/**//*===*/
 36}

 37
 38
 39 ExpandedBlockStart.gifContractedBlock.gif int  enque(sqque  * q, char  e) {/**//*入队函数*/
 40    if((q->rear+1)%MAXQSIZE == q->front) return 0;
 41ExpandedSubBlockStart.gifContractedSubBlock.gif    q->base[q->rear]=e;/**//*===指针的运算。。。===*/
 42    q->rear=(q->rear+1% MAXQSIZE;
 43    return 1;
 44}

 45
 46
 47 ExpandedBlockStart.gifContractedBlock.gif int  deque(sqque  * q, char   * e) {/**//*删除对头元素*/
 48    if(q->front == q->rear) return 0;
 49    *e=q->base[q->front];
 50    q->front=(q->front+1% MAXQSIZE;
 51    return 1;
 52}

 53
 54
 55 ExpandedBlockStart.gifContractedBlock.gif /**/ /*===================主函数部分==================*/
 56 ExpandedBlockStart.gifContractedBlock.gifmain() {
 57    int i=0;
 58    char tem='A',a,*x;
 59    sqque *squ,sque;
 60    squ=&sque;
 61    initque(squ);
 62ExpandedSubBlockStart.gifContractedSubBlock.gif    for(i=1;i<=16;i++){
 63        enque(squ,tem++);
 64    }

 65
 66    print(sque);
 67    printf("\nThere are %d elements in this queue\n",quelength(sque));
 68    x=&a;
 69    deque(squ,x);
 70    printf("\nAfter delque,");
 71    print(sque);
 72    printf("\nThe deleted element:%c",a);
 73
 74
 75
 76getch();
 77
 78}

 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101

转载于:https://www.cnblogs.com/LinderMan/archive/2009/07/25/1530754.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值