Home > Back-end >  C language queue topics
C language queue topics

Time:09-24

. Write a program to arbitrary input n number less than 100, the odd and even number of them respectively in the chain team for Q1 and Q2, then match the output chain team Q1, Q2 in value, until either the queue is empty,
Input:
20
42 and 45, 64, 90, 85, 39 34 19 83 75 70 99 78 79 88 61 88 80 54
Output:
45 42
38 85
39 64
99, 90,
19 34
83, 78,
75, 70,
79, 88,
61, 88,

# include
# include
Struct node
{
The int data;
Struct node * next;
};
Typedef struct node node;
Typedef struct node * PNODE;

Typedef struct
{
PNODE front and rear;
} Linkqueue;

Void initqueue (Linkqueue * q)
{
Q - & gt; The front=q - & gt; Rear=(PNODE) malloc (sizeof (NODE));
if(! Q - & gt; Front)
exit(0);
Q - & gt; The front - & gt; Next=NULL;
}

Linkqueue insertqueue1 (Linkqueue * q, int n)
{
int i;
PNODE p;
P=(PNODE) malloc (sizeof (NODE));
If (p==NULL)
exit(0);
for(i=0; i{
The scanf (" % d ", & amp; p-> The data);
p-> Next=NULL;
Q - & gt; Rear - & gt; Next=p;
Q - & gt; Rear=p;
}
}

Linkqueue insertqueue2 (Linkqueue * q, int x)
{
int i;
PNODE p;
P=(PNODE) malloc (sizeof (NODE));
If (p==NULL)
exit(0);
X=p - & gt; The data;
p-> Next=NULL;
Q - & gt; Rear - & gt; Next=p;
Q - & gt; Rear=p;
}

Void outqueue (Linkqueue * q, int n)
{
Int x, I;
PNODE p;
If (q - & gt; The front==q - & gt; Rear)
return;
for(i=0; i{
P=q - & gt; The front - & gt; Next;
X=p - & gt; The data;
Q - & gt; The front - & gt; Next=p - & gt; Next;
If (q - & gt; Rear==p)
Q - & gt; Rear=q - & gt; The front;
free(p);
}
}

Linkqueue panduan (Linkqueue * q)
{
PNODE p;
Q1 Linkqueue *;
Linkqueue * q2;
Initqueue (q1);
Initqueue (q2);
P=q - & gt; The front - & gt; Next;
While (q - & gt; The front!=q - & gt; Rear)
{
If (p - & gt; Data % 2==0)
{
Insertqueue2 (q1, p - & gt; The data);
}
The else
{
Insertqueue2 (q2, p - & gt; The data);
}
P=p - & gt; Next;
}
PNODE p1, p2,
P1=q1 - & gt; The front - & gt; Next;
The p2=q2 - & gt; The front - & gt; Next;
While (q1 - & gt; The front!=q1 - & gt; Rear& & Q2 - & gt; The front!=q2 - & gt; Rear)
{
Printf (" % d % d \ n ", p1 - & gt; Data, p2 - & gt; The data);
P1=p1 - & gt; Next;
The p2=p2 - & gt; Next;
If (q1 - & gt; Rear==p1 & amp; & Q2 - & gt; Rear==p2)
{
Q1 - & gt; Rear=q1 - & gt; The front;
Q2 - & gt; Rear=q2 - & gt; The front;
}
Free (p1);
Free (p2);
}
}

Int main ()
{
Linkqueue q;
int n;
The scanf (" % d ", & amp; n);
Initqueue (& amp; Q);
Insertqueue1 (& amp; Q, n);
Panduan (& amp; Q);
return 0;
}
This code is where the problem is? Have not out the results
Trouble bosses use queue reply
Another program in panduan () this function, I debug it shows the program received signal SIGSEGV

CodePudding user response:

No one in? "

CodePudding user response:

Linkqueue insertqueue1 (Linkqueue * q, int n)
{
int i;
PNODE p;
P=(PNODE) malloc (sizeof (NODE));
If (p==NULL)
exit(0);
for(i=0; i{//here is you want to insert the n Numbers, but you give each to insert each queue node allocates memory
//will face the p for malloc, the former to the circle inside the
The scanf (" % d ", & amp; p-> The data);
p-> Next=NULL;
Q - & gt; Rear - & gt; Next=p;
Q - & gt; Rear=p;
}
}

To change the first, then see

CodePudding user response:

To insert the number n, malloc call n times, each time to apply for a new node, insert it into to

CodePudding user response:

I changed, I still have a question, when performing panduan this function, debugging is displayed in initqueue (q1) of this step q - & gt; The front=q - & gt; Rear=(PNODE) malloc (sizeof (NODE)) shows the program received signal SIGSEGV, not very understand, consult,

CodePudding user response:

CodePudding user response:

For
  • Related