Home > Back-end >  Novice to answer questions in c list
Novice to answer questions in c list

Time:09-23

 
# include "stdio.h"
# include "stdlib. H"
//here to create a structure used to represent the linked list node type
Struct node
{
The int data;
Struct node * next;
};
Int main ()
{
Head, struct node * * p * q * t;
Q=NULL;
Int I, n, a;
Scanf_s (" % d ", & amp; n);
The head=NULL;//head initial null pointer
For (I=1; I & lt;=n; I++)//cycle read number n
{
Scanf_s (" % d ", & amp; A);
//a dynamic application memory space to store a node, and temporary Pointers point to the node p
P=(struct node *) malloc (sizeof (struct node));
P - & gt; Data=https://bbs.csdn.net/topics/a;//store the data to the current node's data domain
P - & gt; Next=NULL;//set the current node pointer to empty, following is the current nodes under a null
If (head==NULL)
The head=p;//if this is the first to create node, then turn the head pointer to the node
The else
Q - & gt; Next=p;//if not the first to create nodes, will be on a subsequent node pointer to the current node
q=p;//pointer q also points to the current node
}
//output all Numbers in the list
T=head;
While (t!=NULL)
{
Printf (" % d ", t - & gt; The data);
T=t - & gt; next;//move on to the next node
}
getchar(); getchar();
return 0;
}


In this code, please line 26 q - & gt; Next=p, what is the difference between with 27 q=p?

And the head=p code, that the p what this structure pointer assignment to the head pointer?

CodePudding user response:

Q - & gt; Next=p, meaning behind the nodes added p q, q=p, which means the pointer q point p, convenient to add the next cycle, head=p comments write very clear, if you can understand, in the paper,
  • Related