Home > Back-end >  Why bother everybody to help me take a look at the code, is the access violation
Why bother everybody to help me take a look at the code, is the access violation

Time:09-22

# include
# include
//the output of the list
Struct node
{
Int num, score;
Struct node * link;
};//define the type of node

Struct node * creat (int);
Void print (node *);
Void main ()
{
The node * head=0;
The head=creat (2);
Print (the head);
getchar();
getchar();
}

Struct node * creat (int n)
{
The node * h=0, * p, * q;
int i;
for(i=1; i<=n; I++) {
Q=(*) malloc (sizeof (node));
The scanf (" % d % d ", & amp; Q - & gt; Num, & amp; Q - & gt; Score);
Q - & gt; The link=0;
If (h==0) h=q;
The else
p-> The link=0;
P=q;//p pointer to footer node
}

Return h;
}

Void print (node * h)
{
Node * p=0;
P=h;
Do
{
Printf (" num=% d \ \ n tscore=% d ", p - & gt; Num, p - & gt; Score);
P=p - & gt; The link;
}
While (h);
}

CodePudding user response:

Create and print function has bugs, fix:
 struct node * creat (int n) 
{
The node * h=0, * p;
int i;
For (I=1; i <=n; I++)
{
P=(*) malloc (sizeof (node));
The scanf (" % d % d ", & amp; p-> Num, & amp; p-> Score);
p-> The link=0;

If (h==0)
H=p;
The else
H - & gt; The link=p;
}

Return h;
}

Void print (node * h)
{
Node * p=h;

Do
{
Printf (" num=% d \ \ n tscore=% d ", p - & gt; Num, p - & gt; Score);
P=p - & gt; The link;
} while (p);
}

CodePudding user response:

Thank you very much, can ask again my mistake what reason be

CodePudding user response:

Creat the function, may be you to list the concept of understanding does not reach the designated position, logic errors,
Print function, the original code, you in the loop, h value will not change, became a dead loop,
  • Related