Home > Back-end >  Yang hui triangle queue
Yang hui triangle queue

Time:10-28

#include
#include
# define MAXSIZE 50
//the definition of circular queue
Typedef int queueelementype;
Typedef struct
{
Queueelementype element [MAXSIZE];//queue element space
Int the front;//head pointer
Int rear;//the tail pointer
} seqqueue;
//initialization circular queue
Void initqueue (seqqueue * q)
{//will * q initialized to a new cycle list
Q - & gt; The front=q - & gt; Rear=0;
}
//determine whether empty
Int isempty (seqqueue * q)
{
If (q - & gt; The front==q - & gt; Rear)
return 0;
The else
return 1;
}
//read queue element
Int gethead (seqqueue * q, queueelementype * x)
{
If (q - & gt; The front==q - & gt; Rear)
return 0;
The else
{
* x=q - & gt; Element [q - & gt; front];
return 1;
}
}
//circular queue team
Int enterqueue (seqqueue * q, queueelementype x)
{
//the element x team
If ((q - & gt; Rear MAXSIZE==+ 1) % q - & gt; Front)//the tail pointer, pointer plus 1 after above marks the queue is full
return 0;
The else
{
Q - & gt; Element [q - & gt; rear]=x;
Q - & gt; Rear=(q - & gt; Rear + 1) % MAXSIZE;//reset queue Settings of the pointer
}
return 1;
}
//circular queue out team
Int deletequeue (seqqueue * q, queueelementype * x)
{
//delete the team marched header element, use x returns its value
If (q - & gt; The front==q - & gt; Rear)//the queue is empty
return 0;
The else
{
* x=q - & gt; Element [q - & gt; front];
Q - & gt; The front=(q - & gt; Front + 1) % MAXSIZE;//reset the team head pointer
return 1;//operation success
}
}
//print Yang hui triangle
//print Yang hui triangle algorithm first n rows element
Void yanghuitriangle ()
{
Seqqueue q;
Int n, I, temp, x, n=0;
Initqueue (& amp; Q);
Enterqueue (& amp; Q, 1);
The scanf (" % d ", & amp; N);//first line element team
For (n=2; n<=N; N++)//n line element and ers, and print n - 1 the element
{
Enterqueue (& amp; Q, 1);/* * n lines of the first yuan 10-year team/
for(i=1; i<=n - 2; I++)//to use elements to produce line n - 1 n the middle n - 2 elements into team
{
Deletequeue (& amp; Q, & amp; Temp);
Printf (" % d \ n ", temp);/* the element */print n - 1
Gethead (& amp; Q, & amp; X);
Temp=temp + x;//use the team line elements generate n n - 1 */
elementEnterqueue (& amp; Q, temp);
printf("\n");
}

Deletequeue (& amp; Q, & amp; X);
Printf (" % d \ n ", x);/* to print the first lines of the last element */n - 1
Enterqueue (& amp; Q, 1);/* * n lines of the last yuan 10-year team/

}
printf("\n");
while(! Isempty (& amp; Q))/* */print the last line element
{
Deletequeue (& amp; Q, & amp; X);
Printf (" % d \ n ", x);
printf("\n");
}
printf("\n");

}
Void main ()
{
Yanghuitriangle ();
}



Where is the wrong with

CodePudding user response:

Suggest you learn the debug
In addition I just look at the

Function does not allocate memory have been carried out to q or allocated memory why directly during the callback function q - & gt; The front=q - & gt; Rear=0; Have you ever thought of q=NULL?
Your program for the most part because this kind of situation lead to segment fault the proposal to the debug

 void yanghuitriangle () 
{
Seqqueue q;
Int n, I, temp, x, n=0;
Initqueue (& amp; Q);
Enterqueue (& amp; Q, 1);

Void initqueue (seqqueue * q)
{//will * q initialized to a new cycle list
Q - & gt; The front=q - & gt; Rear=0;
}


  • Related