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

127人阅读 评论(0)

 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;
}


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

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：64113次
• 积分：1872
• 等级：
• 排名：千里之外
• 原创：124篇
• 转载：15篇
• 译文：0篇
• 评论：11条
评论排行
最新评论