Home > Back-end >  Using circular queue simulation cache, each 5 sets of data cache one-time readout
Using circular queue simulation cache, each 5 sets of data cache one-time readout

Time:10-26

Question:
1. The main function in the do - while loop exit loop judgment had been input very troublesome
( think continuous input data, simulation cache unceasingly, until they want to stop judging from the keyboard input something out again),
2. InQueue function in two if judgment how changes can omit a,

If it is no good to think program change (bosses, forgive me, hope I can talk about realizing the function of cache this idea -
 
//2019213020
# include & lt; stdio.h>
# include & lt; Stdlib. H>
6//# define MAX_SIZE each cache up to five sets of data

Typedef struct
{
Int the queue [MAX_SIZE];
int rear;
int front;
} the QUEUE, * pQUEUE;

Void InitQueue (pQUEUE p);
Void InQueue pQUEUE (p);
Void OutQueue (pQUEUE p);

Int main ()
{
The QUEUE myQueue;
Char choose;
InitQueue (& amp; MyQueue);
Do {
InQueue (& amp; MyQueue);
Printf (" whether to continue (Y/N) \ N ");
The scanf (" % c ", & amp; Choose);//every time feel so enter judgment is very trouble, want can continuous input data until the stop program in the input
} while (choose=='Y' | | choose=='Y');
return 0;
}

//function function: initialize queue
Void InitQueue (pQUEUE p)
{
P - & gt; The front=0;
P - & gt; Rear=0;
}

//function function: implement caching, speaking, reading and writing function
Void InQueue (pQUEUE p)
{
//a:
If ((p - & gt; Rear MAX_SIZE + 1) %==p - & gt; Front)
{
Printf (" overflow \ n ");
OutQueue (p);
}
Printf (" Input x: \ n ");
The scanf (" % d ", & amp; P - & gt; The queue [p - & gt; rear]);
P - & gt; Rear=p - & gt; Rear MAX_SIZE % + 1;

//2:
If ((p - & gt; Rear MAX_SIZE + 1) %==p - & gt; Front)//team with the disposable output data, and clear the queue
{
Printf (" overflow \ n ");
OutQueue (p);
}
return ;
}

//function function:
cache the output functionsVoid OutQueue (pQUEUE p)
{
If (p - & gt; The front==p - & gt; Rear)//empty exit is
{
Printf (" the underflow! \n");
The exit (1);
}
While (p - & gt; The front!=p - & gt; Rear)
{
Printf (" % d \ n ", p - & gt; The queue [p - & gt; front]);
P - & gt; The front=p - & gt; The front MAX_SIZE % + 1;
}
return ;
}
  • Related