顺序队列的基本操作

 

 

 

  /*顺序队的基本操作*/
     #include <stdio.h>
#include <malloc.h>
     #define MaxSize 100
     typedef char ElemType;
     typedef struct
     {
  ElemType queue[MaxSize];
         int front,rear;
     } queuetype;
     void initqueue(queuetype *Q)
     {
       Q->front=Q->rear=-1;
     }
     void enter(queuetype *Q,ElemType x)
     {
       if (Q->rear==MaxSize) printf("队列上溢出!/n");
       else
       {
   Q->rear++;         /*队尾指针后移*/
   Q->queue[Q->rear]=x;   /*新元素赋给队尾单元*/
       }
    }
    void ddelete(queuetype *Q)
    {
      if (Q->front==Q->rear)
  printf("队列为空!/n");
      else
  Q->front++;
    }

    ElemType gethead(queuetype *Q)
    {
      if (Q->front==Q->rear)
  printf("队列为空!/n");
      else
  return(Q->queue[Q->front+1]);
    }
// *//*
    int empty(queuetype *Q)
    {
      if (Q->front==Q->rear) return(1);  /*为空,则返回true*/
      else return(0);               /*不为空,则返回flase*/
    }
    void display(queuetype *Q)
    {
      int i;
      printf("队列元素:");
      for (i=Q->front+1;i<=Q->rear;i++)
  printf("%c ",Q->queue[i]);
      printf("/n");
    }
   void main()
    {
      queuetype *qu;
   qu = (queuetype *)malloc(sizeof(queuetype));

      printf("初始化队列qu/n");
      initqueue(qu);
      printf("队列空:%d/n",empty(qu));
      printf("依次入队a,b,c,d元素/n");
      enter(qu,'a');
      enter(qu,'b');
      enter(qu,'c');
      enter(qu,'d');
      display(qu);
      printf("出队一次/n");
      ddelete(qu);
      printf("队首元素:%c/n",gethead(qu));
      printf("出队一次/n");
      ddelete(qu);
      display(qu);
    }
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值