In brush to a problem to solve the problem, need to the length of the array and array and returns a linked list, but after the function call will be stuck in this method, also don't know is why errors, hope leaders can solve
The following function: The List CreatList (int x [], int n) { The List L, p, q; L=(List) malloc (sizeof (struct Node)); P=L; for(int i=0 ; I & lt; n; I + +) { Printf (" ok "); L - & gt; Data=https://bbs.csdn.net/topics/x [I]; Q=L; L=L - & gt; Next; } L - & gt; Next=NULL; return p; }
The following is the source code:
#include #include #include #include #include Typedef int ElementType; Typedef struct Node * PtrToNode; Struct Node { ElementType Data; PtrToNode Next; }; Typedef PtrToNode List; The List CreatList (int x [], int n); Int * ReArry (int * x, int len); Int a [1000005], [1000005] b; Int main () { The List l1 and l2; Int k1=0, k2=0, n, k=0; While (1) { The scanf (" % d ", & amp; n); If (n==1) break; A [k1 + +]=n;
} While (1) { The scanf (" % d ", & amp; n); If (n==1) break; B [k2 + +]=n;
} ReArry (a, k1); ReArry (b, k2); L1=CreatList (a, k1); Printf ("=====\ n "); L2=CreatList (b, k2); return 0; } The List CreatList (int x [], int n) { The List L, p, q; L=(List) malloc (sizeof (struct Node)); P=L; for(int i=0 ; I & lt; n; I + +) { Printf (" ok "); L - & gt; Data=https://bbs.csdn.net/topics/x [I]; Q=L; L=L - & gt; Next; } L - & gt; Next=NULL; return p; }
Int * ReArry (int * x, int n) { Int I, j, t; for(i=0; i{ For (j=I + 1; j{ If (x [I] & gt; X [j]) { T=x [I]; [I]=x x [j]; X [j]=t; } } } Return the x; }
CodePudding user response:
Did you use to initialize the object The List CreatList (int x [], int n) { The List L, p, q; L=(List) malloc (sizeof (struct Node));//you only malloc here P=L; for(int i=0 ; I & lt; n; I + +) { Printf (" ok "); L - & gt; Data=https://bbs.csdn.net/topics/x [I]; Q=L; L=L - & gt; Next;//but here next not malloc } L - & gt; Next=NULL; return p; }