队列的实现

1 #include"../utili.h"
  2
  3  typedef struct Queue
  4 {
  5     ElemType *base;
  6     int front;
  7     int rear;
  8 }Queue;
  9
 10 void init_queue(Queue *Q);//初始化队列
 11 void enqueue(Queue *Q,ElemType x);//入队
 12 void dequeue(Queue *Q);//出队
 13 ElemType gethead(Queue *Q);//获取队头元素
 14 bool isfull(Queue *Q);//判满
 15 bool isempty(Queue *Q);//判空
 16 void show(Queue *Q);
 17 //
 18 bool isfull(Queue *Q)
 19 {
 20     if(Q->rear >= DEFAULT_SIZE)
 21         return TRUE;
 22     return FALSE;
 23 }
 24
 25 bool isempty(Queue *Q)
 26 {
 27     if(Q->front == Q->rear)
 28         return TRUE;
 29     return FALSE;
 30 }
 31
 32 void init_queue(Queue *Q)
 33 {
 34     Q->base = (ElemType*)malloc(sizeof(ElemType)*DEFAULT_SIZE);
 35     assert(Q->base != NULL);
 36     Q->front = 0;
 37     Q->rear = 0;
 38 }
39
 40 void show(Queue *Q)
 41 {
 42     for(int i = Q->front;i<Q->rear;++i)
 43     {
 44         cout<<Q->base[i]<<endl;
 45     }
 46 }
 47
 48 void dequeue(Queue *Q)
 49 {
 50     if(isempty(Q))
 51     {
 52         cout<<"queue empty"<<endl;
 53     }
 54     Q->front++;
 55 }
 56
 57 void enqueue(Queue *Q,ElemType x)
 58 {
 59
 60     if(isfull(Q))
 61     {
 62         cout<<"queue full"<<endl;
 63     }
 64     Q->base[Q->rear++] = x;
 65 }
 66 ElemType gethead(Queue *Q)
 67 {
 68     if(isempty(Q))
 69     {
 70         cout<<"queue empty,no head"<<endl;
 71         return;
 72     }
 73     return Q->base[Q->front];
 74 }

39
 40 void show(Queue *Q)
 41 {
 42     for(int i = Q->front;i<Q->rear;++i)
 43     {
 44         cout<<Q->base[i]<<endl;
 45     }
 46 }
 47
 48 void dequeue(Queue *Q)
 49 {
 50     if(isempty(Q))
 51     {
 52         cout<<"queue empty"<<endl;
 53     }
 54     Q->front++;
 55 }
 56
 57 void enqueue(Queue *Q,ElemType x)
 58 {
 59
 60     if(isfull(Q))
 61     {
 62         cout<<"queue full"<<endl;
 63     }
 64     Q->base[Q->rear++] = x;
 65 }
 66 ElemType gethead(Queue *Q)
 67 {
 68     if(isempty(Q))
 69     {
 70         cout<<"queue empty,no head"<<endl;
 71         return;
 72     }
 73     return Q->base[Q->front];
 74 }

/

1 #include"../utili.h"
  2 #include"queue.h"
  3
  4 int main()
  5 {
  6     Queue Q;
  7     init_queue(&Q);
  8     enqueue(&Q,1);
  9     enqueue(&Q,2);
 10     enqueue(&Q,3);
 11     enqueue(&Q,4);
 12     show(&Q);
 13     cout<<"==============="<<endl;
 14     dequeue(&Q);
 15     show(&Q);
 16     return 0;
 17 }   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值