Home > Back-end >  The great spirit show singly linked lists
The great spirit show singly linked lists

Time:10-04

Input and output contains five singly linked list of nodes, why I write only input, but can't output? Refer to

CodePudding user response:

Put the head in the create function=p; To the head - & gt; Next=p;
Recommended code directly, can also help you debug the ~

CodePudding user response:

The
reference 1/f, confident boy reply:
in the create function head=p; To the head - & gt; Next=p;
Advice code directly, but also can help you debug it ~

#include
#include
#include
Struct node {
int data;
Struct node * link;
} the head;
Typedef struct node NODK;
Void createList (NODK * head, int n) {
NODK * p * q;
int i;
P=(NODK *) malloc (sizeof (NODK));
The head=p;
for(i=1; I<=n; I++) {
Q=(NODK *) malloc (sizeof (NODK));
The scanf (" % d ", & amp; Q - & gt; The data);
Q - & gt; The link=NULL;
P - & gt; The link=q;
P=q;
}
}
Void outputList (NODK * head) {
NODK * p;
P=the head - & gt; The link;
While (p - & gt; The link!=NULL) {
Printf (" % d "6, p - & gt; The data);
P=p - & gt; The link;
}
}
Int main () {
Printf (" please input the value of the five nodes: ");
CreateList (& amp; The head, 5);
OutputList (& amp; The head);
return 0;
}

CodePudding user response:

The
reference 1/f, confident boy reply:
in the create function head=p; To the head - & gt; Next=p;
Advice code directly, but also can help you debug it ~

Changed after the output, but seems to be wrong

CodePudding user response:

 # include 
#include
#include

Struct node {
int data;
Struct node * link;
} the head;

Typedef struct node NODK;

Void createList (NODK * head, int n)
{
NODK * p * q;
int i;
//p=(NODK *) malloc (sizeof (NODK));
//the head - & gt; The link=p;
P=the head;
for(i=1; I<=n; I++) {
Q=(NODK *) malloc (sizeof (NODK));
The scanf (" % d ", & amp; Q - & gt; The data);
Q - & gt; The link=NULL;
P - & gt; The link=q;
P=q;
}
}
Void outputList (NODK * head)
{
NODK * p;
P=the head - & gt; The link;
//while (p - & gt; The link!=NULL) {
While (p) {
Printf (" % d "6, p - & gt; The data);
P=p - & gt; The link;
}
}
Int main () {
Printf (" please input the value of the five nodes: ");
CreateList (& amp; The head, 5);
OutputList (& amp; The head);
return 0;
}

For your reference ~

CodePudding user response:

Singly linked list data structure on data sort http://bbs.csdn.net/topics/392201633

CodePudding user response:

refer to the second floor LuYuKe response:
Quote: reference 1/f, confident boy reply:
in the create function head=p; To the head - & gt; Next=p;
Advice code directly, but also can help you debug it ~

#include
#include
#include
Struct node {
int data;
Struct node * link;
} the head;
Typedef struct node NODK;

Void createList (NODK * head, int n) {
NODK * p * q;
int i;
P=(NODK *) malloc (sizeof (NODK));
The head=p;
for(i=1; I<=n; I++) {
Q=(NODK *) malloc (sizeof (NODK));
The scanf (" % d ", & amp; Q - & gt; The data);
Q - & gt; The link=NULL;
P - & gt; The link=q;
P=q;
}
}
Void outputList (NODK * head) {
NODK * p;
P=the head - & gt; The link;
While (p - & gt; The link!=NULL) {
Printf (" % d "6, p - & gt; The data);
P=p - & gt; The link;
}
}
Int main () {
Printf (" please input the value of the five nodes: ");
CreateList (& amp; The head, 5);
OutputList (& amp; The head);
return 0;
}


Your head to the p (head=p)
Point p and q (p=q);
List at the end of q - the link=null, which is equivalent to the head=null
Your head have been changed, so there is no output,

CodePudding user response:

Or you can change a bit long-winded

#include
#include
#include
Struct node
{
int data;
Struct node * link;
} the head;
Typedef struct node NODK;
Void createList (NODK * head, int n)
{
NODK * p * q;
int i;
P=(NODK *) malloc (sizeof (NODK));
The head - & gt; The link=p;
For (I=1; I & lt;=n; I++)
{
Q=(NODK *) malloc (sizeof (NODK));
The scanf (" % d ", & amp; Q - & gt; The data);
Q - & gt; The link=NULL;
P - & gt; The link=q;
P=q;
}
}
Void outputList (NODK * head)
{
NODK * p;
P=the head - & gt; The link;
While (p)
{
Printf (" % d "6, p - & gt; The link - & gt; The data);
P=p - & gt; The link;
}
}
Int main ()
{
Printf (" input the number 5: ");
CreateList (& amp; The head, 5);
OutputList (& amp; The head);
return 0;
}

CodePudding user response:

5250 reference 7 floor response:
or you can change, is a bit repetitive

#include
#include
#include
Struct node
{
int data;
Struct node * link;
} the head;
Typedef struct node NODK;
Void createList (NODK * head, int n)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related