C Tutorial/Data Structure/Circular Queue

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Circular queues

#include <stdio.h>
#include <stdlib.h>
#define MAX 10

void insert(int queue[], int *rear, int front, int value)
{
   *rear= (*rear +1) % MAX;
   if(*rear == front)
   {
      printf("The queue is full can not insert a value\n");
      exit(0);
   }
   queue[*rear] = value;
}
void delete(int queue[], int *front, int rear, int * value)
{
   if(*front == rear)
   {
      printf("The queue is empty can not delete a value\n");
      exit(0);
   }
   *front = (*front + 1) % MAX;
   *value = queue[*front];
}
void main()
{
   int queue[MAX];
   int front,rear;
   int n,value;
   front=0; rear=0;
   insert(queue,&rear,front,1);
   insert(queue,&rear,front,2);
   insert(queue,&rear,front,3);
   insert(queue,&rear,front,4);
  
   delete(queue,&front,rear,&value);
   printf("The value deleted is %d\n",value);
   delete(queue,&front,rear,&value);
   printf("The value deleted is %d\n",value);

   delete(queue,&front,rear,&value);
   printf("The value deleted is %d\n",value);
}
The value deleted is 1
The value deleted is 2
The value deleted is 3