-->
当前位置:首页 > 题库

PROGRAMMING:Creation and output of one way linked list

Luz5年前 (2021-05-10)题库380
This topic requires two additional functions to achieve the following functions:
Enter several positive integers and end with - 1. Add nodes to the linked list to * * create * * a single linked list and * * output * * the single linked list.
###Add a node function to the end of the linked list:
`Link AppendNode(Link head,int data);`
The link structure is defined as follows:
```
typedef struct link
{
int data;
struct link *next;
}*Link;
```
*'head': pointer to the chain header. If head is null, a new head node will be created
*'data ': the data value of the node to be added
*Function return value: pointer to the chain header after adding a node
###Output linked list function:
`void DisplyNode(Link head);`
*'head': pointer of chain header
###Main function example:
```
#include
#include
typedef struct link
{
int data;
struct link *next;
}*Link;
Link AppendNode(Link head,int data);
void DisplyNode(Link head);
int main()
{
int data;
Link head = NULL;
while (1)
{
scanf("%d",&data);
if (data==-1)
break;
head = AppendNode(head,data);
}
DisplyNode(head);
return 0;
}
/*Implement the appendnode function here*/
/*The displynode function is implemented here*/
```
###Input format:
Enter several positive integers (space separated) from the keyboard, ending with - 1.
###Output format:
The data element values of each node in the single linked list are output in turn, and the elements are separated by commas. If the linked list is empty, null is output. See the output example.
###Input example:
```in
1 3 5 7 9 -1
```
###Output example:
```out
1,3,5,7,9
```
###Input example:
```in
-1
```
###Output example:
```out
NULL
```







answer:If there is no answer, please comment