Home > Back-end >  Use questions about the list
Use questions about the list

Time:11-03

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;
}

CodePudding user response:

reference 1/f, the truth is right or wrong 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;
}


Here should use a loop to every node to L allocate memory

CodePudding user response:

 # include 
#include
#include
//# include & lt; Stdio. H>
//# include & lt; Stdlib. H>

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);
Void ReArry (int * x, int len);
//int a [1000005], [1000005] b;

# define MAX_SIZE 8

Int main ()
{
The List l1 and l2;
Int * a * b;
Int k1=0, k2=0, n.//, k=0;

A=(int *) malloc (sizeof (int) * MAX_SIZE);
if (! A)
exit(0);

While (1)
{
The scanf (" % d ", & amp; n);
If (n==1)
break;
A [k1 + +]=n;
If (k1 % MAX_SIZE==0) {
A=(int *) realloc (a, sizeof (int) * (k1 + MAX_SIZE));
if (! A)
exit(0);
}

}

B=(int *) malloc (sizeof (int) * MAX_SIZE);
if (! B)
exit(0);

While (1)
{
The scanf (" % d ", & amp; n);
If (n==1)
break;
B [k2 + +]=n;

If (k2 % MAX_SIZE==0) {
B=(int *) realloc (b, sizeof (int) * (k2 + MAX_SIZE));
if (! B)
exit(0);
}
}

ReArry (a, k1);
ReArry (b, k2);
L1=CreatList (a, k1);
Printf ("=====\ n ");
L2=CreatList (b, k2);

Free (a);
Free (b);
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 ");
Q=(List) malloc (sizeof (struct Node));
if (! Q)
exit(0);
Q - & gt; Data=https://bbs.csdn.net/topics/x [I];
P - & gt; Next=q;
P=q;
}
P - & gt; Next=NULL;
Return the L;
}

//int * ReArry (int * x, int n)
Void 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 x;
}

For your reference ~


Create a list of some problems;
Along with other people is not good place changed, can consult;

  • Related