队列_数组实现 [循环队列]

   1:  // 数组队列.cpp : 定义控制台应用程序的入口点。
   2:  //
   3:  #include 
   4:  #include 
   5:  #include  //abs()
   6:  #include 
   7:  #include 
   8:  using namespace std;
   9:   
  10:  //定义一个队列的结构体
  11:  struct MyQueue 
  12:  {
  13:      int nTail;    //队列尾
  14:      int nHead;  //队列头
  15:      int nData[10];    //队列数据
  16:  };
  17:   
  18:  //规则说明:
  19:  //nHead 直接指向了对头的数据位置,
  20:  //nTail 直接指向了队尾要插入的位置,
  21:  //如果nTail==nHead ,队列为空
  22:  //nTail 比 nHead位置小一,(就是在他前面一个),代表队满
  23:  //所以数组中能存放的数据要比数组的空间小1,是为了判断队列满的情况。
  24:   
  25:  MyQueue g_queue;        //定义全局的队列
  26:   
  27:   
  28:   
  29:  //设置队列为空
  30:  int SetEmpty(void)
  31:  {
  32:      g_queue.nTail = 0;        //都设置为零
  33:      g_queue.nHead = 0;
  34:      return 1;
  35:  }
  36:   
  37:  //判断队列是否为满
  38:  bool IsFull(void)
  39:  {
  40:      //如果 ntial 比 nHead小1,则队列满。
  41:      return (g_queue.nTail + 1) % 10 == g_queue.nHead;
  42:  }
  43:   
  44:  //判断队列是否为空
  45:  bool IsEmpty(void)
  46:  {
  47:      return (g_queue.nHead == g_queue.nTail);  //如果尾等于头,则为空
  48:  }
  49:   
  50:   
  51:  //入队
  52:  int EnQueue(int nData)
  53:  {
  54:      if(!IsFull())
  55:      {
  56:          g_queue.nData[g_queue.nTail] = nData;
  57:          g_queue.nTail = (g_queue.nTail + 1) % 10;
  58:          return abs( (g_queue.nTail - g_queue.nHead) % 10 ); //返回当前队列长度
  59:      }
  60:      else
  61:      {
  62:          cout<<"The queue is full, exit(0)"<<endl;
  63:          system("pause");
  64:          return 0;
  65:      }
  66:  }
  69:  //出队
  70:  int DeQueue(void)
  71:  {
  72:      if(!IsEmpty())
  73:      {
  74:          int nData = g_queue.nData[g_queue.nHead];
  75:          g_queue.nHead = (g_queue.nHead + 1) % 10;
  76:          return nData;
  77:      }
  78:      else
  79:      {
  80:          cout<<"The Queue is empty, exit(0)"<<endl;
  81:          system("pause");
  82:          return 0;
  83:      }
  84:  }
  85:   
  86:   
  87:  int _tmain(int argc, _TCHAR* argv[])
  88:  {
  89:      //测试
  90:      SetEmpty();            //设置队列为空
  91:      if (IsEmpty())        //判断为空,输出信息
  92:      {
  93:          printf("The queue is empty!/n");
  94:      }
  95:   
  96:      //
  97:      for (int i = 0; i < 9; ++i)
  98:      {
  99:          EnQueue(i);
 100:      }
 101:      if (IsFull())                //判断队列满
 102:      {
 103:          printf("The Queue is full/n");        //队列满的英语是不是full啊
 104:      }
 105:   
 106:      for (int i = 0; i < 9; ++i)                //出队
 107:      {
 108:          printf("%d ", DeQueue());            
 109:      }
 110:      printf("/n");
 111:   
 112:      if (IsEmpty())                        
 113:      {
 114:          printf("The queue is empty!/n");
 115:      }
 116:   
 117:      for (int i = 0; i < 8; ++i)            //测试出队和入队
 118:      {
 119:          EnQueue(i);
 120:          EnQueue(i*2);
 121:          printf("%d ", DeQueue());
 122:      }
 123:      printf("/n");
 124:   
 125:      while(!IsEmpty())
 126:      {
 127:          printf("%d ", DeQueue());
 128:      }
 129:      printf("/n");
 130:   
 131:   
 132:      SetEmpty();
 133:      for(int i=0; i<12; i++) //当入队9 10 11时,队列已经满
 134:          EnQueue(i);
 135:   
 136:      for(int i=0; i<15; i++)//当出对i=9-14,共6次
 137:          DeQueue();
 138:   
 139:      system("pause");
 140:      return 0;
 141:  }
 142:   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值