您的位置:首页 > 其它

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

2011-06-17 09:55 387 查看
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:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: