Home > Back-end >  For help, run on oj has been the Runtime Error, run on VS. No problem
For help, run on oj has been the Runtime Error, run on VS. No problem

Time:09-25

Create a chain queue, able to complete the queue initialization, team, a team, the team first and of the element, destruction of queue operations, such as

(queue queue structure including head nodes) are defined as follows:

Typedef int Elemtype;

Typedef struct Node//queue element

{

Elemtype data;//data domain

struct Node *next;//points to the next element

} QNode;

Typedef struct//queue

{

QNode * front;//queue head node

QNode * tail;//points to the line element

} LinkQueue;

Part of the code has been given, please complete and submit the do not contain has given the code,

C + +
Void InitQueue (LinkQueue & amp; Q)
{
Q.f ront=(QNode *) malloc (sizeof (QNode));
Q.f ront - & gt; Next=NULL;
Q.t ail=q.f ront;
}

Void Destroy (LinkQueue & amp; Q)
{
* p=QNode q.f ront, * t;
While (p)
{
T=p;
P=p - & gt; next;
Free (t);
}
}

Int main ()
{
LinkQueue q;
InitQueue (q);
Char CMD [10].
Int x, res;
While (the scanf (" % s ", CMD)!=(EOF)
{
If (STRCMP (CMD, "enq")==0)
{
The scanf (" % d ", & amp; X);
The EnQueue (q, x);
}
Else if (STRCMP (CMD, "front")==0)
{
If (Empty (q))
Printf (" the EMPTY \ n ");
The else
Printf (" % d \ n ", the Front (q));
}
Else if (STRCMP (CMD, "back")==0)
{
If (Empty (q))
Printf (" the EMPTY \ n ");
The else
Printf (" % d \ n ", the Back (q));
}
The else
{
If (Empty (q))
Printf (" the EMPTY \ n ");
The else
Printf (" % d \ n ", DeQueue (q));
}
}
Destroy (q);
return 0;
}
Enter

The input data consists of the following command:

Enq x (1) : the x team

(2) the deq: the team

(3) the front: get team first element

(4) the back: access of the element

Each command a line, end with EOF,

The total order number can't more than 500,

O

When performing the deq output element out of the squad, when performing the front output team first element, when performing the back when the output of the element, when the team is EMPTY, need to output the EMPTY,

The sample input

Enq 1
Enq 2
The front
Deq
Enq 3
The front
The back


Sample output

1
1
2
3
Here is my code
#include
#include
#include
#include
using namespace std;
Typedef int Elemtype;
Typedef struct Node//queue element
{
Elemtype data;//data domain
Struct Node * next;//points to the next element
} QNode;
Typedef struct//queue
{
QNode * front;//queue head node
QNode * tail;//points to the line element
} LinkQueue;

Bool Empty (const LinkQueue& Q) {
Return (q.t ail==NULL);
}
Void the EnQueue (LinkQueue& Q, int e) {
Static int flag=1;
If (flag) {
Q.f ront - & gt; Data=https://bbs.csdn.net/topics/e;
flag=0;
}
The else {
P=QNode * (QNode *) malloc (sizeof (QNode));
p-> Data=https://bbs.csdn.net/topics/e;
p-> Next=NULL;
If (q.f ront!=NULL)
{
Q.t ail - & gt; Next=p;
Q.t ail=p;
}
The else
Q.f ront=q.t ail=p;
}
}
Elemtype DeQueue (LinkQueue& Q) {
QNode * p;
Int e;
P=q.f ront;
E=p - & gt; The data;
If (q.f ront==q.t ail)
Q.f ront=q.t ail=NULL;
The else
Q.f ront=q.f ront - & gt; next;
Free (p);
Return e;
}
Elemtype Front (const LinkQueue& Q) {
Return q.f ront - & gt; The data;
}
Elemtype Back (const LinkQueue& Q) {
Return q.t ail - & gt; The data;
}
Void InitQueue (LinkQueue& Q)
{
Q.f ront=(QNode *) malloc (sizeof (QNode));
Q.f ront - & gt; Next=NULL;
Q.t ail=q.f ront;
}

Void Destroy (LinkQueue& Q)
{
* p=QNode q.f ront, * t;
While (p)
{
T=p;
P=p - & gt; next;
Free (t);
}
}

Int main ()
{
LinkQueue q;
InitQueue (q);
Char CMD [10].
Int x, res;
While (the scanf (" % s ", CMD)!=(EOF)
{
If (STRCMP (CMD, "enq")==0)
{
The scanf (" % d ", & amp; X);
The EnQueue (q, x);
}
Else if (STRCMP (CMD, "front")==0)
{
If (Empty (q))
Printf (" the EMPTY \ n ");
The else
Printf (" % d \ n ", the Front (q));
}
Else if (STRCMP (CMD, "back")==0)
{
If (Empty (q))
Printf (" the EMPTY \ n ");
The else
Printf (" % d \ n ", the Back (q));
}
The else
{
If (Empty (q))
Printf (" the EMPTY \ n ");
The else
Printf (" % d \ n ", DeQueue (q));
}
}
Destroy (q);
return 0;
}

CodePudding user response:

 # include 
#include
#include
#include

using namespace std;

Typedef int Elemtype;

Typedef struct Node//queue element
{
Elemtype data;//data domain
Struct Node * next;//points to the next element
} QNode;

Typedef struct//queue
{
QNode * front;//queue head node
QNode * tail;//points to the line element
} LinkQueue;

Bool Empty (const LinkQueue& Q)
{
Return (q.t ail==NULL);
}

Void the EnQueue (LinkQueue& Q, int e)
{
/*
Static int flag=1;
If (flag) {
Q.f ront - & gt; Data=https://bbs.csdn.net/topics/e;
flag=0;
return;
}
*/
//else {
P=QNode * (QNode *) malloc (sizeof (QNode));
p-> Data=https://bbs.csdn.net/topics/e;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related