# 经典的数据结构——循环队列

 front = ( front + 1) % MAX;
 rear = ( rear + 1) % MAX;

#include <QCoreApplication>
#include <iostream>

using namespace std;

#include <stdio.h>
#include <stdlib.h>

#include <stdio.h>

#define MAX 5

/*Declaration of circular queue.*/
typedef struct
{
int front   ;
int rear    ;
int count   ;
int ele[MAX]    ;
}CirQueue;

/*Initailization of circular queue.*/
void initCirQueue(CirQueue * q)
{
q->front =  0;
q->rear  = -1;
q->count =  0;
}

/*Check Queue is full or not*/
int isFull(CirQueue * q)
{
int full=0;

if(q->count == MAX)
full = 1;

return full;
}

/*Check Queue is empty or not*/
int isEmpty(CirQueue * q)
{
int empty=0;

if(q->count == 0)
empty = 1;

return empty;
}

/*To insert item into circular queue.*/
void insertCirQueue(CirQueue * q, int item)
{
if( isFull(q) )
{
printf("\nQueue Overflow");
return;
}

q->rear = (q->rear+1)%MAX;
q->ele[q->rear] = item;

q->count++;

printf("\nInserted item : %d",item);
}

/*To delete item from queue.*/
int deleteCirQueue(CirQueue * q, int *item)
{
if( isEmpty(q) )
{
printf("\nQueue Underflow");
return -1;
}

*item    = q->ele[q->front];

q->front = (q->front+1)%MAX;

q->count--;

return 0;
}

int main()
{
int item=0;
CirQueue q;

initCirQueue(&q);

insertCirQueue(&q, 10);
insertCirQueue(&q, 20);
insertCirQueue(&q, 30);
insertCirQueue(&q, 40);
insertCirQueue(&q, 50);
insertCirQueue(&q, 60);

if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);

if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);

if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);

if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);

if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);

insertCirQueue(&q, 60);

if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);

if ( deleteCirQueue( &q, &item ) == 0 )
printf("\nDeleted item is : %d",item);

printf("\n");
return 0;
}

-----------------

• 本文已收录于以下专栏：

举报原因： 您举报文章：经典的数据结构——循环队列 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)