分类: C/C++
2008-10-23 16:15:57
循环队列的算法 #includeusing namespace std; #define NULL 0 #define MAXSIZE 100 typedef int Elemtype; typedef struct { Elemtype *base; int front ; int rear; }SqQueue; void InitQueue(SqQueue *&Q) { Q=(SqQueue *)malloc(sizeof(SqQueue)); Q->base=(Elemtype *)malloc( MAXSIZE *sizeof(Elemtype)); if(!Q->base) { printf("The InitQueue is fail!"); return; } Q->front=Q->rear=0; } void EnQueue(SqQueue *Q,Elemtype e) { if( (Q->rear+1)%MAXSIZE==Q->front ) { printf("\nThe EnQueue is fail!"); return; } Q->base[Q->rear]=e; Q->rear=(Q->rear+1)%MAXSIZE; } void DeQueue(SqQueue *Q,Elemtype *e) { if(Q->rear==Q->front) { printf("\nThe DeQueue is fail,the Queue is empty!"); return; } *e=Q->base[Q->front]; Q->front=(Q->front+1)%MAXSIZE; } int main() { SqQueue *que; Elemtype num,s; que=NULL; InitQueue(que); scanf("%d",&num); while(num!=-1) { EnQueue(que,num); scanf("%d",&num); } cin.get(); printf("\nThe result of DeQueue is :\n"); while (que->front!=que->rear) { DeQueue(que,&s); cout< } return 0; } |