I. Brief description
How to handle an empty queue: 1. Set another flag to distinguish whether the queue is empty or full; 2. Use less than one element space, as a sign that the queue header pointer is in the next position on the tail of the team as the queue is full.
Two. header files
1 //3_4_part1.h2 /**3 Author:zhaoyu4 Email:[email protected]5 date:2016-6-96 note:realize My textbook << data structure (C-language version) >>7 */8 //Page9#include <cstdio>Ten#include"head.h" One #defineQelemtype int A //----Loop queue: The sequential storage structure of queues---- - #defineMaxqsize 10//Maximum Queue Length -typedefstruct{ theQelemtype *Base; - intFront; - intRear; - }sqqueue; + //the basic Operation description and implementation----of----cyclic queue -Status Initqueue (Sqqueue &Q) + { A //constructs an empty queue Q atQ.Base= (Qelemtype *)malloc(maxqsize*sizeof(Qelemtype)); - if(! Q.Base) - { - exit (OVERFLOW); - } -Q.front = Q.rear =0; in returnOK; - } to intqueuelength (sqqueue Q) + { - //returns the number of elements of Q, that is, the length of the queue the return(Q.rear-q.front + maxqsize)%maxqsize; * } $Status EnQueue (Sqqueue &Q, Qelemtype e)Panax Notoginseng { - //Insert Element E for Q's new team tail element the if((q.rear+1)%maxqsize = =Q.front) + { A returnERROR;//Queue Full the } +Q.Base[Q.rear] =e; -Q.rear = (q.rear+1)%maxqsize; $ returnOK; $ } -Status DeQueue (Sqqueue &q, Qelemtype &e) - { the //if the queue is not empty, the queue header element of Q is deleted, and the value is returned with E . - //and returns OK, otherwise returns an ERRORWuyi if(Q.front = =q.rear) the { - returnERROR; Wu } -E = Q.Base[Q.front]; AboutQ.front = (q.front+1)%maxqsize; $ returnOK; - } - voidPrintQueue (sqqueue Q) - { A intCNT =Q.front; + if(Q.front = =q.rear) the { -printf"void\n"); $ return; the } the while((cnt+1)%maxqsize! =q.rear) the { the //printf ("%d\t%d\n", q.base[cnt++], CNT); the output is so strange. -printf"%d\t"Q.Base[CNT]); incnt++; the } theprintf"%d\n"Q.Base[CNT]); About}
3_4_part2.h
Three. cpp file
1#include"3_4_part2.h"2 intMainintargcChar Const*argv[])3 {4 sqqueue Q;5 Initqueue (Q);6 for(inti =1; I <Ten; ++i)7 {8EnQueue (Q, i*Ten);9 }Ten PrintQueue (Q); One inte; AEnQueue (Q, -); - PrintQueue (Q); - DeQueue (Q, e); theprintf"%d\n", e); -EnQueue (Q, -); - PrintQueue (Q); - return 0; +}
3_4_part2.cpp
Four. Testing
Five. Other
A strange error was found during debugging, and later found to be different compilers, different interpretations of when the self-increment operation (++/--) was performed as a parameter in a printf statement.
The following code is different in the result of compilation execution in VS and execution with GCC compilation.
1#include <stdio.h>2 intMainintargcChar Const*argv[])3 {4 intCNT =0, cnt_1 =0, cnt_2 =0, Cnt_3 =0, Cnt_4 =0;5 inta[Ten] = {1,2,3,4,5,6,7,8,9};6 for(inti =1; I <=4; i++)7 {8printf"%d-", CNT);9printf"%d\t", a[cnt++]);Ten } Oneprintf"\ n"); A for(inti =1; I <=4; i++) - { -printf"%d-%d\t", a[cnt_1++], cnt_1); the } -printf"\ n"); - for(inti =1; I <=4; i++) - { +printf"%d-%d\t", a[++cnt_2], cnt_2); - } +printf"\ n"); A for(inti =1; I <=4; i++) at { -printf"%d-%d\t", Cnt_3, a[cnt_3++]); - } -printf"\ n"); - for(inti =1; I <=4; i++) - { inprintf"%d-%d\t", Cnt_4, a[++Cnt_4]); - } toprintf"\ n"); + intCnt_5 =1, Cnt_6 =1, cnt_7 =1; -printf"%d-%d\n", Cnt_5,5* (cnt_5++)); the return 0; *}
tese.c
VS2015 Compiling execution results
GCC Compilation Execution Results
Data structure algorithm C language implementation (12)---3.4 sequential representation and implementation of circular queue & queue