#include <stdio.h>
#include <stdlib.h>
typedef struct NODE
{
int data;//data field
struct Node * pnext;//pointer field
}node, *pnode;
Pnode create_list (void) {
int len;//The number of valid nodes to hold
int i;//The value of the node used to temporarily store user input
The value of the INT val;//node
Pnode Phead = (pnode) malloc (sizeof (NODE));
if (NULL = = Phead) {
printf ("Allocation failed, program terminated!") ");
Exit (-1);
}
printf ("Please enter the number of linked lists you want to enter");
scanf ("%d", &len);
Pnode ptail = Phead;
Ptail->pnext = Null;//ptail always points to the tail node
for (i=0; i<len; ++i) {
printf ("Please enter the value of%d nodes:", i+1);
scanf ("%d", &val);
Pnode pnew = (pnode) malloc (sizeof (NODE));
if (NULL = = pnew)
{
printf ("Allocation failed, program terminated!") ");
Exit (-1);
}
Pnew->data = val;
Ptail->pnext = pnew;
Pnew->pnext = NULL;
Ptail = pnew;
}
return phead;
}
void Traver_list (Pnode phead) {
Pnode ptmp = phead;//Set a temporary node for traversing
while (1) {
if (Ptmp->pnext = = NULL) {
Return
}
printf ("%d", ptmp->pnext->data);
Ptmp->pnext = ptmp->pnext->pnext;
}
}
int main (int argc, const char * argv[]) {
Pnode Phead = null;//equivalent to struct Node *phead = NULL;
Phead = Create_list ();//create a non-circular single-linked list and pay the first address to Phead
Traver_list (Phead);//Iterate through the list
return 0;
}
Data structure-linked list C language implementation