Implementation of cyclic queue

Source: Internet
Author: User

 

  
  
  1. # Ifndef SQQUEUE_H_INCLUDED
  2. # Define SQQUEUE_H_INCLUDED/* prevent repeated inclusion */
  3.  
  4. //////////////////////////////////////// //
  5. // Contains the header file
  6. # Include <stdlib. h>
  7. # Include "ds. h" // OK, Status, and other definitions
  8.  
  9. // Data element type (int type is used by default)
  10. # Ifndef ElemType
  11. # Define ElemType int
  12. # Define USE_DEFAULT_ELEMTYPE/* use the default type flag */
  13. # Endif // ElemType
  14.  
  15. //////////////////////////////////////// //
  16. // The storage structure of the cyclic queue
  17.  
  18. # Define MAXQSIZE 500/* Maximum capacity of cyclic queue */
  19. Typedef struct {
  20. /* TODO (#1 #): define the type of the cyclic queue */
  21. ElemType * base;
  22. Int front;
  23. Int rear;
  24. //....................................
  25. } SqQueue;
  26.  
  27.  
  28. //////////////////////////////////////// //
  29. // Basic operations of the cyclic queue
  30.  
  31. // Construct an empty queue Q
  32. Status InitQueue (SqQueue & Q)
  33. {
  34. /* TODO (#2 #): Create an empty queue */
  35. Q. base = (ElemType *) malloc (MAXQSIZE * sizeof (ElemType ));
  36. If (! Q. base) exit (OVERFLOW );
  37. QQ. front = Q. rear = 0;
  38. Return OK; // TODO: replace this line of code.
  39. //....................................
  40. }
  41.  
  42. // Destroy queue Q
  43. // Prerequisite: the queue Q already exists.
  44. Status DestroyQueue (SqQueue & Q)
  45. {
  46. /* TODO (#3 #): destroys a queue */
  47. Free (Q. base );
  48. Q. base = NULL;
  49. Q. front = 0;
  50. Q. rear = 0;
  51. Return OK;
  52. //....................................
  53. }
  54.  
  55. // Clear the queue Q As an empty queue
  56. // Prerequisite: the queue Q already exists.
  57. Status ClearQueue (SqQueue & Q)
  58. {
  59. /* TODO (#4 #): Clear the queue */
  60. Q. base = 0;
  61. Q. rear = 0;
  62. Return OK;
  63. //....................................
  64. }
  65.  
  66. // If the queue Q is empty, TRUE is returned; otherwise, FALSE is returned.
  67. // Prerequisite: the queue Q already exists.
  68. Status QueueEmpty (SqQueue Q)
  69. {
  70. /* TODO (#5 #): determines whether the queue is empty */
  71. If (Q. front = Q. rear)
  72. Return OK;
  73. Else
  74. Return ERROR;
  75. //....................................
  76. }
  77.  
  78. // Return the number of elements in the queue Q, that is, the queue length.
  79. // Prerequisite: the queue Q already exists.
  80. Int QueueLength (SqQueue Q)
  81. {
  82. /* TODO (#6 #): Return queue length */
  83. Return (q. rear-Q.front + MAXQSIZE) % MAXQSIZE;
  84. //....................................
  85. }
  86.  
  87. // Use e to return the Q header element of the queue.
  88. // Prerequisite: the queue Q exists and is not empty.
  89. Status GetHead (SqQueue Q, ElemType & e)
  90. {
  91. /* TODO (#7 #): store the element from the queue header to e */
  92. If (Q. rear = Q. front)
  93. Return ERROR;
  94. E = Q. base [Q. front];
  95. // E = * (Q. base + Q. front );
  96. Return OK; // return the operation status (success: OK, failure: ERROR)
  97. //....................................
  98. }
  99.  
  100. // Insert element e as the new team end element of queue Q
  101. // Prerequisite: the queue Q exists and is not full
  102. Status EnQueue (SqQueue & Q, ElemType e)
  103. {
  104. /* TODO (#8 #): Element e enters the queue */
  105. If (Q. rear + 1) % MAXQSIZE = Q. front)
  106. Return ERROR;
  107. // E = * (Q. base + Q. rear );
  108. Q. base [Q. rear] = e;
  109. Q. rear = (Q. rear + 1) % MAXQSIZE;
  110. Return OK; // return the operation status (success: OK, failure: ERROR)
  111. //....................................
  112. }
  113.  
  114. // Delete the queue Header element of queue Q and return it with e
  115. // Prerequisite: the queue Q exists and is not empty.
  116. Status DeQueue (SqQueue & Q, ElemType e)
  117. {
  118. /* TODO (#9 #): the output queue is stored in e */
  119. If (Q. front = Q. rear)
  120. Return ERROR;
  121. // E = * (Q. base + Q. front );
  122. E = Q. base [Q. front];
  123. Q. front = (Q. front + 1) % MAXQSIZE;
  124. Return OK; // return the operation status (success: OK, failure: ERROR)
  125. //....................................
  126. }
  127.  
  128. //////////////////////////////////////// //
  129.  
  130.  
  131. // TODO: Use the QueueView function after defining the SqQueue type
  132. /***** // TODO: delete this row to use QueueView ()
  133. # Include <stdio. h>
  134. // View the queue status (for debugging)
  135. Void QueueView (SqQueue Q)
  136. {
  137. Extern void PrintElem (ElemType e); // used to print data
  138. Int I = 0;
  139. If (Q. front <0 | Q. front> = MAXQSIZE | Q. rear <0 | Q. rear> = MAXQSIZE ){
  140. Printf ("queue not initialized \ n ");
  141. Return;
  142. }
  143. Printf ("--- Queue View --- \ n ");
  144. Printf ("front = % d, rear = % d \ n", Q. front, Q. rear );
  145. If (Q. rear> = Q. front ){
  146. Printf ("... \ n ");
  147. For (I = Q. front; I <Q. rear; I ++ ){
  148. Printf ("% 5d \ t", I );
  149. PrintElem (Q. base [I]);
  150. Printf ("\ n ");
  151. }
  152. If (I <MAXQSIZE) printf ("...... \ n ");
  153. } Else {
  154. For (I = 0; I <Q. rear; I ++ ){
  155. Printf ("% 5d \ t", I );
  156. PrintElem (Q. base [I]);
  157. Printf ("\ n ");
  158. }
  159. Printf ("... \ n ");
  160. For (I = Q. front; I <MAXQSIZE; I ++ ){
  161. Printf ("% 5d \ t", I );
  162. PrintElem (Q. base [I]);
  163. Printf ("\ n ");
  164. }
  165. }
  166. Printf ("--- view end --- \ n ");
  167. }
  168. * ***** // TODO: delete this row to use QueueView ()
  169.  
  170. // Cancel the default definition of ElemType to avoid affecting other parts.
  171. # Ifdef USE_DEFAULT_ELEMTYPE
  172. # Undef ElemType
  173. # Undef USE_EFAULT_ELEMTYPE
  174. # Endif
  175.  
  176. # Endif // SQQUEUE_H_INCLUDED
  
  
  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Include "sqqueue. h"
  4.  
  5. // Initialize the system
  6.  
  7.  
  8. Void Finalize (SqQueue & q );
  9.  
  10. //////////////////////////////////////// ////
  11. // Main program
  12. Int main ()
  13. {
  14. SqQueue q; // cyclic queue
  15. Int x;
  16. // System initialization
  17. InitQueue (q );
  18. Printf ("data elements in the queue, ending with 0 ");
  19. Scanf ("% d", & x );
  20. While (x! = 0 ){
  21. EnQueue (q, x );
  22. Scanf ("% d", & x );
  23. }
  24. Printf ("\ n number of queue elements ");
  25.  
  26. Printf ("% d", QueueLength (q ));
  27.  
  28.  
  29. Printf ("\ nheader element :");
  30. If (! QueueEmpty (q )){
  31. If (GetHead (q, x) = OK)
  32. Printf ("% d", x );
  33. }
  34.  
  35.  
  36. Printf ("\ n outgoing queue, first-in-first-out ");
  37. If (DeQueue (q, x) = OK)
  38. Printf ("% d", x );
  39. Printf ("\ n :");
  40. If (! QueueEmpty (q )){
  41. If (GetHead (q, x) = OK)
  42. Printf ("% d \ n", x );
  43. }
  44. }

650) this. width = 650; "src =" http://img1.51cto.com/attachment/201304/222911580.jpg "border =" 0 "alt =" "/>

This article from the "Zhao Yuqiang blog" blog, please be sure to keep this source http://zhaoyuqiang.blog.51cto.com/6328846/1179584

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.