Home > Back-end >  OJ queue problem solving!!!!!!
OJ queue problem solving!!!!!!

Time:09-25

Oneself write operation and output the same but OJ has been not to please everyone a great god to help us see what is wrong with



Here are several commands and the meaning of the queue:

INIT initialization queue
The PRINT output one line: from the beginning to the end output queue each element, element are separated with a space
ENTER the value value into the team
OUT if the queue is not empty, the team first element OUT of the team, if the queue is empty, do not do any action,
LENGTH the number of elements in the output current queue

Queue is the character of each element,

Input
The test cases of the first line is an integer n (0 & lt; N & lt; 10000), said the test case of how many command,

Followed by n lines, each line is a command, sequentially from beginning to end each command,

The Output
PRINT and LENGTH of them command, output the corresponding information,

The Sample Input
11.
INIT
ENTER E
ENTER 5
ENTER t
LENGTH
ENTER 8
ENTER A
PRINT the
OUT
LENGTH
PRINT the

The Sample Output
3
E 5 t 8 A
4
8 A 5 t







Here is my code:
#include
#include
# include
# include
# include
# include
using namespace std;
# define OK 1
# define the ERROR 0
# define OVERFLOW - 2

Typedef char QElemType;
Typedef struct
{
QElemType * base;//storage base address
Int the front;//the head pointer
Int rear;//the tail pointer

} SqQueue;

Int INIT (SqQueue & amp; Q)
{
Q.b ase=(QElemType *) malloc (10000 * sizeof (QElemType));
if(! Q.b ase) exit (0);
Q.f ront=Q.r ear=0;
Return OK;
}


Int PRINT (SqQueue & amp; Q)
{
For (int I=Q.f ront; i{
Printf (" % c ", Q.b ase [I]);
}
printf("\n");

Return OK;
}

Int the ENTER (SqQueue & amp; Q, char value)
{

Q.b ase [Q.r ear]=value;
Q.r ear++;
Return OK;
}

Int OUT (SqQueue & amp; Q, char & amp; Value)
{
If (Q.f ront==Q.r ear) return 0;
value=https://bbs.csdn.net/topics/Q.base [Q.f ront];
Q.f ront=Q.f ront + 1;
Return OK;

}

Int LENGTH (SqQueue & amp; Q)
{
Return Q.r ear - Q.f ront;
}

Int main () {
SqQueue Q;
int i=0;
Char value;
Int choice;
Char s [10];
The scanf (" % d ", & amp; Choice);
While (i{

scanf("%s",s);
If (STRCMP (" INIT ", s)==0) INIT (Q);

Else if (STRCMP (" ENTER ", s)==0)
{
The scanf (" % s ", & amp; Value);
ENTER (Q value);
}

Else if (STRCMP (" PRINT ", s)==0) PRINT (Q);

Else if (STRCMP (" LENGTH ", s)==0) printf (" % d \ n ", LENGTH (Q));

Else if (STRCMP (" OUT ", s)==0) OUT (Q value);



i++;

}
Free (Q.b ase);


return 0; }


CodePudding user response:

Abnormal considering imperfect, you take a queue when a ring to deal with,
Assuming that full of 10000 data, bring up a, into another, your rear++ not cross a line?

So every function to consider more, considering the rear reverse!

CodePudding user response:

reference 1/f, arfi response:
abnormal considering imperfect, do you want your queue when a ring to deal with,
Assuming that full of 10000 data, bring up a, into another, your rear++ not cross a line?

So every function to consider more, considering the rear reverse!

What do I have to that solution is more appropriate, I to this respect is just learning, thank you for your answer,

CodePudding user response:

1. The best record in the queue number of elements, to prevent the front '/rear overlap after don't know is an empty queue or full queue
2. The function of the front/rear after + + for more than 10000 o
3. Print function considering less than front rear scenario (two section of the print or the same for more than 10000 o, just loop condition to write clear, best written number from 0 to the queue,

The above changes mainly is to add a more, in order to realize the "ring" queue operations

CodePudding user response:

Ok thank you very much

reference 5 floor arfi reply:
1. It is best to record the number of element in the queue, prevent front '/rear overlap after don't know is an empty queue or full queue
2. The function of the front/rear after + + for more than 10000 o
3. Print function considering less than front rear scenario (two section of the print or the same for more than 10000 o, just loop condition to write clear, best written number from 0 to the queue,

The above changes mainly is to add a more, in order to realize the "circular" queue operations

CodePudding user response:

reference 5 floor arfi reply:
1. It is best to record the number of element in the queue, prevent front '/rear overlap after don't know is an empty queue or full queue
2. The function of the front/rear after + + for more than 10000 o
3. Print function considering less than front rear scenario (two section of the print or the same for more than 10000 o, just loop condition to write clear, best written number from 0 to the queue,

The above changes mainly is to add a more, in order to realize the "circular" queue operations


Can you help me take a look at the code I looked at your suggestion to modify the OJ still not to pass is really don't know what to do

#include
#include
# include
# include
# define OK 1
# define the ERROR 0
# define OVERFLOW - 2
# define MAXSIZE 10020

Typedef char QElemType;
Typedef struct
{
QElemType * base;//storage base address
Int the front;//the head pointer
Int rear;//the tail pointer

} SqQueue;

Int INIT (SqQueue & amp; Q)
{
Q.b ase=new QElemType [MAXSIZE];
if(! Q.b ase) exit (OVERFLOW);
Q.f ront=Q.r ear=0;
Return OK;
}


Int PRINT (SqQueue & amp; Q)
{

If (Q.f ront> %=Q.r ear MAXSIZE) return the ERROR;

The else {
For (int I=Q.f ront; i{
Printf (" % c ", Q.b ase [I]);
}
printf("\n");

Return OK; }
}

Int the ENTER (SqQueue & amp; Q, char value)
{
If ((Q.r ear + 1) % MAXSIZE==Q.f ront) return the ERROR;
Q.b ase [Q.r ear]=value;
Q.r ear=(Q.r ear + 1) % MAXSIZE;
Return OK;
}

Int OUT (SqQueue & amp; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related