Home > Back-end >  Data structure -- about queue is used to implement form such as a b @ b # character sequence test, p
Data structure -- about queue is used to implement form such as a b @ b # character sequence test, p

Time:12-22

 # include & lt; stdio.h> 
#include
# define MAXQSIZE 100
# define the ERROR 0
# define OK 1
Typedef struct {
Char * base;
Int the front;
Int rear;
} SqQueue;

Void InitQueue (SqQueue * Q)//build order queue
{
Q - & gt; The base=(char *) malloc (MAXQSIZE * sizeof (char));
if(! Q - & gt; Base) exit (1);
Q - & gt; The front=Q - & gt; Rear=0;
}

Int the EnQueue (SqQueue * Q, char e)
{
If ((Q - & gt; Rear MAXQSIZE==+ 1) % Q - & gt; Front) return the ERROR;//queue full condition
Q - & gt; [Q - base & gt; rear]=e;
Q - & gt; Rear=(Q - & gt; Rear + 1) % MAXQSIZE;
Return OK;
}

Int to DeQueue (SqQueue * Q, char * e)//head remove an element from the queue
{
If (Q - & gt; The front==Q - & gt; Rear) return the ERROR;//the queue empty condition
* e=Q - & gt; The base/Q - & gt; front;
Q - & gt; The front=(Q - & gt; Front + 1) % MAXQSIZE;
Return OK;
}

Int DeQueue_rear (SqQueue * Q, char * e)//remove an element from a queue
{
If (Q - & gt; The front==Q - & gt; Rear) return the ERROR;//the queue empty
* e=Q - & gt; Base [Q - & gt; rear];
Q - & gt; Rear=(Q - & gt; Rear - 1) % MAXQSIZE;
Return OK;
}

Int GetHead (SqQueue Q, char * e)
{
If (Q.f ront==Q.r ear) return the ERROR;//the queue empty
* e=Q.b ase [Q.f ront];
Return OK;
}

Int QueueLength (SqQueue * Q)
{
Return (Q - & gt; Rear - Q - & gt; Front + MAXQSIZE) % MAXQSIZE;
}

Int CheckSymmetry (SqQueue * Q, char ch [])
{
InitQueue (Q);
int i=0;
Char * e * c;
While (ch [I]! )='@' character into the queue before//@
{
The EnQueue (Q, ch [I]);
i++;
}
i++;
While (ch [I]! After='#')//@ # character into the queue before
{
The EnQueue (Q, ch [I]);
i++;
}
While (QueueLength (Q)!=0)
{
if(! DeQueue (Q, c)) return the ERROR;//take team header element
if(! DeQueue_rear (Q, e)) return the ERROR;//take of the element
If (* c!=* e) return 0;
}
If (Q - & gt; The front==Q - & gt; Rear) return 1;//the queue is empty, symmetrical success
}

Int main ()
{
SqQueue Q;
//Q=(SqQueue *) malloc sizeof (Q) (*).
//if(! Q) exit (1);
int i=0;
Char ch [100]={} '0', c;
Do {
Printf (" please input a character (like @ a + b + b # sequence) : \ n ");
The scanf (" % c ", & amp; C);
getchar();
Ch [I]=c;
i++;
} while (c!='#');
If (CheckSymmetry (& amp; Q, ch)) printf (" is central symmetry sequence \ n ");
The else printf (" not symmetrical center sequence \ n ");
}



This is the problem screenshot, if can't judge, don't know where the code wrong??
Trouble says about it, my program for a run half fixed the problem, don't know if common problems
Thank you very much!!!!!!

CodePudding user response:

 # include & lt; stdio.h> 
#include

# define MAXQSIZE 100
# define the ERROR 0
# define OK 1

Typedef struct {
Char * base;
Int the front;
Int rear;
} SqQueue;

Void InitQueue (SqQueue * Q)//build order queue
{
Q - & gt; The base=(char *) malloc (MAXQSIZE * sizeof (char));
if(! Q - & gt; Base)
exit(1);
Q - & gt; The front=Q - & gt; Rear=0;
}

Int the EnQueue (SqQueue * Q, char e)
{
If ((Q - & gt; Rear MAXQSIZE==+ 1) % Q - & gt; Front) return the ERROR;//queue full condition
Q - & gt; [Q - base & gt; rear]=e;
Q - & gt; Rear=(Q - & gt; Rear + 1) % MAXQSIZE;
Return OK;
}

Int to DeQueue (SqQueue * Q, char * e)//head remove an element from the queue
{
If (Q - & gt; The front==Q - & gt; Rear)
Return the ERROR;//the queue empty condition
* e=Q - & gt; The base/Q - & gt; front;
Q - & gt; The front=(Q - & gt; Front + 1) % MAXQSIZE;
Return OK;
}

Int DeQueue_rear (SqQueue * Q, char * e)//remove an element from a queue
{
If (Q - & gt; The front==Q - & gt; Rear)
Return the ERROR;//the queue empty
//* e=Q - & gt; Base [Q - & gt; rear];
* e=Q - & gt; [Q - base & gt; rear - 1].
Q - & gt; Rear=(Q - & gt; Rear - 1) % MAXQSIZE;

Return OK;
}

Int GetHead (SqQueue Q, char * e)
{
If (Q.f ront==Q.r ear)
Return the ERROR;//the queue empty
* e=Q.b ase [Q.f ront];
Return OK;
}

Int QueueLength (SqQueue * Q)
{
Return (Q - & gt; Rear - Q - & gt; Front + MAXQSIZE) % MAXQSIZE;
}

Int CheckSymmetry (SqQueue * Q, char ch [])
{
InitQueue (Q);
int i=0;
//char * e * c;
Char e, c;
While (ch [I]! )='@' character into the queue before//@
{
The EnQueue (Q, ch [I]);
i++;
}
i++;
While (ch [I]! After='#')//@ # character into the queue before
{
The EnQueue (Q, ch [I]);
i++;
}
While (QueueLength (Q)!=0)
{
if(! DeQueue (Q, & amp; C))
Return the ERROR;//take team header element
if(! DeQueue_rear (Q, & amp; E))
Return the ERROR;//take of the element
Printf (" % % c: \ n "c, c, e);
If (c!=e)
return 0;
}
If (Q - & gt; The front==Q - & gt; Rear)
return 1;//the queue is empty, symmetrical success

return 0;
}

Int main (void)
{
SqQueue Q;
//Q=(SqQueue *) malloc sizeof (Q) (*).
//if(! Q) exit (1);
int i=0;
Char ch [100]={} '0', c;
Do {
Printf (" please input a character (like @ a + b + b # sequence) : \ n ");
The scanf (" % c ", & amp; C);
getchar();
Ch [I]=c;
i++;
} while (c!='#');
If (CheckSymmetry (& amp; Q, ch))
Printf (" is central symmetry sequence \ n ");
The else
Printf (" not symmetrical center sequence \ n ");
}

For your reference ~
  • Related