Home > Back-end >  C language to create a linked list
C language to create a linked list

Time:09-24

Excuse me each bosses, I this code is wrong? Above vs2012 I compile run he stop working, and so on c - free5, after the length of the input, the input is stopped working after the first value

# include
# include
# include
Typedef struct node
{
The int data;//define a data segment
Struct node * pNext;//define a pointer field
} Node, * Pnode;

Pnode creat_list (void);
Void traverse_list (Pnode Phead);

Int main (void)
{
Pnode pHead=NULL;
PHead=creat_list ();//create a acyclic singly linked list, the list of the first node address pay Phead
Traverse_list (pHead);//traversal
return 0;

}
Pnode creat_list (void)
{
int len;//to hold the number of effective node
int val;//used for temporary storage of user input node values
int i;


//allocate a valid data is not stored head node
Pnode pHead=(Pnode) malloc (sizeof (Node));
If (NULL==pHead)
{
Printf (" allocation failure, the program terminates. \n");
exit(-1);
}
Pnode pTail=pHead;
PTail - & gt; PNext=NULL;


Printf (" please input the length of the list: len=");
The scanf (" % d ", & amp; Len);

for(i=0; I{
Printf (" please enter the first % d node values ", I + 1);
The scanf (" % d, & amp; Val ");
Pnode pNew=(Pnode) malloc (sizeof (Node));
If (NULL==pNew)
{
Printf (" allocation failure, the program terminates. \n");
exit(-1);
}
PNew - & gt; Data=https://bbs.csdn.net/topics/val;
PTail - & gt; PNext=pNew;
PNew - & gt; PNext=NULL;
PTail=pNew;

}
Return pHead;
}
Void traverse_list (Pnode pHead)
{
Pnode p=pHead - & gt; PNext;
While (NULL!=p)
{
Printf (" % d ", p - & gt; The data);
P=p - & gt; PNext;
}
Printf (" \ n ");
return;
}


CodePudding user response:

 
# include
# include
# include
Typedef struct node
{
The int data;//define a data segment
Struct node * pNext;//define a pointer field
} Node, * Pnode;

Pnode creat_list (void);
Void traverse_list (Pnode Phead);

Int main (void)
{
Pnode pHead=NULL;
PHead=creat_list ();//create a acyclic singly linked list, the list of the first node address pay Phead
Traverse_list (pHead);//traversal
return 0;

}
Pnode creat_list (void)
{
int len;//to hold the number of effective node
int val;//used for temporary storage of user input node values
int i;


//allocate a valid data is not stored head node
Pnode pHead=(Pnode) malloc (sizeof (Node));
If (NULL==pHead)
{
Printf (" allocation failure, the program terminates. \n");
exit(-1);
}
Pnode pTail=pHead;
PTail - & gt; PNext=NULL;


Printf (" please input the length of the list: len=");
The scanf (" % d ", & amp; Len);

for(i=0; I{
Printf (" please enter the first % d node values ", I + 1);
The scanf (" % d, & amp; Val ");//write wrong here, the scanf (" % d ", & amp; Val);
Pnode pNew=(Pnode) malloc (sizeof (Node));
If (NULL==pNew)
{
Printf (" allocation failure, the program terminates. \n");
exit(-1);
}
PNew - & gt; Data=https://bbs.csdn.net/topics/val;
PTail - & gt; PNext=pNew;
PNew - & gt; PNext=NULL;
PTail=pNew;

}
Return pHead;
}
Void traverse_list (Pnode pHead)
{
Pnode p=pHead - & gt; PNext;
While (NULL!=p)
{
Printf (" % d ", p - & gt; The data);
P=p - & gt; PNext;
}
Printf (" \ n ");
return;
}

CodePudding user response:

[b class, C language is so difficult to find errors, might as well learn python] [/b]

CodePudding user response:

 # include 
//# include
# include

Typedef struct node
{
The int data;
Struct node * pNext;
} Node, * Pnode;

Pnode creat_list (void);
Void traverse_list (Pnode Phead);

Int main (void)
{
Pnode pHead=NULL;
PHead=creat_list ();
Traverse_list (pHead);
return 0;

}
Pnode creat_list (void)
{
int len;
int val;
int i;


//allocate a valid data is not stored head node
Pnode pHead=(Pnode) malloc (sizeof (Node));
If (NULL==pHead)
{
Printf (" allocation failure, the program terminates. \n");
exit(-1);
}
Pnode pTail=pHead;
PTail - & gt; PNext=NULL;


Printf (" please input the length of the list: len=");
The scanf (" % d ", & amp; Len);

for(i=0; I{
Printf (" please enter the first % d node values ", I + 1);
The scanf (" % d ", & amp; Val);/* this place the error of the compiler should have prompted to */
Pnode pNew=(Pnode) malloc (sizeof (Node));
If (NULL==pNew)
{
Printf (" allocation failure, the program terminates. \n");
exit(-1);
}
PNew - & gt; Data=https://bbs.csdn.net/topics/val;
PTail - & gt; PNext=pNew;
PNew - & gt; PNext=NULL;
PTail=pNew;

}
Return pHead;
}
Void traverse_list (Pnode pHead)
{
Pnode p=pHead - & gt; PNext;
While (NULL!=p)
{
Printf (" % d ", p - & gt; The data);
P=p - & gt; PNext;
}
Printf (" \ n ");
return;
}


Suggest remove the unnecessary comments, annotation too much will make the code looks more messy,
For a look at the code can understand don't need to add a comment, this code to create the linked list method can add a comment: tail interpolation to create a linked list

CodePudding user response:

The
reference 3 floor cfjtaishan response:
 # include 
//# include
# include

Typedef struct node
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related