#include
#include
Typedef int datatype.
Typedef struct node
{
Datatype data;
Struct node * next;
} linklist;
Void creatlist1 (linklist * head, int n)
{
Linklist * p, * s;
int i;
head-> next=NULL;
S=the head;
for(i=0; i
P=(linklist *) malloc (sizeof (linklist));
The scanf (" % d ", & amp; P - & gt; The data);
s-> Next=p;
S=p;
}
}
Void displaylist (linklist * head)
{
Linklist * p;
int i=0;
P=the head - & gt; next;
If (p==NULL)
{
Printf (" this is a null pointer \ n ");
}
While (p)
{
Printf (" % d ", p - & gt; The data);
P=p - & gt; next;
}
}
Int main ()
{
Linklist * head;
Head=(linklist *) malloc (sizeof (linklist));
Creatlist1 (head, 5);//to create a chain list
Displaylist (head);//the elements in the output list
return 0;
}
CodePudding user response:
P=(linklist *) malloc (sizeof (linklist));P - & gt; Next=NULL
CodePudding user response:
As friends upstairs said, code generating problem is the main reason for the displaylist () function ofwhile (p)
{
Printf (" % d ", p - & gt; The data);
P=p - & gt; next;
}
When traversing the list, the last element, the theoretical p - & gt; Next should point to null Pointers, but since createlist1 () function allocates memory for loop, the no of p - & gt; Next value explicitly assigned a null pointer value, leading to the last element of p - & gt; Next is not null pointer but a random value, in I debug VS2015, this value is 0 XCDCDCDCD, thus leading to the displaylist () function while can access 0 XCDCDCDCD () statement, and the address can't access, can direct error,
Solution is in createlist1 () function for loop in
p=(linklist *) malloc (sizeof (linklist));
P - & gt; Next=nullptr;
CodePudding user response:
Singly linked list data structure on data sort http://bbs.csdn.net/topics/392201633CodePudding user response:
# include
#include
Typedef int datatype.
Typedef struct node
{
Datatype data;
Struct node * next;
} linklist;
Void creatlist1 (linklist * head, int n)
{
Linklist * p, * s;
int i;
head-> next=NULL;
S=the head;
for(i=0; i{
P=(linklist *) malloc (sizeof (linklist));
The scanf (" % d ", & amp; P - & gt; The data);
s-> Next=p;
S=p;
}
s-> Next=NULL;//the last node 's next pointer NULL
}
Void displaylist (linklist * head)
{
Linklist * p;
//int I=0;
P=the head - & gt; next;
If (p==NULL)
{
Printf (" this is a null pointer \ n ");
return;//exit from displylist
}
While (p)
{
Printf (" % d ", p - & gt; The data);
P=p - & gt; next;
}
}
Int main ()
{
Linklist * head;
Head=(linklist *) malloc (sizeof (linklist));
Creatlist1 (head, 5);//to create a chain list
Displaylist (head);//the elements in the output list
return 0;
}
For your reference ~
Why is the time to create a list on the last node for processing, namely the last node next pointer should point to NULL
Displaylist in an infinite loop is because had not found a NULL