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

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

 22}

 23
 24/*=============对循环队列进行操作的函数定义==============*/
 25
 26int 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
 34int quelength(sqque q){/*返回循环队列的元素个数*/
 35    return (q.rear-q.front+MAXQSIZE)%MAXQSIZE;/*===*/
 36}

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

 45
 46
 47int 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/*===================主函数部分==================*/
 56main(){
 57    int i=0;
 58    char tem='A',a,*x;
 59    sqque *squ,sque;
 60    squ=&sque;
 61    initque(squ);
 62    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
posted @ 2009-07-25 10:12  林田惠  阅读(506)  评论(0编辑  收藏  举报


.
    ;  `┣━┒ ; `.┣─┒`   . .   ;   `.        
   .┟━┃┍╄┓┟━│ ╃━  `     、.    
` ┝─┃┣╈┤┣━┃;/ ╈ ╰⌒ˋのˊ  
 . ┗━┘┗┸┛└━┛/┃┻ `.   ′ ˋ `