循环队列

#include<iostream>

#include<cstdlib>

#include<ctime>

using namespace std ;

#define MAXSIZE 100

#define  OK true

#define  ERROR false

#define  LEN 5

typedef bool Status ;

typedef int QElemType ;

typedef struct

{

 QElemType data[MAXSIZE];

 int front ;

 int rear ;

}SqQueue;

Status initQueue(SqQueue * Q)

{

 Q->front = Q->rear = 0 ;

 return OK ;

}

int getQLength(SqQueue Q)

{

  return (Q.rear - Q.front + MAXSIZE) % MAXSIZE ;

}

Status enQueue(SqQueue* Q , QElemType e )

{

 if((Q->rear+1)%MAXSIZE == Q->front)

  return ERROR ;

 Q->data[Q->rear] = e ;

 Q->rear = (Q->rear+1+MAXSIZE)%MAXSIZE;

 return OK ;

}

Status deQueue(SqQueue* Q , QElemType* e )

{

  if(Q->front == Q->rear)

   return ERROR ;

  *e = Q->data[Q->front] ;

  Q->front = (Q->front + 1 + MAXSIZE) % MAXSIZE ;

  return OK ;

}

void printQ(SqQueue Q)

{

 cout<<"打印:"<<endl;

 int pfront = Q.front ;

 while( pfront != Q.rear)

 {

  cout<<Q.data[pfront]<<" ";

  pfront = (pfront + 1 + MAXSIZE) % MAXSIZE ;

 }

 cout<<endl;

}

int main()

{

    SqQueue Q ;

    initQueue(&Q);

 QElemType e ;

 srand((unsigned)time(0));

 for(int i = 0 ; i < LEN ;i ++)

 {

  e = rand() % 10 + 1 ;

  enQueue(&Q,e);

 }

    printQ(Q);

 cout<<"删除一个元素";

 deQueue(&Q,&e);

 cout<<" "<<e<<" 后,";

 printQ(Q);

 system("pause");

 return 0 ;

}

posted @ 2013-05-29 15:19  dot dot 小点点丶  阅读(122)  评论(0编辑  收藏  举报