Home > Back-end >  C singly linked list data input, print
C singly linked list data input, print

Time:12-09

#include
#include
Typedef struct lnode {
The int data;
Struct lnode * next;
} lnode * linklist;


Int main (void)
{
Linklist l;
L=NULL;
Int number;
for(int i=0; i<5; I++)
{
Input a number: the scanf (" % d ", & amp; Number);
Linklist s=(lnode *) malloc (sizeof (lnode));
S - & gt; data=https://bbs.csdn.net/topics/number;
S - & gt; Next=NULL;
Linklist last=l;
If (last)
{
While (the last - & gt; Next)
{
The last of the last=- & gt; Next;
}
Last - & gt; Next=s;
}
The else l=s;
}
Linklist p=l;
While (p - & gt; Next!=NULL)
Printf (" % d \ n ", p - & gt; The data);

return 0;
}

This is my code, but can't realize the input and output, to solve the ~

CodePudding user response:

 # include 
#include
Typedef struct lnode {
The int data;
Struct lnode * next;
} lnode * linklist;
Int main (void)
{
Linklist l;
L=NULL;
Int number;
For (int I=0; I & lt; 5; I + +)
{
Printf (" input a number: ");
The scanf (" % d ", & amp; Number);
Linklist s=(lnode *) malloc (sizeof (lnode));
S - & gt; Data=https://bbs.csdn.net/topics/number;
S - & gt; Next=NULL;
Linklist last=l;
If (last)
{
While (the last - & gt; Next) last=last - & gt; Next;
Last - & gt; Next=s;
}
The else l=s;
}
Linklist p=l;
While (p)
{
Printf (" % d \ n ", p - & gt; The data);
P=p - & gt; Next;
}
return 0;
}

CodePudding user response:

Your input and output to the

CodePudding user response:

 # include 
#include

Typedef struct lnode {
The int data;
Struct lnode * next;
} lnode * linklist;


Int main (void)
{
Linklist l, last;
L=NULL;
Int number;
for(int i=0; i<5; I++)
{
Input a number://the scanf (" % d ", & amp; Number);
Printf (" input a number: ");
The scanf (" % d ", & amp; Number);
Linklist s=(lnode *) malloc (sizeof (lnode));
S - & gt; data=https://bbs.csdn.net/topics/number;
S - & gt; Next=NULL;

if (! L)
L=s;
The else
Last - & gt; Next=s;
The last=s;
/*
Linklist last=l;
If (last)
{
While (the last - & gt; Next)
{
The last of the last=- & gt; Next;
}
Last - & gt; Next=s;
}
The else l=s;
*/
}
Linklist p=l;
//while (p - & gt; Next!=NULL)
While (p) {
Printf (" % d \ n ", p - & gt; The data);
P=p - & gt; Next;
}

return 0;
}

For your reference ~