Home > Back-end >  The pta Reversing Linked List for help
The pta Reversing Linked List for help

Time:10-20

 # include & lt; stdio.h> 
#include

Typedef struct Node {
Int adress.
The int data;
Int nextadress;
Struct Node * Next;
} the Node;

Typedef struct Node * List;

Int main () {
The List L1, L, p, q;
Int fadress, k, n.
The scanf (" % d % d % d ", & amp; Fadress, & amp; N, & amp; k);
int i;
L1=(List) malloc (sizeof (Node));
L1 - & gt; Next=NULL;
Q=L1.
for(i=0; iP=(List) malloc (sizeof (Node));
The scanf (" % d % d % d ", & amp; P - & gt; Adress, & amp; P - & gt; The data, & amp; P - & gt; Nextadress);
Q - & gt; Next=p;
Q=p;

}
Q - & gt; Next=NULL;
/* the List t;
T=L1 - & gt; Next;
While (t) {
If (t - & gt; Next) {
05 05 printf (" % d % d % d \ n ", t - & gt; Adress, t - & gt; The data, t - & gt; Nextadress);
} else {
Printf (" % 5 d % d % d \ n ", t - & gt; Adress, t - & gt; The data, t - & gt; Nextadress);
}

T=t - & gt; Next;
}
printf("\n"); */
The List newL newp, newq, t;
NewL=(List) malloc (sizeof (Node));
NewL - & gt; Next=NULL;
Newq=newL;
T=L1 - & gt; Next;
For (I=k; I>=1; I -) {
While (t) {
If (t - & gt; data=https://bbs.csdn.net/topics/=i) {
Newp=(List) malloc (sizeof (Node));
Newp - & gt; Adress=t - & gt; Adress.
Newp - & gt; data=https://bbs.csdn.net/topics/t-> data;
Newp - & gt; Nextadress=t - & gt; Nextadress;
Newq - & gt; Next=newp;
Newq=newp;
break;
} else {
T=t - & gt; Next;
}
}
T=L1 - & gt; Next;
}
For (I=k + 1; i<=n; I++) {
T=L1 - & gt; Next;
While (t) {
If (t - & gt; data=https://bbs.csdn.net/topics/=i) {
Newp=(List) malloc (sizeof (Node));
Newp - & gt; Adress=t - & gt; Adress.
Newp - & gt; data=https://bbs.csdn.net/topics/t-> data;
Newp - & gt; Nextadress=t - & gt; Nextadress;
Newq - & gt; Next=newp;
Newq=newp;
break;
} else {
T=t - & gt; Next;
}
}
}
Newq - & gt; Next=NULL;
List the L2, x1, x2,
X1=newL - & gt; Next;
X2=x1 - & gt; Next;
L2=newL;
for(i=0; iIf (x2 - & gt; Next) {
X1 - & gt; Nextadress=x2 - & gt; Adress.
L2 - & gt; Next=x1;
L2=x1;
X1=x1 - & gt; Next;
X2=x2 - & gt; Next;

}

}
X1 - & gt; Nextadress=x2 - & gt; Adress.
L2 - & gt; Next=x1;
L2=x1;
X1=x1 - & gt; Next;

L2 - & gt; Next=x1;
L2=x1;
L2 - & gt; Next=NULL;


The List f;
F=newL - & gt; Next;
While (f) {
If (f - & gt; Next) {
05 05 printf (" % d % d % d \ n ", f - & gt; Adress, f - & gt; The data, the f - & gt; Nextadress);
} else {
Printf (" % 5 d % d % d \ n ", f - & gt; Adress, f - & gt; The data, the f - & gt; Nextadress);
}

F=f - & gt; Next;
}



return 0;
}

I run on their own ide the answer with the same case as soon as you submit prompt segmentation fault... Small white for guiding

This is the result of I run the compiler

A case of topic request

CodePudding user response:

Seek bosses advice QAQ

CodePudding user response:

CodePudding user response:

Said the demand

CodePudding user response:

List the L2, x1, x2,
X1=newL - & gt; Next;
X2=x1 - & gt; Next;
L2=newL;
for(i=0; iIf (x2 - & gt; Next) {

80 lines before and after this period, you can ensure newL of at least two elements?
If there is only one element, x2=x1 - & gt; Next, the x2 is NULL, the inside of the for loop x2 - & gt; Next is the incorrect access

CodePudding user response:




Fun
reference 4 floor response:
List L2, x1, x2,
X1=newL - & gt; Next;
X2=x1 - & gt; Next;
L2=newL;
for(i=0; iIf (x2 - & gt; Next) {

80 lines before and after this period, you can ensure newL of at least two elements?
If there is only one element, x2=x1 - & gt; Next, the x2 is NULL, the inside of the for loop x2 - & gt; Next is the incorrect access

I changed this part of the code
 List L2, x1, x2, x3. 
X1=newL - & gt; Next;
L2=(List) malloc (sizeof (Node));
L2 - & gt; Next=NULL;
X2=L2;
While (x1) {
The x3=(List) malloc (sizeof (Node));
The x3 - & gt; Adress=x1 - & gt; Adress.
The x3 - & gt; data=https://bbs.csdn.net/topics/x1-> data;
If (x1 - & gt; Next) {
The x3 - & gt; Nextadress=x1 - & gt; Next - & gt; Adress.
} else {
The x3 - & gt; Nextadress=1;
}
X2 - & gt; Next=x3;
X2=x3;
X1=x1 - & gt; Next;
}
X2 - & gt; Next=NULL;

Segmentation fault problem is gone but I run error or is the same with the sample submitted..


Fun
reference 4 floor response:
List L2, x1, x2,
X1=newL - & gt; Next;
X2=x1 - & gt; Next;
L2=newL;
for(i=0; iIf (x2 - & gt; Next) {

80 lines before and after this period, you can ensure newL of at least two elements?
If there is only one element, x2=x1 - & gt; Next, the x2 is NULL, the inside of the for loop x2 - & gt; Next is the incorrect access

I changed this part of the code
 List L2, x1, x2, x3. 
X1=newL - & gt; Next;
L2=(List) malloc (sizeof (Node));
L2 - & gt; Next=NULL;
X2=L2;
While (x1) {
The x3=(List) malloc (sizeof (Node));
The x3 - & gt; Adress=x1 - & gt; Adress.
The x3 - & gt; data=https://bbs.csdn.net/topics/x1-> data;
If (x1 - & gt; Next) {
The x3 - & gt; Nextadress=x1 - & gt; Next - & gt; Adress.
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related