Home > Back-end >  C insert problem list
C insert problem list

Time:09-30

Bosses, help me to look at the code to collapse! I want to create a linked list, insert an element behind list and then output the elements, but is an error occurred when insert!!

#include
#include

Typedef struct Node {
The int data;
Struct Node * next;
} the Node;
Typedef struct Node * List;

Void InitList (List L)
{
L=(List) malloc (sizeof (Node));
if(! L)
{
Printf (" memory allocation failure!" );
The exit (0);
}
L - & gt; Next=NULL;
}

Void Insert (the List L, int a)
{
The List of p, s;
P=L;
S=(List) malloc (sizeof (Node));
if(! S)
{
Printf (" memory allocation failure!" );
The exit (0);
}
S - & gt; Next=NULL;
S - & gt; data=https://bbs.csdn.net/topics/a;
S - & gt; Next=p - & gt; next;
P - & gt; Next=s;

}

The main ()
{
List the ha;
InitList (ha);
Insert (ha, 1);
Printf (" % d ", ha - & gt; Next - & gt; The data);
}

CodePudding user response:

This pass you have any question, you initialize a ha a pointer, the pointer to initialize, it is invalid, accept parameters you there is also a list, type, is the value of you in the init assignment is to give the L in operation, and ha it doesn't matter,

CodePudding user response:

So you take the NULL ha inserted, NULL - & gt; Next, so in the s - next=p - when they next segmentation fault
  • Related