Cyclic queue, cyclic queue pointer
The implementation of the Circular queue is limited in Chinese, but the code written is easy to understand, so I will not explain it more directly.
1: header file: # include "DQueue. h"
# Pragma once # include <stdio. h> # include <stdlib. h ># define ElemType int # define SizeFull 20 typedef struct Queue {ElemType * data; int sizefull; int front; int rear;} Queue; void Show (); // display the menu void InitQueue (Queue * Q); // initialize the Queue void DestoryQueue (Queue * Q); // destroy the Queue void ClearQueue (Queue * Q ); // clear the Queue bool IsFull (Queue * Q); // empty bool IsEmpty (Queue * Q); // judge whether the value is int QueueLength (Queue * Q ); // calculate the Queue length void EnQueue (Queue * Q, ElemType x); // enter the Queue void DeQueue (Queue * Q); // output Queue void ShowQueue (Queue * Q ); // display the queue
2: function implementation DQueue. cpp:
<Pre name = "code" class = "cpp"> # include "DQueue. h "void InitQueue (Queue * Q) {Q-> data = (ElemType *) malloc (sizeof (ElemType) * SizeFull); if (Q-> data = NULL) {printf ("Opening memory failed \ n"); return;} Q-> sizefull = SizeFull; Q-> front = Q-> rear = 0;} void Show () {printf ("************************************ * *********** \ n "); printf ("* [1]: Inbound stack [2]: Outbound stack * \ n"); printf ("* [3]: Stack length [4]: destroy stack * \ n "); printf (" * [5]: Clear stack [6]: Display stack * \ n "); printf (" * [0]: stack rollback * \ n "); Printf ("************************************* * ********** \ n "); printf ("select:");} void DestoryQueue (Queue * Q) {free (Q-> data ); q-> front = Q-> rear = Q-> sizefull = 0;} void ClearQueue (Queue * Q) {Q-> front = 0; q-> rear = 0;} bool IsFull (Queue * Q) {return (Q-> rear + 1) % Q-> sizefull = Q-> front ;} bool IsEmpty (Queue * Q) {return Q-> rear % Q-> sizefull = Q-> front;} int QueueLength (Queue * Q) {return Q-> rear % Q-> sizefull;} void EnQueue (Queue * Q, ElemType x) {if (IsFull (Q) {printf ("the space is full \ n"); return ;} q-> data [Q-> rear] = x; Q-> rear = (Q-> rear + 1) % Q-> sizefull;} void DeQueue (Queue * Q) {if (IsEmpty (Q) {printf ("the queue is empty and \ n" cannot be deleted); return;} Q-> front = (Q-> front + 1) % Q-> sizefull;} void ShowQueue (Queue * Q) {for (int I = Q-> front; I! = Q-> rear; I = (I + 1) % Q-> sizefull) {printf ("% d", Q-> data [I]);} printf ("\ n ");}
3: main function main. cpp
<Pre name = "code" class = "cpp"> # include "DQueue. h "void main () {Queue Q; InitQueue (& Q); ElemType x; int Length; int pos = 1; while (pos) {Show (); scanf ("% d", & pos); switch (pos) {case 1: printf ("Enter data, end with-1 :"); while (scanf ("% d", & x), x! =-1) {EnQueue (& Q, x);} break; case 2: DeQueue (& Q); break; case 3: Length = QueueLength (& Q ); printf ("Length = % d \ n", Length); break; case 4: DestoryQueue (& Q); break; case 5: ClearQueue (& Q); break; case 6: ShowQueue (& Q); break; default: break ;}}}