#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define MAXSIZE 6
/*
The loop queue is implemented in an array, and traditional array resolution creates a waste of memory. The improved version is both an array of loops to solve.
*/
typedef struct QUEUE
{
int *pbase;
int front;
int rear;
}queue;
void DeQueue (Queue *,int *val);
void EnQueue (Queue *,int);
void Initqueue (Queue *);
BOOL Queuefull (Queue *);
BOOL Queueempty (Queue *);
void Main ()
{
Queue queue;
Initialize Queue
Initqueue (&queue);
Team
EnQueue (&queue,2);
EnQueue (&queue,3);
EnQueue (&queue,5);
EnQueue (&queue,4);
Out Team
int Val;
DeQueue (&queue,&val);
DeQueue (&queue,&val);
DeQueue (&queue,&val);
DeQueue (&queue,&val);
}
void Initqueue (Queue *queue)
{
The header coordinates point to the first element, and the coordinates point to the next one of the current element.
queue->front=0;
queue->rear=0;
queue->pbase= (int *) malloc (sizeof (int) *6);
if (queue->pbase==null)
{
printf ("Memory allocation failed");
Exit (-1);
}
}
void EnQueue (Queue *queue,int val)
{
if (Queuefull (queue))
{
printf ("The current queue is full and can no longer be queued");
Return
}
queue->pbase[queue->rear]=val;
Queue->rear= (queue->rear+1)%maxsize;//assumes that starting with array 5, after it is finished, it can also start at 0.
}
void DeQueue (Queue *queue,int *val)
{
if (Queueempty (queue))
{
printf ("The current queue is empty and can no longer be out of the team");
Return
}
*val=queue->pbase[queue->front];
Queue->front= (queue->front+1)%maxsize;//assumes that starting with array 5, after it is finished, it can also start at 0.
printf ("%d", *val);
printf ("\ n");
}
BOOL Queuefull (Queue *queue)
{
if ((queue->rear+1)%maxsize==queue->front)
return true;
Else
return false;
}
BOOL Queueempty (Queue *queue)
{
if (Queue->rear==queue->front)
return true;
Else
return false;
}
An array of loops if rear==front, can't tell if it's empty or full?
* * What should I do? Let a space do not have thick elements, the space is 6 of the array, when full, can only save 5.
There is another problem, the loop array, how to determine whether it is full?
Loop array How do we get them from 6 down to 0, the way there are numbers, 0,1,2,3,4,5,6,7,8,8,10,11,10,11, can you think of that?
0,1,2,3,4,5,6 after the first count, in the re-0,1,2,3,4,5, no, no, just count it over. Re-count, is not the operation of redundancy.
How to judge the situation is full, or count, there is a no, as long as the current tail coordinates to ensure that the next position and the head coordinates is a position, it is not possible!
This article from the "Jane Answers Life" blog, reproduced please contact the author!
 Loop queue implementation of linear structure